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

@vivareal/error-glossary

v0.0.8

Published

Handler to format graphql errors into beautifully crafted user messages

Downloads

328

Readme

Error Glossary 📖

A library to centralize and craft the messages for all errors coming from Gandalf

Problem to Solve

We have several client applications using Gandalf as a BFF and a lot of the logic related services and data fetching are replicated from project to project.

This library is an attempt to centralize and reuse the same error formating logic for all the Gandalf related projects. The benefits of doing this include:

  • 📒 Single source of truth for error messages on the client
  • 📓 Serves as documentation for all errors related to frontend
  • 🔖 Possibility to reuse messages for common errors
  • ✏️ Ability to define custom errors according to the project
  • ✂️ No more copy paste error handling logic when creating a new project
  • 📚 Step forward to centralize and standardize errors as an organization

The last item is specially important as this is a long time proposal discussed here, with more infos here and more recently here

Installation

npm i --save @vivareal/error-glossary

# or

yarn add @vivareal/error-glossary

Usage

Import and instantiate the ErrorGlossary class into the file that manages graphql requests and error handling

Usually this is the lib/errors.js but if your use case is simple enough, you can plug the ErrorGlossary directly into the service.js file

import ErrorGlossary from '@vivareal/error-glossary'

const errorGlossary = new ErrorGlossary(app, options)

try {
  service.graphql(...)
} catch (error) {
  throw errorGlossary.getError(error);
}

Important to notice the getError method expects an object with a property graphQLErrors, which would be an array. Basically is what Apollo GraphQL return to the client. Errors that aren't GraphQL errors should not be passed here (Network Errors for instance), so please verify the error object before passing to the getError method.

Arguments

  • app (string): the name of your application. Each application should have it's own glossary with formatted messages, as well as reuse common messages
app = 'CANALPRO' | 'OWNERS' | 'BACKOFFICE';

More applications can be added later

  • options (object): currently the options object has only one property, defaultError. It will be used as a fallback in case the glossary don't find a given error from the backend
const options = {
  defaultError: {
    code: 'G0001',
    message: 'Default Error',
    statusCode: 400,
    path: ['*'],
  }
}

Methods

  • getError: receives the graphql error object and returns the mapped error object
/**
 * Get a formatted error from the glossary, else gets the default Error
 * @param {Object} error - Graphql Error object directly from Apollo Client
 * @param {Array.<Object>} error.graphQLErrors - Array of objects with error properties
 * @param {string} error.graphQLErrors[].code - unique code from gandalf glossary
 * @param {number} error.graphQLErrors[].statusCode - error statusCode
 * @param {Array.<String>} error.graphQLErrors[].path - error path from query or mutation
 * @param {string} error.graphQLErrors[].message - error message from gandalf glossary
 * @param {Array.<Object>} error.graphQLErrors[].locations - error line locations
 * @returns {Object} - with { code, message, statusCode, path }
*/
getError(error) {}
  • isAuthError: check if the given error is an authentication error - 401
/**
 * Check if error is an authentication error and returns a boolean
 * @param {Object} error - formatted error object
 * @param {string} error.message - error message
 * @param {string} error.code - error code
 * @param {number} error.statusCode - error statusCode
 * @param {Array.<String>} error.path - error path from query or mutation
 * @returns {Boolean} 
*/
isAuthError(error) {}
  • getErrorByCode(code): it returns the glossary error which matches the given code

should only be used when the APIs returns a reliable code the frontend can trust. For now, we better keep using the getError() method

/**
 * Get the error object based on the gandalf error code
 * @param {string} code - error unique code
 * @returns {Object} - with { code, message, statusCode, path }
*/
getErrorByCode(code)