added RequestError error with its test
This commit is contained in:
parent
4c55419afc
commit
b470698537
3 changed files with 42 additions and 6 deletions
10
src/error.ts
Normal file
10
src/error.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
export class RequestError extends Error {
|
||||||
|
|
||||||
|
public code: number;
|
||||||
|
|
||||||
|
constructor(msg: string, code: number) {
|
||||||
|
super(msg);
|
||||||
|
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import Adaptor from "./adaptors/adaptor";
|
import Adaptor from "./adaptors/adaptor";
|
||||||
|
import { RequestError } from "./error";
|
||||||
import { Request, Response } from "./messages";
|
import { Request, Response } from "./messages";
|
||||||
import { RouteSubscribeTypeFn } from "./types";
|
import { RouteSubscribeTypeFn } from "./types";
|
||||||
|
|
||||||
|
@ -35,10 +36,19 @@ export default class Service {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const rawRes = await this.adaptors[adaptor].request(`${req.service}.${req.subject}`, rawReq);
|
const rawRes = await this.adaptors[adaptor].request(`${req.service}.${req.subject}`, rawReq);
|
||||||
|
const res: Response<U> = JSON.parse(rawRes);
|
||||||
|
|
||||||
return JSON.parse(rawRes);
|
if(res.statusCode < 200 || res.statusCode >= 599) {
|
||||||
|
throw new RequestError("error while request", res.statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
return { statusCode: 500 }
|
if(err instanceof RequestError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new RequestError("unexpected error", 500);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
import NatsAdaptor from "../src/adaptors/nats";
|
import NatsAdaptor from "../src/adaptors/nats";
|
||||||
|
import { RequestError } from "../src/error";
|
||||||
import { Message } from "../src/messages";
|
import { Message } from "../src/messages";
|
||||||
import Service from "../src/service";
|
import Service from "../src/service";
|
||||||
|
|
||||||
let srv!: Service;
|
let srv!: Service;
|
||||||
|
|
||||||
|
const natsServer = '127.0.0.1:4222';
|
||||||
const serviceName = "test-service";
|
const serviceName = "test-service";
|
||||||
const adaptorName = "nats";
|
const adaptorName = "nats";
|
||||||
const subject = "test";
|
const subject = "test";
|
||||||
|
|
||||||
const dataExpected = "hello world";
|
|
||||||
const statusCodeExpected = 200;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
srv = new Service(serviceName);
|
srv = new Service(serviceName);
|
||||||
|
@ -19,8 +19,11 @@ afterEach(async () => {
|
||||||
await srv.stop();
|
await srv.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('nats adaptor', async () => {
|
test('nats adaptor success', async () => {
|
||||||
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: ['127.0.0.1:4222'] }));
|
const dataExpected = "hello success";
|
||||||
|
const statusCodeExpected = 200;
|
||||||
|
|
||||||
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
||||||
srv.subscribe(adaptorName, subject, async (msg: Message<string>) => {
|
srv.subscribe(adaptorName, subject, async (msg: Message<string>) => {
|
||||||
return {data: msg.data, statusCode: statusCodeExpected};
|
return {data: msg.data, statusCode: statusCodeExpected};
|
||||||
});
|
});
|
||||||
|
@ -33,3 +36,16 @@ test('nats adaptor', async () => {
|
||||||
expect(res.statusCode).toBe(statusCodeExpected);
|
expect(res.statusCode).toBe(statusCodeExpected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('nats adaptor error', async () => {
|
||||||
|
const dataExpected = "hello error";
|
||||||
|
const statusCodeExpected = 500;
|
||||||
|
|
||||||
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
||||||
|
srv.subscribe(adaptorName, subject, async (msg: Message<string>) => {
|
||||||
|
return {data: msg.data, statusCode: statusCodeExpected};
|
||||||
|
});
|
||||||
|
|
||||||
|
await srv.listen();
|
||||||
|
expect(srv.request(adaptorName, {service: serviceName, subject: subject, data: dataExpected})).rejects.toThrow(RequestError);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue