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

jm-err

v1.0.8

Published

common error defines as JSON format

Downloads

22

Readme

English | 简体中文

jm-err

common error defines as JSON format.

used for return json format info on RESTful api.

define an Err

const Err = {
    SUCCESS: {
        err: 0, // unique code, integer
        status: 500, // status, integer
        msg: 'Success', // message, string
        ... // else info
    },
    ...
}

status: Http status

if status is not defined, status = err when err is Http status value, otherwise 500.

install

npm i jm-err

usage

use Err:

const {Err} = require('jm-err')

console.log(Err.FAIL)

throw an error.

const {Err, err} = require('jm-err')

throw err(Err.FAIL)

throw err('string as param')

// define a new Err
throw err({
  err: 222,
  msg: 'main msg',
  else: 'msg else'
})

// e.data == Err.FA_NOAUTH
const e = err(Err.FA_NOAUTH)
const {
  code,
  status, 
  message,
  data
} = e

output error info:

Error: Fail
    at err (E:\jamma\core\packages\jm-err\lib\index.js:150:13)
    ...
  code: 1,
  status: 500,
  data: { err: 1, msg: 'Fail', status: 500 }
}

template message support

const {err} = require('jm-err')
const e = err('err param: ${param} paramNum: ${num}', {
  param: 'abc',
  num: 123
})
console.log(e.message)
//print 'err param: abc paramNum: 123'

// define template in Err
const ErrSample = {
  err: 1,
  status: 500,
  msg: 'err param: ${param} paramNum: ${num}'
}
const e2 = err(ErrSample, {
  param: 'abc',
  num: 123
})
console.log(e2.message)
// print 'err param: abc paramNum: 123'

i18n

const {Err:{FAIL:{msg}}, t} = require('jm-err')
console.log(msg)
// 输出 'Fail'
console.log(t(msg, 'zh-CN'))
console.log(msg)
// 输出 '失败'

defined Errs

| name | err | status | msg | | ----- |:-------------:| :-----:| :-----| |SUCCESS|0|200|Success| |FAIL|1| |Fail| |FA_SYS|2| |System Error| |FA_NETWORK|3| |Network Error| |FA_PARAMS|4| |Parameter Error| |FA_BUSY|5| |Busy| |FA_TIMEOUT|6| |Time Out| |FA_ABORT|7| |Abort| |FA_NOTREADY|8| |Not Ready| |FA_NOTEXISTS|9| |Not Exists| |FA_EXISTS|10| |Already Exists| |FA_VALIDATION|11| |Validation Error| |OK|200| |OK| |FA_BADREQUEST|400| |Bad Request| |FA_NOAUTH|401| |Unauthorized| |FA_NOPERMISSION|403| |Forbidden| |FA_NOTFOUND|404| |Not Found| |FA_INTERNALERROR|500| |Internal Server Error| |FA_UNAVAILABLE|503| |Service Unavailable|

Contributing

Any type of contribution is welcome, here are some examples of how you may contribute to this project:

  • Use jm-err in your daily work.
  • Submit issues to report bugs or ask questions.
  • Propose pull requests to improve our code.