Skip to content

Commit

Permalink
fix: only generate stack on HttpError if status >= 500 (#2470)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored May 27, 2024
1 parent 8865d29 commit bd60444
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { MODE } from "./runtime/server/mod.tsx";

export function getMessage(status: number): string {
switch (status) {
case 100:
Expand Down Expand Up @@ -144,7 +142,7 @@ export class HttpError {
public options?: ErrorOptions,
) {
this.message = message;
if (MODE !== "production") {
if (status >= 500) {
this.#error = new Error();
}
}
Expand Down
29 changes: 7 additions & 22 deletions src/error_test.ts
Original file line number Diff line number Diff line change
@@ -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");
});

0 comments on commit bd60444

Please sign in to comment.