Work in progress on a React-based error reporting mechanism.

The idea is that REST handlers always fail with throwing an Error (subclass of InteroperableError). The InteroperableError contains type and data field which are JSON-serialized and sent to client. It's up to the client to interpret the error and choose an appropriate way to present it.
This commit is contained in:
Tomas Bures 2017-06-05 00:52:59 +02:00
parent 4504d539c5
commit 79ea9e1897
10 changed files with 304 additions and 142 deletions

53
app.js
View file

@ -44,6 +44,8 @@ const reports = require('./routes/reports');
const reportsTemplates = require('./routes/report-templates');
const namespaces = require('./routes/namespaces');
const interoperableErrors = require('./lib/interoperable-errors');
const app = express();
// view engine setup
@ -245,11 +247,27 @@ if (app.get('env') === 'development') {
if (!err) {
return next();
}
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
if (req.needsJSONResponse) {
const resp = {
message: err.message,
error: err
};
if (err instanceof InteroperableError) {
resp.type = err.type;
resp.data = err.data;
}
res.status(err.status || 500).json(resp);
} else {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
}
});
}
@ -259,11 +277,26 @@ app.use((err, req, res, next) => {
if (!err) {
return next();
}
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
if (req.needsJSONResponse) {
const resp = {
message: err.message,
error: {}
};
if (err instanceof interoperableErrors.InteroperableError) {
resp.type = err.type;
resp.data = err.data;
}
res.status(err.status || 500).json(resp);
} else {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
}
});
module.exports = app;