Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap requests to log JavaScript errors #1292

Open
roxeteer opened this issue Jan 8, 2016 · 9 comments
Open

Wrap requests to log JavaScript errors #1292

roxeteer opened this issue Jan 8, 2016 · 9 comments
Labels

Comments

@roxeteer
Copy link

roxeteer commented Jan 8, 2016

Is it possible to somehow wrap all outbound requests and run custom code in case of a JavaScript error? Is there an internal method in Restangular handling HTTP requests that I could replace with my own implementation?

The case is that our application reports JavaScript errors to Sentry. If the API returns a response that Restangular is not able to handle (basically when Error: Response for getList SHOULD be an array and not an object or something else happens), we would like to send the API request URL to Sentry as well.

I can't do this with addRequestInterceptor() because then I would need to set the URL into global context and it would be sent with all the errors that occur until the context is reset. Also, if I send more than one request at the same time, they override each other's context.

I can't do this with setErrorInterceptor() either, because it's never called if Restangular throws an error.

@roxeteer
Copy link
Author

roxeteer commented Jan 8, 2016

From what I gathered, I should wrap fetchFunction() with a try-catch to achieve this, but I'm not able to get further than that.

@daviesgeek
Copy link
Collaborator

Could you provide some more information on exactly what you're trying to do and exactly why addRequestInterceptor won't work?

@roxeteer
Copy link
Author

It's been quite some time since I worked on this, but if I remember correctly, addRequestInterceptor doesn't work because the error hasn't yet occurred when the interceptor is called. Since requests are asynchronous, there can be multiple requests happening at the same time and it would be quite complicated to track which of these requests is the one throwing an error.

What I want to do is to run my own code when server returns something with 200 OK but Restangular itself throws an error.

I guess another solution would be that there's an interceptor or a callback that is called when there's a JavaScript error.

@daviesgeek
Copy link
Collaborator

Okay. And to clarify, setErrorInterceptor, doesn't work because it doesn't actually catch errors thrown by the Restangular library; just request that error out, correct?

@roxeteer
Copy link
Author

Yes, exactly.

@roxeteer
Copy link
Author

Of course another option is that the exceptions thrown by Restangular contain the URL in the message.

@daviesgeek
Copy link
Collaborator

Hmmm okay. And you need the URL to correctly send it to Sentry, correct?

@roxeteer
Copy link
Author

I need the URL to see from the Sentry report where the error actually came from. I can see the line of code, but it would be beneficial to see which REST endpoint was called and with what parameters.

@daviesgeek
Copy link
Collaborator

Ah okay. I believe you are correct that you should wrap the fetchFunction. Try adding a catch onto the promise here and see if that works correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants