Ich möchte die Erstellung der Fehlerreaktion auf diese Weise zentralisieren:
app.get('/test', function(req, res){
throw {status: 500, message: 'detailed message'};
});
app.use(function (err, req, res, next) {
res.status(err.status || 500).json({status: err.status, message: err.message})
});
So habe ich immer das gleiche Fehlerausgabeformat.
PS: natürlich ein Objekt zu extend the standard error wie dies schaffen könnte:
const AppError = require('./lib/app-error');
app.get('/test', function(req, res){
throw new AppError('Detail Message', 500)
});
'use strict';
module.exports = function AppError(message, httpStatus) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.status = httpStatus;
};
require('util').inherits(module.exports, Error);
Meine Antwort auf eine verwandte Frage könnte helfen: http://stackoverflow.com/questions/10170857/cani-ireuse-the-express-js- error-view/10556093 # 10556093 – Pickels
Könnten Sie bitte die akzeptierte Antwort aktualisieren? –