Dissect Error
Parse some js error and extract a sort of usefull info like:
- type (
SyntaxError
,ReferenceError
, etc) - message
- error site
- line
- column
- code that threw the error
- file
- stack trace (parsed as array)
Works with runtime errors, syntax errors, node VM eval errors and plain eval errors
And have zero dependencies!
How to use?
1. Install it
npm install -S dissect-error
2. Import it
const dissect = ;
3. Use it
try // throw some error catch err const info = ; console;
Properties
The dissect function return an object with the following properties:
property | description |
---|---|
.type | JS error types like: Error, SyntaxError, MyCustomError, etc |
.message | The message from the error |
.site | What threw the error, Eg. Object.eval |
.line | Line # where the error happen |
.column | Column # where the error happen |
.stack | Array of frames from the stack trace, each frame is an object (see below) |
.expression | Expression which threw the error. Eg: const 2 = 1; |
.file | The file which threw the error. Eg: /app/project/index.js |
The .stack
returns an array of stack frames wich have the folowing properties
property | description |
---|---|
.site | What thre the error. Eg. Object.eval |
.file | The file which threw the error |
.line | Line # where the error happen |
.column | Column # where the error happen |
Use cases
Plain runtime errors
try await ; catch err const parsed = ; // result parsedline; // 2 parsedexpression; // await IO(); parsedsite; // IO;
Node VM errors
Node VM errors are a bit different as they are parsed relative to the code evaluated by the VM, instead of the context of the VM invokation.
const vm = ;const dissect = ; const code = `module.exports = ( => };`; const ctx = module require console global process ;vm; try vm; catch err const parsed = ; // result parsedtype; // SyntaxError parsedmessage; // Unexpected token => parsedline; // 2 (relative to the "code" parsed by the node VM) parsedexpression; // 'module.exports = ( => };