fix RequestError prop name + add some tests

This commit is contained in:
qpismont 2023-10-19 23:27:51 +02:00
parent b470698537
commit 30ff9c1cbb
6 changed files with 34 additions and 8 deletions

View file

@ -1,4 +1,5 @@
dist/
jest.config.js
tests/
.eslintrc.cjs
.eslintrc.cjs
coverage/

3
.gitignore vendored
View file

@ -1,2 +1,3 @@
node_modules/
dist/
dist/
coverage/

View file

@ -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;
}
}

View file

@ -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
View 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);
});

View file

@ -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);
});