From b47069853783a0b17ee5a2fa7c825d82aacd9a4b Mon Sep 17 00:00:00 2001 From: qpismont Date: Thu, 19 Oct 2023 22:53:31 +0200 Subject: [PATCH] added RequestError error with its test --- src/error.ts | 10 ++++++++++ src/service.ts | 14 ++++++++++++-- tests/lib.test.ts | 24 ++++++++++++++++++++---- 3 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 src/error.ts diff --git a/src/error.ts b/src/error.ts new file mode 100644 index 0000000..c542a80 --- /dev/null +++ b/src/error.ts @@ -0,0 +1,10 @@ +export class RequestError extends Error { + + public code: number; + + constructor(msg: string, code: number) { + super(msg); + + this.code = code; + } +} \ No newline at end of file diff --git a/src/service.ts b/src/service.ts index c043b8c..3dcd894 100644 --- a/src/service.ts +++ b/src/service.ts @@ -1,4 +1,5 @@ import Adaptor from "./adaptors/adaptor"; +import { RequestError } from "./error"; import { Request, Response } from "./messages"; import { RouteSubscribeTypeFn } from "./types"; @@ -35,10 +36,19 @@ export default class Service { try { const rawRes = await this.adaptors[adaptor].request(`${req.service}.${req.subject}`, rawReq); + const res: Response = 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) { - return { statusCode: 500 } + if(err instanceof RequestError) { + throw err; + } else { + throw new RequestError("unexpected error", 500); + } } } diff --git a/tests/lib.test.ts b/tests/lib.test.ts index 2e12c28..0d81aca 100644 --- a/tests/lib.test.ts +++ b/tests/lib.test.ts @@ -1,15 +1,15 @@ import NatsAdaptor from "../src/adaptors/nats"; +import { RequestError } from "../src/error"; import { Message } from "../src/messages"; import Service from "../src/service"; let srv!: Service; +const natsServer = '127.0.0.1:4222'; const serviceName = "test-service"; const adaptorName = "nats"; const subject = "test"; -const dataExpected = "hello world"; -const statusCodeExpected = 200; beforeEach(() => { srv = new Service(serviceName); @@ -19,8 +19,11 @@ afterEach(async () => { await srv.stop(); }); -test('nats adaptor', async () => { - srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: ['127.0.0.1:4222'] })); +test('nats adaptor success', async () => { + const dataExpected = "hello success"; + const statusCodeExpected = 200; + + srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] })); srv.subscribe(adaptorName, subject, async (msg: Message) => { return {data: msg.data, statusCode: statusCodeExpected}; }); @@ -33,3 +36,16 @@ test('nats adaptor', async () => { 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) => { + return {data: msg.data, statusCode: statusCodeExpected}; + }); + + await srv.listen(); + expect(srv.request(adaptorName, {service: serviceName, subject: subject, data: dataExpected})).rejects.toThrow(RequestError); +}); +