Skip to main content
Seasoned ⭐️⭐️⭐️
January 28, 2022
Solved

Global error-handling

  • January 28, 2022
  • 7 replies
  • 159 views

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

    Best answer by henning.kvinnesland

    Did exactly what I needed. Thank you :)

    7 replies

    Practitioner ⭐️⭐️⭐️
    February 2, 2022

    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

    Seasoned ⭐️⭐️⭐️
    February 2, 2022

    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?

    Practitioner ⭐️⭐️⭐️
    February 22, 2022

    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 

    Seasoned ⭐️⭐️⭐️
    February 22, 2022

    Awesome! I’ll try it out!

    Practitioner ⭐️⭐️⭐️
    March 3, 2022

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

    Seasoned ⭐️⭐️⭐️
    March 4, 2022

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

    Seasoned ⭐️⭐️⭐️
    March 16, 2022

    Did exactly what I needed. Thank you :)