fix: only generate stack on HttpError if status >= 500 (#2470)

This commit is contained in:
Marvin Hagemeister
2024-05-27 14:34:36 +02:00
committed by GitHub
parent 8865d290ce
commit bd60444f7c
2 changed files with 8 additions and 25 deletions
+1 -3
View File
@@ -1,5 +1,3 @@
import { MODE } from "./runtime/server/mod.tsx";
export function getMessage(status: number): string { export function getMessage(status: number): string {
switch (status) { switch (status) {
case 100: case 100:
@@ -144,7 +142,7 @@ export class HttpError {
public options?: ErrorOptions, public options?: ErrorOptions,
) { ) {
this.message = message; this.message = message;
if (MODE !== "production") { if (status >= 500) {
this.#error = new Error(); this.#error = new Error();
} }
} }
+7 -22
View File
@@ -1,27 +1,12 @@
import { expect } from "@std/expect"; import { expect } from "@std/expect";
import { MODE, setMode } from "./runtime/server/mod.tsx";
import { HttpError } from "./error.ts"; import { HttpError } from "./error.ts";
Deno.test("HttpError - contains stack in development", () => { Deno.test("HttpError - contains stack if error >=500", () => {
const tmp = MODE; const err = new HttpError(404);
setMode("development"); expect(err.status).toEqual(404);
try { expect(typeof err.stack).toEqual("undefined");
const err = new HttpError(404);
expect(err.status).toEqual(404);
expect(typeof err.stack).toEqual("string");
} finally {
setMode(tmp);
}
});
Deno.test("HttpError - contains no stack in production", () => { const err2 = new HttpError(500);
const tmp = MODE; expect(err2.status).toEqual(500);
setMode("production"); expect(typeof err2.stack).toEqual("string");
try {
const err = new HttpError(404);
expect(err.status).toEqual(404);
expect(err.stack).toEqual(undefined);
} finally {
setMode(tmp);
}
}); });