Skip to content

Commit

Permalink
improve error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rafa-lopes-pt committed Jul 9, 2024
1 parent eb683ad commit e6c7c53
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
9 changes: 6 additions & 3 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import HTTPCodes from "simple-http-codes";
import timeoutMiddleware from "./routes/middleware/timeout.middleware";
import router from "./routes/router";
import HttpError from "../../shared/types/HttpError/HttpError.type";
import { inspect } from "node:util";

const server = express();
server.use(morgan("dev"));
server.use(
Expand Down Expand Up @@ -36,9 +38,10 @@ server.use(
(error: Error, _req: Request, res: Response, _next: NextFunction) => {
if (error instanceof HttpError) {
error.log();
return res
.status(error.statusCode)
.json({ ...error, data: error.message });
return res.status(error.statusCode).json({
...error,
data: inspect(error.message, { depth: null }),
});
}

console.error(error);
Expand Down
8 changes: 1 addition & 7 deletions shared/types/HttpError/HttpError.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ErrorCodes, ErrorNames, ErrorNumbers } from "./ErrorCodes.type";
import HttpErrorContext from "./HttpErrorContext.type";

const getErrorName = (code: ErrorNumbers) =>
[...Object.entries(ErrorCodes)].filter((entry) => entry[1] === code)[0][0];

Expand Down Expand Up @@ -29,11 +28,6 @@ export default class HttpError extends Error {
}

log() {
console.error(
this.statusCode,
this.name,
this.message,
this.context || ""
);
console.dir(this, { depth: null });
}
}

0 comments on commit e6c7c53

Please sign in to comment.