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

@nerjs/errors

v1.2.2

Published

Custom errors

Downloads

16

Readme

Custom errors

Install

npm i @nerjs/errors

or:

yarn add @nerjs/errors

Use

HttpError

const HttpError = require('@nerjs/errors/HttpError')

const error = new HttpError(404) // HttpError: Not found

console.log(error) // { code: 404, message: 'Not Found' }

all status codes:

HttpError.STATUS_CODES

Gql Errors

Errors designed to integrate into the Apollo server

GqlError

const GqlError = require('@nerjs/errors/GqlError')

new GqlError(message: String, code: String, datails?: Object, originalError?: Error)

codes

GqlError.codes

{
    FORBIDDEN: 'FORBIDDEN',
    AUTH: 'UNAUTHORIZED',
    GRAPHQL_VALIDATION: 'GRAPHQL_VALIDATION_FAILED',
    VALIDATION: 'VALIDATION',
    INTERNAL_SERVER_ERROR: 'INTERNAL_SERVER_ERROR',
    DB: 'DB_ERROR',
    NOT_FOUND: 'NOT_FOUND_ERROR',
    UNKNOWN: 'UNKNOWN',
}

.is(err)

resolver.js:

Query = {
    test: () => throw new GqlError('message')
}

app.js

new ApolloServer({
  typeDefs,
  resolvers,
  formatError: (err) => { 
    console.log(GqlError.is(err)) // true
    return err;
  },
})

AutGqlError

@nerjs/errors/AutGqlError extends GqlError code: GqlError.codes.AUTH props: message: String

ForbiddenGqlError

@nerjs/errors/ForbiddenGqlError extends GqlError code: GqlError.codes.FORBIDDEN props: message: String

DbGqlError

@nerjs/errors/DbGqlError extends GqlError code: GqlError.codes.DB props: err: Error

NotFoundGqlError

@nerjs/errors/NotFoundGqlError extends GqlError code: GqlError.codes.NOT_FOUND props: message: String

ValidationGqlError

@nerjs/errors/ValidationGqlError extends GqlError code: GqlError.codes.VALIDATION props: message: String, details: Object prop.details: {errors: Array, map: Object}

example:

new ValidationGqlError('Validation failed', {
    errors: [
        'character limit exceeded',
        'Invalid value'
    ],
    map: {
        firstField: 'character limit exceeded',
        secondField: 'Invalid value'
    }
})

YupGqlError

@nerjs/errors/YupGqlError extends ValidationGqlError code: GqlError.codes.VALIDATION props: err: Error

converts yup ValidationError to GqlError

helpers

PromiseDbGqlError

@nerjs/errors/PromiseDbGqlError extends Promise all thrown exceptions are wrapped in DbGqlError

example:

db.js

mongoose.Promise = PromiseDbGqlError

mongoose.model('Users', new mongoose.Schema({ /* ... */ }))

resolver.js:

Query = {
    test: () => User.create({/* ... */}) // throw Error
}

app.js

new ApolloServer({
  typeDefs,
  resolvers,
  formatError: (err) => { 
    console.log(DbGqlError.is(err)) // true
    return err;
  },
})

Client Gql errors

Parsing and conversion of client errors that come from the Apollo server

Error Codes Used

ClientGqlError

const ClientGqlError = require('@nerjs/errors/ClientGqlError')

new ClientGqlError(message: String, path: Array<String> | String , extensions = {})

ClientGqlError.codes equal Server codes

ClientGqlError.parseServerGqlError()

parse server errors.

Find error in graphQLErrors

Params

|name|type|description| |:--:|:--:|:--| |err|Error| Array of errors graphQLErrors | |path| String || Array<String>| query path| |code|Strind | Error code| |defaultMessage|String|If no error is found| |strict|Boolean|A way to compare paths when searching|

recursive:

const graphQLErrors = [
  {
    "message": "Unauthorized",
    "locations": [/* ... */],
    "path": [
      "user",
      "id"
    ],
    "extensions": {
      "code": "UNAUTHORIZED",
      "exception": { /* ... */ }
    }
  }
]

ClientGqlError.parseServerGqlError(
  graphQLErrors,
  'user', // or ['user']
  'UNAUTHORIZED'
  'Default message',
  false
) // Not found. Returns new ClientGqlError(Default message)

ClientGqlError.parseServerGqlError(
  graphQLErrors,
  'user', // or ['user']
  'UNAUTHORIZED'
  null,
  false
) // Not found. Returns NULL

ClientGqlError.parseServerGqlError(
  graphQLErrors,
  ['user', 'id'],
  'UNAUTHORIZED'
  'Default message',
  false
) // Successfully Found Error. Returns new ClientGqlError(...)

ClientGqlError.parseServerGqlError(
  graphQLErrors,
  'user', // or ['user']
  'UNAUTHORIZED'
  'Default message',
  true // STRICT
) // Successfully Found Error. Returns new ClientGqlError(...)

ValidationClientGqlError

extends ClientGqlError

const ValidationClientGqlError = require('@nerjs/errors/ValidationClientGqlError')

new ValidationClientGqlError(message: String, path: Array<String> | String , extensions = {})

ValidationClientGqlError.parseServerGqlError(graphQLErrors, path)


:link: All utils