Solved

Global error-handling

  • 28 January 2022
  • 7 replies
  • 109 views

Userlevel 2

What is best practice for global error-handling in the JS-SDK?

icon

Best answer by henning.kvinnesland 16 March 2022, 20:58

View original

7 replies

Userlevel 2
Badge

Hi Henning,

There are two approaches to global error handling in the JavaScript SDK. For a React application, it is recommended to use a component around all your code and store the errors inside a context (data storage) shown in this component.  For a NodeJS application, it is recommended to use a middleware that will catch the error and highlight it.

For Cognite API related errors, we do have some documentation on that in the API reference doc (https://docs.cognite.com/api/v1/) 

I hope this helps

Regards

Omar

Userlevel 2

We have the error-boundary in place in our React-application, but it does not deal well with async-errors outside of React-code. You can in theory rethrow the error-messages and have them caught, but I couldn’t get this approach to work.

Also I would very much like not having to try-catch every single api-call we make.

I couldn’t find anything in the documentation that seemed relevant. Which part would you have me look at?

Userlevel 2
Badge

Hi Henning.

For doing global promise rejection you can use this link. This will enable you to handle all the unhandled promise rejections. When it comes to having similar functionality in the SDK, this is valuable insight as we currently do not offer such a thing.

Let me know if this helps

Kindest Regards

Omar 

Userlevel 2

Awesome! I’ll try it out!

Userlevel 2
Badge

Hi Henning. Were you able to resolve this issue? Wanted to follow up to see how it went

Userlevel 2

Haven’t had a chance to implement it yet. I’ll let you know as soon as possible :)

Userlevel 2

Did exactly what I needed. Thank you :)

Reply