ym-error-code

1.0.0 • Public • Published

ym-error-code v1.0.0

REST API Error Messages Generator - Module to automate and standardize error messages on REST API based services.

Installation

$ npm i ym-error-code --save

Implementation

Adding New Error

You can add your custom error codes on the class instantiation.

//Import
import ErrorGenerator from "ym-error-code";
 
//Instantiation, with new error codes
const Error = new ErrorGenerator({
    "1": {
    "code": 1,
    "details": "New Error",
    "message": {
      "en": "Your new error message",
      "my": "သီဟိုဠ်မှ ဉာဏ်ကြီးရှင်သည် အာယုဝဍ္ဎနဆေးညွှန်းစာကို ဇလွန်ဈေးဘေး ဗာဒံပင်ထက် အဓိဋ္ဌာန်လျက် ဂဃနဏဖတ်ခဲ့သည်။"
    }
  },
    "2": {
    "code": 2,
    "details": "New Error 2",
    "message": {
      "en": "Your new error message 2",
      "my": "သီဟိုဠ်မှ ဉာဏ်ကြီးရှင်သည် အာယုဝဍ္ဎနဆေးညွှန်းစာကို ဇလွန်ဈေးဘေး ဗာဒံပင်ထက် အဓိဋ္ဌာန်လျက် ဂဃနဏဖတ်ခဲ့သည်။"
    }
  },
});
 
Error.getMessage(1);
 
/* will return
    {
        "code": 1,
        "details": "New Error",
        "message": "Your new error message"
    }
*/
 

Warning: If your new error code are the same with the existing error code, the existing error code will be overridden.

Internationalization

By default, ym-error-code message comes with English (United States) locale strings. If you need other locales, pass the key and the string value on the class instantiation.

//Import
import ErrorGenerator from "ym-error-code";
 
//Instantiation, english by default
const Error =  new ErrorGenerator();
 
//Instantiation with Burmese as default language
const Error =  new ErrorGenerator(null, 'my');
 
//pass the locale string
Error.setLanguage('my');
 

Note: Currently, the ​​available languages for displaying error messages are English ('en') and Myanmar ('my').

Basic usage

 
Error.getMessage(200);
 
/* will return
    {
        "code": 200,
        "details": "Ok",
        "message": "Request has successfully been completed."
    }
*/

Using Custom Error Message

You can change the error message with your custom message.

 
Error.getCustomMessage(200, 'Your custom message');
 
/* will return
    {
        "code": 200,
        "details": "Ok",
        "message": "Your custom message"
    }
*/

Package Sidebar

Install

npm i ym-error-code

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

42.9 kB

Total Files

7

Last publish

Collaborators

  • kyawzintun