Skip to content

Commit

Permalink
feat: 🎸 improve error message display
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Oct 25, 2023
1 parent f8fd402 commit 66ea68a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 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
19 changes: 3 additions & 16 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import writeTo from 'stream-write';
import globalModules from 'global-modules';
import { resolve } from 'path';
import LRU from 'lru-cache';
import _ from 'lodash';
import Engine from './engine';
import Script, { parseCommand } from './script';
import File from './file';
Expand Down Expand Up @@ -113,25 +114,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
2 changes: 1 addition & 1 deletion packages/core/src/server/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const errorHandler = (request, response) => (error, code = 400) => {
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]);
response.write(JSON.stringify(error));
}
response.end();
};
Expand Down

0 comments on commit 66ea68a

Please sign in to comment.