Wix Error Boundaries
In many cases, when errors thrown in our code, we want to handle them properly. Sometimes we want to apply an algorithms to resolve the error, in other cases we would like to report them to an external system or maybe just throw again and bubble them up.
Error handling become even more complicated when your app is living under another app context and those two apps may communicate. In this case, errors might be thrown on both sides and then you might be mislead by handling not-yours errors.
Bounding errors by scope or context of the actual running code is crucial when working on mixed app content environment. By using
wix-error-boundaries, applying such methodology will be easy for you to bound your errors the way you see fit and handle them according to the errors' scope and the way you see fit.
npm install -save wix-error-boundaries
errorBoundaries function accept the following parameters:
Arrayof strings which represents the scopes you'd like to bound between
Functionwith the signature
(error, scope), where
erroris the thrown
scopeis the scope the error happened
For the following example, let's assume got an API with functions called
Now, let's define some scopes:
const MY_SCOPE = 'myCodeZone'const OTHER_SCOPE = 'otherCodeZone'
and initialize the error boundaries with our
const myCodeZone otherCodeZone =
Pay attention that the returned object of
errorBoundaries is actually containing the scopes as keys. Each key value is a wrapping function that will be bound to that certain scope.
Next, to use those wrappers you can do the following
const scopedFoo =// safely executing 'foo' while its scoped
foo throws an exception, it will be handled in
errorHandler according to the error scope!