errorable
TypeScript icon, indicating that this package has built-in type declarations

0.6.0 • Public • Published

Errorable

NPM version Build Status Dependency Status Coverage percentage

Create javascript errors in a smart & simple way.

Usage

Defines an IErrorOptions object

JSON files are totally ok for ease of maintance.

import { IErrorOptions } from "errorable";
 
const json: IErrorOptions = {
  Hello: {
    code: 100,
    messages: {}
  },
  I: {
    Love: {
      You: {
        code: 1,
        messages: {
          "en-US": "I Love U!",
          "zh-CN": "我爱你!"
        }
      }
    }
  },
  Me: {
    alias: "I"
  }
};

Generate errors

import { Generator } from "errorable";
 
const errors: any = Generator.generate(json);
 
// Got three errors:
// errors.ILoveYou
// errors.MeLoveYou
// errors.Hello

or

import { Generator } from "errorable";
const errors: any = Generator.generate(json, true);
 
// Got three errors:
// errors.I_LOVE_YOU
// errors.ME_LOVE_YOU
// errors.HELLO

Create and throw localized errors

const { ILoveYou, Hello } = errors;
throw new ILoveYou("zh-CN");
throw new Hello("en-US");

Load from or save to files

const jsonFilename = path.resolve(__dirname, "./tmp.json");
 
// Saving json to files
Generator.save(jsonFilename, json);
 
// Loading json from files
const newJson = Generator.load(jsonFilename);
 
// json equals newJson
expect(json).toEqual(newJson);

License

MIT © calidion

Dependencies (0)

    Dev Dependencies (11)

    Package Sidebar

    Install

    npm i errorable

    Weekly Downloads

    3

    Version

    0.6.0

    License

    MIT

    Unpacked Size

    20 kB

    Total Files

    11

    Last publish

    Collaborators

    • calidion
    • yanceywang