Package Exports
- @snomiao/die
Readme
Die
Just like PHP's DIE('REASON'), Simple function while doing functional dev
Usage
import DIE from '@snomiao/die'
const token = process.env.TOKEN ?? DIE("Missing Token")
console.log(token)
Reference
spec
import DIE from ".";
it("lives", () => {
const token = "123" ?? DIE("Missing Token");
console.log(token);
expect(token).toEqual("123");
});
it("dies", () => {
let err: any;
try {
const token = process.env.TOKEN ?? DIE("Missing Token");
console.log(token);
} catch (e) {
err = e;
}
expect(err).toEqual("Missing Token");
});
it("dies with error", () => {
let err: any;
try {
const token = process.env.TOKEN ?? DIE(new Error("Missing Token"));
console.log(token);
} catch (e) {
err = e;
}
expect(err.message).toEqual("Missing Token");
});
Impl
export function DIE(reason?: string | Error): never {
if (typeof reason === "string") {
const err = new Error(reason);
throw err.stack;
}
throw reason;
}