Angular5 Http error - Cannot parse given error object
toonborgers opened this issue · 4 comments
I'm creating an Angular5 app with Typescript and was trying to add stacktrace-js for proper error handling.
When I was testing my setup using a failing http request (404 response), I get an error from stacktrace-js that the error can't be parsed.
Expected Behavior
The error can be parsed correctly.
Current Behavior
The error can't be parsed.
Steps to Reproduce (for bugs)
Set up an environment as in following gist and start the dev server. When the component does the request and it fails, the error handler gets triggered and the described behaviour occurs.
Context
I was trying to setup error handling for an angular app
Your Environment
- stacktrace.js version: 2.0.0 (typings version: 0.0.32)
- Browser Name and version: Chrome 63.0.3239.132
- Operating System and version (desktop or mobile): macOS Sierra 10.12.6
- Angular CLI version: 1.6.3
I am seeing this as well. Interested in the proper way to workaround this or handle it.
For now, I'm just not sending these Errors to stacktrace.js:
// Don't try to send stacktraces of HTTP errors as they are already logged on the server
// and they cause stacktrace-js to throw an exception.
if (!error.url && !error.headers) {
// get the stack trace
StackTrace.fromError(error).then(stackframes => {
...
}
}
With ExtJS i had the same problem.
This should be because in chrome user defined exceptions dont have a stacktrace by default. As there is nothing to parse stacktrace.js
throws an error. To get a stacktrace in user definedexceptions you have to use the V8 STacktrace API; see: Error.captureStackTrace().
Example with standard javascript:
function MyError(message) {
this.message = message;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, CustomError);
} else {
this.stack = (new Error).stack;
}
}
MyError.prototype = Object.create(Error.prototype);
MyError.prototype.name = "MyError";
For ExtJS i just overwritten the constructor of the default exception.
@JoeScylla not sure about this, but according to this new Error().stack
should be always undefined for a couple of browsers.