0

Our current situation is as described, we have a huge angular application where a lot off API calls are fired in various components with error-handling so error will be displayed to the user with a toast-message. In our HttpService we are doing something like this:


lorem.pipe(
 ...
 catchError(this.formatError)
);
...

formatError(error) {
   return throwError(error.error);
}

error.error contains a JSON error-message like { success: false, code: 'XY' }

In our components we are using the success code to display an appropriate error message.

Now the problem: We are using Sentry.io to capture client exceptions using the ErrorHandler functionality of angular, but it seems like the handleError function excepts a proper Object of type Event and therefore we are getting a lot of falsey error message transmitted.

My question is, is there any possibility to map the error result after it has been thrown, so that the handleError function of the ErrorHandler receives a valid Event but the error section in the subscribe of the observable inside the components will still get the JSON-Object as error, so we don't have to rewrite every error-handling and still get an Event for the ErrorHandler?

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
  • you're looking for `throwError(error)` – Z. Bagley Feb 25 '20 at 18:01
  • Sry... My fault... Thats not the problem see my update in formatError, if I throwError with error.error the ErrorHandler does not receive an error event, if I throwError with just error, I have to rewrite all error code handling in my subscriptions,... That's my problem – Nickolaus Feb 25 '20 at 18:20

1 Answers1

0

Sentry.io has specific handlers: the ErrorHandler only handles Error types. If you want to ensure it's an event you will need to ensure the type of the item you throw is an Error. In your case, you will need to ensure you throwError(new Error(error)).

More preferably, you will instead create a custom class for your error similar to this format: enter image description here

Z. Bagley
  • 8,942
  • 1
  • 40
  • 52
  • using this approach I would still have to rewrite all the error-handling for the observables, because `catch` prevents the error to be thrown at all... or do I get something wrong here? – Nickolaus Feb 25 '20 at 18:43
  • @Nickolaus You can catch errors and throw new ones: https://stackoverflow.com/questions/50295564/how-to-propagate-errors-through-catcherror-properly Catch, update, throw new error. If you are using `handleError(myHandle)` to display it, you just need to update to `throwError(new CustomError(error))` inside of **myHandle(error)** function and it should show in your user side notification then pass the new format on to Sentry, your global handler. – Z. Bagley Feb 25 '20 at 18:47
  • Also want to note that the only things you should need to change is your custom handler, and add the new throwError(...). You shouldn't need to change all your code if they all handleError() the same way and use the same function. – Z. Bagley Feb 25 '20 at 18:49
  • mhhh... I will give it a try and let you know... but wouldn't the global error handler receive both errors the one with the valid error event and the formatted message/cutom-error? – Nickolaus Feb 25 '20 at 18:52
  • `catchError(...)` catches the error and prevents it from continuing down the pipeline, `throwError(...)` creates a new error and passes it through the pipeline. – Z. Bagley Feb 25 '20 at 19:02
  • hey, today I discussed your solution with a colleague of mine, and I am pretty sure we are talk at cross purposes here...I am sry but your suggestion won't work in our setup without rewriting the error messaging for the user – Nickolaus Feb 26 '20 at 16:40