1.0.4 • Published 5 years ago
ym-error-codes v1.0.4
ym-error-codes v1.0.5
REST API Error Messages Generator - Module to automate and standardize error messages on REST API based services.
All codes are copy from this Wiki HTTP Status Codes
Installation
$ npm i ym-error-codes --save
Implementation
By default, ym-error-codes
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-codes";
//Instantiation, english by default
const Error = new ErrorGenerator()
//Instantiation with Burmese as default language
const Error = new ErrorGenerator('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
{
"status": 200,
"name": "Ok",
"message": "Request has successfully been completed."
}
*/
Using Custom Error Message
You can change the error message with custom message.
Error.getCustomMessage(200, 'Your custom message');
/* will return
{
"status": 200,
"name": "Ok",
"message": "Your custom message"
}
*/
Adding New Error
You can add your custom error codes
Error.setCustomErrors({
"1": {
"status": 1,
"name": "New Error",
"message": {
"en": "Your new error message",
"my": "သီဟိုဠ်မှ ဉာဏ်ကြီးရှင်သည် အာယုဝဍ္ဎနဆေးညွှန်းစာကို ဇလွန်ဈေးဘေး ဗာဒံပင်ထက် အဓိဋ္ဌာန်လျက် ဂဃနဏဖတ်ခဲ့သည်။"
}
},
"2": {
"status": 2,
"name": "New Error 2",
"message": {
"en": "Your new error message 2",
"my": "သီဟိုဠ်မှ ဉာဏ်ကြီးရှင်သည် အာယုဝဍ္ဎနဆေးညွှန်းစာကို ဇလွန်ဈေးဘေး ဗာဒံပင်ထက် အဓိဋ္ဌာန်လျက် ဂဃနဏဖတ်ခဲ့သည်။"
}
},
});
Error.getMessage(1);
/* will return
{
"status": 1,
"name": "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.