Skip to content

calidion/errorable

Repository files navigation

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