peerigon / erroz

Streamlined errors with descriptive error messages through metadata and error codes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Helper for wrapped errors

meaku opened this issue · comments

Sometime it's useful to catch some known errors and convert them to erroz-errors. Unfortunately those errorz-erros have the stacktrace of erroz-creation. It might be helpful to add an option to pass an original error when creating an erroz-error instance to append the original error.

Something like:

var ParseError = erroz({
  statusCode: 400,
  status: "fail",
  name: "ParseError",
  code: "parse-error",
  template "Could not parse %what"
});

try{ 
  JSON.parse("xaz--");
}
catch(err) {
   //pass original err as second argument (optional)
   errozErr = new ParseError({ what: "ever" }, err); 

   console.log(errozErr.stack) //would equal err.stack    
 }

Although it seems convenient it's also confusing as the stack is not pointing to the real "throw" . Maybe it's possible to merge the stacks or introduce a second originalStack attribute.

Mhmmm ... I'm not sure about this.

These should probably stay to different errors. The first error is an internal parse error that doesn't know the context, and the second one is the "public" error that is printed to the user. Imho it's ok that these are two distinct errors.

It's just pain in the ass if you're using a global error handler which catches all unhandled errors and swallows the actual stack. But maybe we should leave it to application logic..

In any case the application needs to catch the error before it is passed to the global error handler, so I don't see the benefit here.

It's a few lines less to write. And that's it. ™️