Skip to content

Commit

Permalink
feat(api): add error types & ctor fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 21, 2018
1 parent d93940a commit 4d3785f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/api/src/decorators/deprecated.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { illegalArgs } from "../error";

/**
* Method property decorator factory. Augments original method with
* deprecation message (via console), shown when method is invoked.
Expand All @@ -11,7 +13,7 @@ export function deprecated(msg?: string, log = console.log): MethodDecorator {
const signature = `${target.constructor.name}#${prop}`;
const fn = descriptor.value;
if (typeof fn !== "function") {
throw new Error(`${signature} is not a function`);
illegalArgs(`${signature} is not a function`);
}
descriptor.value = function () {
log(`DEPRECATED ${signature}: ${msg || "will be removed soon"}`);
Expand Down
39 changes: 39 additions & 0 deletions packages/api/src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export class IllegalArityError extends Error {
constructor(n: number) {
super(`illegal arity: ${n}`);
}
}

export class IllegalArgumentError extends Error {
constructor(msg?: any) {
super("illegal argument(s)" + (msg !== undefined ? ": " + msg : ""));
}
}

export class IllegalStateError extends Error {
constructor(msg?: any) {
super("illegal state" + (msg !== undefined ? ": " + msg : ""));
}
}

export class UnsupportedOperationError extends Error {
constructor(msg?: any) {
super("unsupported operation" + (msg !== undefined ? ": " + msg : ""));
}
}

export function illegalArity(n) {
throw new IllegalArityError(n);
}

export function illegalArgs(msg?: any) {
throw new IllegalArgumentError(msg);
}

export function illegalState(msg?: any) {
throw new IllegalArgumentError(msg);
}

export function unsupported(msg?: any) {
throw new UnsupportedOperationError(msg);
}
1 change: 1 addition & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as mixins from "./mixins";

export * from "./api";
export * from "./compare";
export * from "./error";
export * from "./equiv";

export {
Expand Down

0 comments on commit 4d3785f

Please sign in to comment.