GithubHelp home page GithubHelp logo

Unwrap Error Message from Guard about decoders HOT 2 CLOSED

nvie avatar nvie commented on May 22, 2024
Unwrap Error Message from Guard

from decoders.

Comments (2)

arb avatar arb commented on May 22, 2024 1

Hey @nvie thanks for the prompt and thorough reply! Upgrading doesn't seem like it's in the cards at the moment regrettably. I ended up doing something like this

function tryDecoderGuard<D extends d.Decoder<any>>(
  decoder: D,
  value: unknown,
): [Error, null] | [null, d.DecoderType<D>] {
  let decodedValue = null;
  let error = null;
  try {
    decodedValue = d.guard(decoder)(value);
  } catch (e: any) {
    error = e;
  }
  return [error, decodedValue];
}

So that way, we return a tuple with a decoded and typed value OR a guard error. The caller then can refine on that tuple and execute additional logic. I'm using this code in an Express server and going down the .map route would have been difficult to prevent multiple res.send from happening on a given request.

from decoders.

nvie avatar nvie commented on May 22, 2024

Note upfront: This is more complicated than it had to be, which is why I simplified it a lot in 2.0. To learn how to adopt from the old to the new APIs, see this migration guide. I would recommend it, because it also gets rid of the complexity incurred by the lemons and debrief dependencies. 🎉


To answer your question: Before 2.0, you can use use the debrief library to serialize it directly. This is what the guard internally uses too (it's already a dependency).

Here's the relevant code you could copy:

decoders/src/guard.js

Lines 29 to 35 in a396623

decoder(blob)
.mapError((annotation) => {
const err = new Error('\n' + serializer(annotation));
err.name = 'Decoding error';
return err;
})
.unwrap();

Where blob is your input.

Note that there are two ways to turn an Annotation into a textual error message:

  1. Using import { serialize } from 'debrief, then calling serialize(annotation). This will echo back the original input, and "inline" the error message in a human-readable form. Example of a possible returned multi-line string:
    {
      "foo": "bar",
             ^^^^^ Must be integer
    }
    
  2. Using import { summarize } from 'debrief, then calling summarize(annotation). This will return a list-of-strings, each containing the "path" into the object, and the error reason, without echoing back. Less human-friendly, but won't echo back any sensitive data. Example of returned array of strings:
    ['Value at key "foo": Must be integer']
    

Hope that helps!

from decoders.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.