Skip to content

Commit

Permalink
Merge pull request #380 from Inist-CNRS/improve-error-message
Browse files Browse the repository at this point in the history
feat: 🎸 improve error message display
  • Loading branch information
touv authored Oct 25, 2023
2 parents f8fd402 + 4ad1789 commit 5300452
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ function createErrorWith(error, index, funcName, chunk) {
const err = Error(msg);
err.sourceError = error;
err.sourceChunk = JSON.stringify(chunk);
err.toJSON = () => ({
type: error.type || 'Standard error',
scope: error.scope || 'code',
date: error.date || new Date(),
message: msg.split('\n').shift(),
func: funcName,
index,
chunk,
});
Error.captureStackTrace(err, createErrorWith);
debug('ezs')('Caught an', err);
return err;
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export default class Feed {
if (something === null) {
this.seal();
} else if (something !== undefined) {
if (something instanceof Error) {
something.type = 'Data corruption error';
something.scope = 'data';
something.date = new Date();
}
this.push(something);
}
}
Expand Down Expand Up @@ -67,6 +72,9 @@ export default class Feed {
}

stop(withError) {
withError.type = 'Fatal run-time error';
withError.scope = 'statements';
withError.date = new Date();
this.error(withError);
this.seal();
}
Expand Down
18 changes: 2 additions & 16 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,11 @@ ezs.createPipeline = (input, commands, trap) => {
}
return output
.pipe(ezs.catch((e) => {
trap.write({
type: 'Run-time warning',
scope: 'data',
message: e.message.split('\n').shift(),
messageFull: e.message,
sourceError: e.sourceError,
sourceChunk: e.sourceChunk,
});
trap.write(e.toJSON()); // see engine.js createErrorWith
return false; // do not catch the error
}))
.once('error', (e) => {
trap.write({
type: 'Fatal run-time error',
scope: 'statements',
message: e.message.split('\n').shift(),
messageFull: e.message,
sourceError: e.sourceError,
sourceChunk: e.sourceChunk,
});
trap.write(e.toJSON()); // see engine.js createErrorWith
trap.end();
})
.once('end', () => {
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/server/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { httpRequestErrorTotal } from './metrics';
const errorHandler = (request, response) => (error, code = 400) => {
debug('ezs')('Server has caught an error', error);
httpRequestErrorTotal.labels(request.pathName).inc();
if (!response.headersSent) {
response.setHeader('Content-Type', 'text/plain');
response.setHeader('Content-Disposition', 'inline');
response.writeHead(code, { 'X-Error': Parameter.encode(error.toString()) });
response.write(error.toString().split('\n', 1)[0]);
if (response.headersSent) {
return response.end();
}
response.end();
const bodyResponse = JSON.stringify(error);
response.setHeader('Content-Type', 'application/json');
response.setHeader('Content-Length', bodyResponse.length);
response.setHeader('Content-Disposition', 'inline');
response.writeHead(code, { 'X-Error': Parameter.encode(error.toString()) });
return response.end(bodyResponse);
};

export default errorHandler;

0 comments on commit 5300452

Please sign in to comment.