From bd60444f7c19d1838c9fedf212bf01f6d8846889 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 27 May 2024 14:34:36 +0200 Subject: [PATCH] fix: only generate stack on HttpError if status >= 500 (#2470) --- src/error.ts | 4 +--- src/error_test.ts | 29 +++++++---------------------- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/src/error.ts b/src/error.ts index e6e2c149..3b010cf8 100644 --- a/src/error.ts +++ b/src/error.ts @@ -1,5 +1,3 @@ -import { MODE } from "./runtime/server/mod.tsx"; - export function getMessage(status: number): string { switch (status) { case 100: @@ -144,7 +142,7 @@ export class HttpError { public options?: ErrorOptions, ) { this.message = message; - if (MODE !== "production") { + if (status >= 500) { this.#error = new Error(); } } diff --git a/src/error_test.ts b/src/error_test.ts index da86e97b..7f726816 100644 --- a/src/error_test.ts +++ b/src/error_test.ts @@ -1,27 +1,12 @@ import { expect } from "@std/expect"; -import { MODE, setMode } from "./runtime/server/mod.tsx"; import { HttpError } from "./error.ts"; -Deno.test("HttpError - contains stack in development", () => { - const tmp = MODE; - setMode("development"); - try { - const err = new HttpError(404); - expect(err.status).toEqual(404); - expect(typeof err.stack).toEqual("string"); - } finally { - setMode(tmp); - } -}); +Deno.test("HttpError - contains stack if error >=500", () => { + const err = new HttpError(404); + expect(err.status).toEqual(404); + expect(typeof err.stack).toEqual("undefined"); -Deno.test("HttpError - contains no stack in production", () => { - const tmp = MODE; - setMode("production"); - try { - const err = new HttpError(404); - expect(err.status).toEqual(404); - expect(err.stack).toEqual(undefined); - } finally { - setMode(tmp); - } + const err2 = new HttpError(500); + expect(err2.status).toEqual(500); + expect(typeof err2.stack).toEqual("string"); });