Comments (3)
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:
error: Error
- The Decoding Error instance (whatever error the guard throws today, literally)originalBlob: mixed
- The originalblob
value that failed to decode, for further inspection, or logging, or modification
It would have to return:
- An explicit fallback value (of type
T
)
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 onError
handler is inherently project-specific though. But I could provide this snippet as a good example for how to build something like this.
from decoders.
That looks great 💯
from decoders.
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.
If new comments are are posted and/or a solution (pull request) is submitted for review that references this issue, the issue will not be closed. Closed issues can be reopened at any time in the future. Please remember those participating in this open source project are volunteers trying to help others and creating a better collaboration environment for all. Thank you for your continued involvement and contributions!
from decoders.
Related Issues (20)
- Missing field in `inexact` doc HOT 1
- Some way to influence/adjust error messages to redact certain fields in objects? HOT 3
- Question: Type intersection composition HOT 2
- Decoder for object with specified key HOT 3
- Required types HOT 6
- Dispatch does not work when the field differentiating which decoder to use has a non-string value HOT 1
- Decoder with constants is throwing an exception. TypeError: (0 , _Result.Ok) is not a function HOT 5
- Typescript error with required properties HOT 9
- [Bug] Builtin composite decoders cannot take decoders with non-`unknown` input types HOT 8
- WeakGuard in 2.x HOT 3
- Migration help needed: .map to .transform HOT 3
- Export Annotation and Formatting function from package HOT 1
- Composing `lazy` and `either` can become slow. HOT 3
- Package requires nodejs 10 or newer, but this is not documented. HOT 2
- Decoding nested serialized JSON HOT 1
- Unwrap Error Message from Guard HOT 2
- Type is not inferred correctly from a dynamically created decoder with generics HOT 4
- Union type is not inferred correctly from either decoder HOT 1
- Ability to filter out incorrect items during array decoding HOT 6
- URL decoder does not accept URLs with / in the query part HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from decoders.