Skip to content

Commit

Permalink
fix(platform-log-middleware): display error.code in log error
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Feb 29, 2024
1 parent a1f98e8 commit 307e8a7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {PlatformHandlerMetadata, PlatformTest} from "@tsed/common";
import {PlatformLogMiddleware} from "./PlatformLogMiddleware";
import "../domain/PlatformLogMiddlewareSettings";

async function createMiddlewareFixture({statusCode = 200, error}: {statusCode?: number; error?: any} = {}) {
const middleware = await PlatformTest.invoke<PlatformLogMiddleware>(PlatformLogMiddleware);
Expand Down Expand Up @@ -45,7 +46,10 @@ describe("PlatformLogMiddleware", () => {
describe("when no debug, logRequest", () => {
beforeEach(() =>
PlatformTest.create({
logger: {debug: false, logRequest: true}
logger: {
debug: false,
logRequest: true
}
})
);
afterEach(() => PlatformTest.reset());
Expand Down Expand Up @@ -219,6 +223,37 @@ describe("PlatformLogMiddleware", () => {
})
);
});
it("should configure request and create context logger (error.code)", async () => {
// GIVEN
const error: any = {
code: "CODE",
message: "message"
};
const {ctx, middleware} = await createMiddlewareFixture({
statusCode: 400,
error
});

// WHEN
middleware.use(ctx);

// THEN
(ctx.response.getRes().on as jest.Mock).mock.calls[0][1]();
// middleware.onLogEnd(request.$ctx as any);

// THEN
expect(PlatformTest.injector.logger.error).toHaveBeenCalledWith(
expect.objectContaining({
event: "request.end",
method: "GET",
reqId: "id",
url: "url",
status: 400,
error_name: "CODE",
error_message: "message"
})
);
});
});
describe("when no debug, log error", () => {
beforeEach(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class PlatformLogMiddleware implements MiddlewareMethods {
status_code: String(ctx.response.statusCode),
state: "KO",
...cleanObject({
error_name: ctx.error?.name,
error_name: ctx.error?.name || ctx.error?.code,
error_message: ctx.error?.message,
error_errors: ctx.error?.errors,
error_stack: ctx.error?.stack,
Expand Down

0 comments on commit 307e8a7

Please sign in to comment.