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

egg-errors

v2.3.2

Published

egg-errors provide two kinds of errors that is Error and Exception.

Downloads

141,495

Readme

egg-errors

NPM version Node.js CI Test coverage Known Vulnerabilities npm download

Errors for Egg.js

egg-errors provide two kinds of errors that is Error and Exception.

  • Exception is system error that egg will log an error and throw exception, but it will be catched by onerror plugin.
  • Error is business error that egg will transform it to response.

Install

$ npm i egg-errors --save

Usage

Create an Error

const { EggError, EggException } = require('egg-errors');
let err = new EggError('egg error');
console.log(EggError.getType(err)); // ERROR

Create an Exception

err = new EggException('egg exception');
console.log(EggException.getType(err)); // EXCEPTION

You can import an error from an normal error object

err = new Error('normal error');
console.log(EggError.getType(err)); // BUILTIN
err = EggError.from(err);
console.log(EggError.getType(err)); // ERROR

Customize Error

Error can be extendable.

const { EggBaseError } = require('egg-errors');

class CustomError extends EggBaseError {
  constructor(message) {
    super({ message, code: 'CUSTOM_CODE' });
  }
 }

or using typescript you can customize ErrorOptions.

import { EggBaseError, ErrorOptions } from 'egg-errors';

class CustomErrorOptions extends ErrorOptions {
  public data: object;
}
class CustomError extends EggBaseError<CustomErrorOptions> {
  public data: object;
  protected options: CustomErrorOptions;

  constructor(options?: CustomErrorOptions) {
    super(options);
    this.data = this.options.data;
  }
}

Recommend use message instead of options in user land that it can be easily understood by developer, see http error.

HTTP Errors

HTTP Errors is BUILTIN errors that transform 400 ~ 500 status code to error objects. HttpError extends EggBaseError providing two properties which is status and headers;

const { ForbiddenError } = require('egg-errors');
const err = new ForbiddenError('your request is forbidden');
console.log(err.status); // 403

Support short name too:

const { E403 } = require('egg-errors');
const err = new E403('your request is forbidden');
console.log(err.status); // 403

FrameworkBaseError

FrameworkBaseError is for egg framework/plugin developer to throw framework error.it can format by FrameworkErrorFormater

FrameworkBaseError extends EggBaseError providing three properties which is moduleserialNumber and errorContext

FrameworkBaseError could not be used directly, framework/plugin should extends like this

const { FrameworkBaseError } = require('egg-errors');

class EggMysqlError extends FrameworkBaseError {
  // module should be implement
  get module() {
    return 'EGG_MYSQL';
  }
}

const err = new EggMysqlError('error message', '01', { traceId: 'xxx' });
console.log(err.module); // EGG_MYSQL
console.log(err.serialNumber); // 01
console.log(err.code); // EGG_MYSQL_01
console.log(err.errorContext); // { traceId: 'xxx' }

create frameworkError with formater

use the static method .create(message: string, serialNumber: string | number, errorContext?: any) to new a frameworkError and format it convenient

const { FrameworkBaseError } = require('egg-errors');

class EggMysqlError extends FrameworkBaseError {
  // module should be implement
  get module() {
    return 'EGG_MYSQL';
  }
}

const err = EggMysqlError.create('error message', '01', { traceId: 'xxx' });
console.log(err.message); 
// =>
framework.EggMysqlError: error message [ https://eggjs.org/zh-cn/faq/EGG_MYSQL/01 ]

FrameworkErrorFormater

FrameworkErrorFormater will append a faq guide url in error message.this would be helpful when developer encountered a framework error

the faq guide url format: ${faqPrefix}/${err.module}/${err.serialNumber}, faqPrefix is https://eggjs.org/zh-cn/faq by default. can be extendable or set process.env.EGG_FRAMEWORK_ERR_FAQ_PERFIX to override it.

const { FrameworkErrorFormater } = require('egg-errors');

class CustomErrorFormatter extends FrameworkErrorFormater {
  static faqPrefix = 'http://www.custom.com/faq';
}

.format(err)

format error to message, it will not effect origin error

const { FrameworkBaseError, FrameworkErrorFormater } = require('egg-errors');

class EggMysqlError extends FrameworkBaseError {
  // module should be implement
  get module() {
    return 'EGG_MYSQL';
  }
}

const message = FrameworkErrorFormater.format(new EggMysqlError('error message', '01'));
console.log(message); 
// => message format like this
framework.EggMysqlError: error message [ https://eggjs.org/zh-cn/faq/EGG_MYSQL/01 ]
...stack
...
code: "EGG_MYSQL_01"
serialNumber: "01"
errorContext:
pid: 66568
hostname: xxx


// extends
class CustomErrorFormatter extends FrameworkErrorFormater {
  static faqPrefix = 'http://www.custom.com/faq';
}
const message = CustomErrorFormatter.format(new EggMysqlError('error message', '01'));
console.log(message); 
// =>
framework.EggMysqlError: error message [ http://www.custom.com/faq/EGG_MYSQL/01 ]
...

.formatError(err)

append faq guide url to err.message

const { FrameworkBaseError, FrameworkErrorFormater } = require('egg-errors');

class EggMysqlError extends FrameworkBaseError {
  // module should be implement
  get module() {
    return 'EGG_MYSQL';
  }
}

const err = FrameworkErrorFormater.formatError(new EggMysqlError('error message', '01'));
console.log(err.message); // error message [ https://eggjs.org/zh-cn/faq/EGG_MYSQL/01 ]

Available Errors

BaseError
|- EggBaseError
|  |- EggError
|  |- HttpError
|  |  |- NotFoundError, alias to E404
|  |  `- ...
|  |- FrameworkBaseError
|  `- CustomError
`- EggBaseException
   |- EggException
   `- CustomException

Questions & Suggestions

Please open an issue here.

License

MIT

Contributors

|popomore|mansonchor|fengmk2|beliefgp|sm2017| | :---: | :---: | :---: | :---: | :---: |

This project follows the git-contributor spec, auto updated at Tue Feb 22 2022 11:32:47 GMT+0800.