Error boundaries

I am using typescript and so never usually have the situation where a field is accessed on an undefined object. However, I recently had the situation where it was and my user was faced with a blank white screen. How can I catch these errors with an error boundary? Should I be applying the error boundary on the entire app? Never really used it before and it seems to be a legacy API. Is this how peopel still handle it?
5 Replies
Alky
Alky10mo ago
Does this object comes from an API? If you not using ! as an assert operator through your code, the bottleneck usually is the API returning some weird stuff and the error handling doesn't detect.
jairrard
jairrard10mo ago
Yes it comes from an API
Alky
Alky10mo ago
So maybe the problem is the error handling on the API or in the frontend. If the API is supposed to return undefined in a given case, your frontend should handle it. If not, you API need to improve and return something else when undefined appears
jairrard
jairrard10mo ago
Agreed. But in the case where there is a type mismatch, is there any way to handle it in a bit more user-friendly way where they don't just see a white screen?
Alky
Alky10mo ago
Keeping the context of your question, you can cast the type of the API response to be OptimalResponse | undefined. This way, typescript will make sure that you handle the case where the object is undefined. Regardless of your API, i would say that you should always handle the case when the response fail and you receive an undefined. Since it's network, it can fail anytime. Does that answer it?
Want results from more Discord servers?
Add your server