Skip to content
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

Closed
sfarthin opened this issue Jun 23, 2020 · 3 comments
Closed

Add Weakguard to decoders library #521

sfarthin opened this issue Jun 23, 2020 · 3 comments
Labels

Comments

@sfarthin
Copy link
Contributor

sfarthin commented Jun 23, 2020

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 would console.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 with guard once one is confident the type is correct.

@nvie
Copy link
Owner

nvie commented Jun 24, 2020

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 original blob 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.

@sfarthin
Copy link
Contributor Author

That looks great 💯

@github-actions
Copy link

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!

@github-actions github-actions bot added the Stale label Aug 24, 2020
@github-actions github-actions bot closed this as completed Sep 7, 2020
@sfarthin sfarthin mentioned this issue Feb 14, 2022
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants