-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add Weakguard to decoders library #521
Comments
Yeah, great idea! It will need some good documentation, and configurability, so it may take a bit to do it well. This is an area where you can shoot yourself in the foot. I'm thinking of extending the current Guard API so you can provide an optional callback (called whenever a guard would throw an error), which would allow you to both log/inspect, and handle the error in your preferred way, and opt-in to continuing or keep-failing. const verify: Guard<Something> = guard(myDecoder, { onError: myCustomCallback }); Since all projects have different preferences, I don't think I can make it more specific than this generic callback structure. The callback would receive:
It would have to return:
You could then use it to build your own project-specific "weak guard": function weakGuard<T>(decoder: Decoder<T>): Guard<T> {
return guard(decoder, {
onError: (error: Error, originalBlob: mixed): T => {
if (process.env.NODE_ENV === 'production') {
// Report the error to Sentry
sentry.withScope((scope) => {
scope.setExtra('blob', originalBlob);
sentry.captureException(error);
});
// But let's allow unexpected inputs through, sacrificing correctness
// $FlowFixMe - deliberate force-cast here
return ((originalBlob: any): T);
} else {
// Keep failing in development mode
throw error;
}
},
});
} All of the logic inside of the |
That looks great 💯 |
We have detected this issue has not had any activity during the last 60 days. That could mean this issue is no longer relevant and/or nobody has found the necessary time to address the issue. We are trying to keep the list of open issues limited to those issues that are relevant to the majority and to close the ones that have become 'stale' (inactive). If no further activity is detected within the next 14 days, the issue will be closed automatically. |
When adding decoders to an already functioning production environment, many times it is helpful to use a
weakGuard
temporarily. Rather than throwing an exception it wouldconsole.error
the message (or maybe an option for a custom action?). This way the code can continue to function like it has, but the type can be refined as more variances come in. Eventually the weakGuard can be replaced withguard
once one is confident the type is correct.The text was updated successfully, but these errors were encountered: