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

app-errors

v0.0.3

Published

Set of reusable errors for apps / apis.

Downloads

8

Readme

app-errors

Set of reusable errors for apps / apis.

Installation

yarn add app-errors

Usage

import {
  AppError,
  AuthenticationError,
  AuhtorizationError,
  InternalError,
  NotImplementedError,
  ValidationError,
  wrapError
} from 'app-errors'

// AuthenticationError
try {
  parseJwt(token)
} catch (err) {
  throw new AuthenticationError({ message: 'Invalid token', err })
}

// AuthorizationError
if (user.role !== 'admin') {
  throw new AuthorizationError({ message: 'Only admin can access this resource' })
}

// InternalError
if (somethingGoneWrong) {
  throw new InternalError({ message: 'Something gone wrong' })
}

// NotImplementedError
app.post('/new-feature', (req, res, next) => {
  next(new NotImplementedError({ message: 'This feature is not implemented yet' }))
})

// ValidationError #1
const err = new ValidationError()
if (!user.email.match(/@gmail\.com$/)) {
  err.addDetail({ path: 'email', message: 'Must end with "@gmail.com"' })
}
if (!user.password) {
  err.addDetail({ path: 'password', message: 'Password is required' })
}
if (err.hasDetails()) {
  throw err
}

// ValidationError #2
const errors = [
  { path: 'email', message: 'Must end with "@gmail.com"' },
  { path: 'password', message: 'Password is required' }
]
if (errors.length) {
  throw new ValidationError({ details: errors })
}

// wrapError
try {
  someCode()
} catch (err) {
  err = wrapError(err)
  console.log(err instanceof AppError) // true
}

Options

All errors extend AppError.
AppError (as well as other error classes) accepts following options in constructor:

| option | description |------------|----------- | message | Error message. Defaults to message specific to error class (see table below). | err | Original error. | type | Type of error. Defaults to type specific to error class (see table below). | severity | Severity of error. Defaults to severity specific to error class (see table below). | statusCode | HTTP status code. Defaults status code specific to error class (see table below). | data | Custom error data. Defaults to null. | details | Error details. Defaults to null

Default option values for each error class:

| | message | err | type | severity | statusCode | data | details |---------------------|----------------------|------|----------------|----------|------------|------|-------- | AppError | App error | null | internal | error | 500 | null | null | AuthenticationError | Authentication error | null | authentication | warning | 401 | null | null | AuhtorizationError | Authorization error | null | authorization | warning | 403 | null | null | InternalError | Internal error | null | internal | error | 500 | null | null | NotImplementedError | NotImplemented error | null | internal | error | 503 | null | null | ValidationError | Validation error | null | validation | warning | 400 | null | null

Methods

AppError#getOriginalError()

Return original error.

AppError#getType()

Return error type.

AppError#getSeverity()

Return error severity.

AppError#getStatusCode()

Return error HTTP status code.

AppError#getData()

Return error custom data.

AppError#getDetails()

Return error details.

AppError#addDetail({ path: String, message: String })

Add error detail.

AppError#addDetails(Array<{ path: String, message: String }>)

Add many error details at once.

AppError#hasDetails()

Return true if error has at least one detail.

AppError#toJSON()

Get JSON representation of error. Example:

console.log(new AuthenticationError('hello').toJSON())
/*
{
  type: 'authentication',
  severity: 'warning',
  message: 'hello',
  details: [{ path: 'email', message: 'Invalid email' }] // defaults to null if no details were added
}
*/

Note that err.data is not returned in JSON representation of error.

wrapError(err: Error) => err: AppError

wrapError converts error to AppError.
If err passed as argument is already instance of AppError then it returns passed err.
If err passed as argument is not instance of AppError it is wrapped in InternalError.

Example:

const originalErr = new Error('test')
const err = wrapError(originalErr)
console.log(err instanceof AppError) // true
console.log(err instanceof InternalError) // true
console.log(err.message) // 'Internal error'
console.log(err.getOriginalError() instanceof originalErr) // true

This method is useful when globally handling application errors:

app.use(async (ctx, next) => {
  try {
    await next()
  } catch (err) {
    err = wrapError(err)
    logError(err)
    ctx.status = err.getStatusCode()
    ctx.body = err.toJSON() // remember that ValidationError#toJSON may return array!
  }
})

function logError (err) {
  if (err.getSeverity() === 'error') {
    console.error(err.getOriginalError().stack)
  }
}