Show stack in dev
This commit is contained in:
@@ -17,6 +17,15 @@
|
||||
import { ErrorRequestHandler, NextFunction, Request, Response } from 'express';
|
||||
import * as errors from '../errors';
|
||||
|
||||
export type ErrorHandlerOptions = {
|
||||
/**
|
||||
* Whether error response bodies should show error stack traces or not.
|
||||
*
|
||||
* If not specified, by default shows stack traces only in development mode.
|
||||
*/
|
||||
showStackTraces?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Express middleware to handle errors during request processing.
|
||||
*
|
||||
@@ -30,7 +39,12 @@ import * as errors from '../errors';
|
||||
*
|
||||
* @returns An Express error request handler
|
||||
*/
|
||||
export function errorHandler(): ErrorRequestHandler {
|
||||
export function errorHandler(
|
||||
options: ErrorHandlerOptions = {},
|
||||
): ErrorRequestHandler {
|
||||
const showStackTraces =
|
||||
options.showStackTraces ?? process.env.NODE_ENV === 'development';
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
return (
|
||||
error: Error,
|
||||
@@ -43,7 +57,7 @@ export function errorHandler(): ErrorRequestHandler {
|
||||
}
|
||||
|
||||
const status = getStatusCode(error);
|
||||
const message = error.message;
|
||||
const message = showStackTraces ? error.stack : error.message;
|
||||
response.status(status).send(message);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"license": "Apache-2.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "tsc-watch --onFirstSuccess \"nodemon dist/run.js\"",
|
||||
"start": "tsc-watch --onFirstSuccess \"cross-env NODE_ENV=development nodemon dist/run.js\"",
|
||||
"build": "tsc",
|
||||
"lint": "backstage-cli lint",
|
||||
"test": "backstage-cli test",
|
||||
|
||||
Reference in New Issue
Block a user