npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@codama/errors

v1.1.0

Published

Error management for Codama

Downloads

1,422

Readme

Codama ➤ Errors

npm npm-downloads

This package defines a CodamaError class that accepts a specific error code and a context object based on that code. It enables us to catch and handle errors in a more structured way.

Installation

pnpm install @codama/errors

[!NOTE] This package is included in the main codama package. Meaning, you already have access to its content if you are installing Codama this way.

pnpm install codama

Reading error messages

In development mode

When the NODE_ENV environment variable is not set to "production", every error message will be included in the bundle. As such, you will be able to read them in plain language wherever they appear.

In production mode

On the other hand, when NODE_ENV is set to "production", error messages will be stripped from the bundle to save space. Only the error code will appear when an error is encountered. Follow the instructions in the error message to convert the error code back to the human-readable error message.

For instance, to recover the error text for the error with code 123:

npx @codama/errors decode -- 123

Catching errors

When you catch a CodamaError and assert its error code using isCodamaError(), TypeScript will refine the error's context to the type associated with that error code. You can use that context to render useful error messages, or to make context-aware decisions that help your application to recover from the error.

import { CODAMA_ERROR__UNEXPECTED_NODE_KIND, isCodamaError } from '@codama/errors';

try {
    const codama = createFromJson(jsonIdl);
} catch (e) {
    if (isCodamaError(e, CODAMA_ERROR__UNEXPECTED_NODE_KIND)) {
        const { expectedKinds, kind, node } = e.context;
        // ...
    } else if (isCodamaError(e, CODAMA_ERROR__VERSION_MISMATCH)) {
        const { codamaVersion, rootVersion } = e.context;
        // ...
    } else {
        throw e;
    }
}

Contributing

Adding a new error

To add a new error in Codama, follow these steps:

  1. Add a new exported error code constant to src/codes.ts. Find the most appropriate group for your error and ensure it is appended to the end of that group.
  2. Add that new constant to the CodamaErrorCode union in src/codes.ts.
  3. If you would like the new error to encapsulate context about the error itself define that context in src/context.ts.
  4. Add the error's message to src/messages.ts. Any context values that you defined above will be interpolated into the message wherever you write $key, where key is the index of a value in the context (eg. 'Unrecognized node `$kind`.').
  5. Publish a new version of @codama/errors using changesets — maintainers will handle this via tha changesets CI workflow.
  6. Bump the version of @codama/errors or codama in the consumer package from which the error is thrown.

Removing an error message

  • Don't remove errors.
  • Don't change the meaning of an error message.
  • Don't change or reorder error codes.
  • Don't change or remove members of an error's context.

When an older client throws an error, we want to make sure that they can always decode the error. If you make any of the changes above, old clients will, by definition, not have received your changes. This could make the errors that they throw impossible to decode going forward.