Create javascript errors in a smart & simple way.
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"
}
};
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
const { ILoveYou, Hello } = errors;
throw new ILoveYou("zh-CN");
throw new Hello("en-US");
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);
MIT © calidion