fix RequestError prop name + add some tests
This commit is contained in:
parent
b470698537
commit
30ff9c1cbb
6 changed files with 34 additions and 8 deletions
|
@ -2,3 +2,4 @@ dist/
|
|||
jest.config.js
|
||||
tests/
|
||||
.eslintrc.cjs
|
||||
coverage/
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
node_modules/
|
||||
dist/
|
||||
coverage/
|
|
@ -1,10 +1,10 @@
|
|||
export class RequestError extends Error {
|
||||
|
||||
public code: number;
|
||||
public statusCode: number;
|
||||
|
||||
constructor(msg: string, code: number) {
|
||||
constructor(msg: string, statusCode: number) {
|
||||
super(msg);
|
||||
|
||||
this.code = code;
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
}
|
|
@ -38,7 +38,7 @@ export default class Service {
|
|||
const rawRes = await this.adaptors[adaptor].request(`${req.service}.${req.subject}`, rawReq);
|
||||
const res: Response<U> = JSON.parse(rawRes);
|
||||
|
||||
if(res.statusCode < 200 || res.statusCode >= 599) {
|
||||
if(res.statusCode < 200 || res.statusCode >= 299) {
|
||||
throw new RequestError("error while request", res.statusCode);
|
||||
}
|
||||
|
||||
|
|
11
tests/error.test.ts
Normal file
11
tests/error.test.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { RequestError } from "../src/error";
|
||||
|
||||
test('request error', () => {
|
||||
const expectedMsg = "This is my err";
|
||||
const expectedStatusCode = 500;
|
||||
|
||||
const err = new RequestError(expectedMsg, expectedStatusCode);
|
||||
|
||||
expect(err.message).toBe(expectedMsg);
|
||||
expect(err.statusCode).toBe(expectedStatusCode);
|
||||
});
|
|
@ -19,7 +19,7 @@ afterEach(async () => {
|
|||
await srv.stop();
|
||||
});
|
||||
|
||||
test('nats adaptor success', async () => {
|
||||
test('request success', async () => {
|
||||
const dataExpected = "hello success";
|
||||
const statusCodeExpected = 200;
|
||||
|
||||
|
@ -36,7 +36,7 @@ test('nats adaptor success', async () => {
|
|||
expect(res.statusCode).toBe(statusCodeExpected);
|
||||
});
|
||||
|
||||
test('nats adaptor error', async () => {
|
||||
test('request error', async () => {
|
||||
const dataExpected = "hello error";
|
||||
const statusCodeExpected = 500;
|
||||
|
||||
|
@ -49,3 +49,16 @@ test('nats adaptor error', async () => {
|
|||
expect(srv.request(adaptorName, {service: serviceName, subject: subject, data: dataExpected})).rejects.toThrow(RequestError);
|
||||
});
|
||||
|
||||
test('request adaptor not found', async () => {
|
||||
const dataExpected = "hello success";
|
||||
const statusCodeExpected = 200;
|
||||
|
||||
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("bad adaptor", {service: serviceName, subject: subject, data: dataExpected})).rejects.toThrow(Error);
|
||||
});
|
||||
|
Loading…
Reference in a new issue