Skip to content

Commit

Permalink
feat(utils/errors): KpoError takes data as third argument; ensures is…
Browse files Browse the repository at this point in the history
…KpoError returns a boolean; imp
  • Loading branch information
rafamel committed May 14, 2019
1 parent 1045ea5 commit b75f82c
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { IOfType } from '~/types';

export class KpoError<A> extends Error {
export class KpoError<S> extends Error {
public static isKpoError = true;
/**
* An optional `source` -should reference the object that originated the `Errorish`.
*/
public source: any;
/**
* A `data` object.
*/
public data: IOfType<any>;
public constructor(message?: string, source?: A) {
public constructor(message?: string | null, source?: S, data?: IOfType<any>) {
if (!message) {
if (typeof source === 'object' && source !== null) {
message = (source as any).message;
Expand All @@ -15,48 +21,75 @@ export class KpoError<A> extends Error {

super(message);
this.source = source;
this.data = {};
this.data = data || {};
}
/**
* Custom error name
*/
public get name(): string {
return 'CustomError';
}
/**
* References `source.root` if it's a `KpoError`; references `source` if it's an instance of `Error`; otherwise it references itself.
*/
public get root(): Error {
if (isKpoError(this.source)) return this.source.root;
return this.source instanceof Error ? this.source : this;
}
public set<T extends KpoError<A>>(this: T, data: IOfType<any>): T {
/**
* Sets the `data` field and returns itself.
*/
public set<T extends KpoError<S>>(this: T, data: IOfType<any>): T {
this.data = data;
return this;
}
public assign<T extends KpoError<A>>(this: T, data: IOfType<any>): T {
/**
* Assigns `data` to the instance `data` object and returns itself.
*/
public assign<T extends KpoError<S>>(this: T, data: IOfType<any>): T {
Object.assign(this.data, data);
return this;
}
}

/**
* An error that whose stacktrace will be logged by default.
*/
export class OpenError<T> extends KpoError<T> {
public get name(): string {
return 'OpenError';
}
}

/**
* An error that won't result in the process exiting with code 1,
* and will be logged as a warning.
*/
export class SilentError<T> extends KpoError<T> {
public get name(): string {
return 'SilentError';
}
}

/**
* Returns an `OpenError` as long as `source` is not a `KpoError`.
*/
export function open(source?: any): KpoError<any> {
return isKpoError(source) ? source : new OpenError(undefined, source);
}

/**
* Returns a `KpoError` as long as `source` is not a `KpoError`.
*/
export function error(source?: any): KpoError<any> {
return isKpoError(source) ? source : new KpoError(undefined, source);
}

export function isKpoError(err: any): err is KpoError<any> {
// We're duck typing errors as there might be several instances running
return err && err instanceof Error && (err.constructor as any).isKpoError;
return Boolean(
err && err instanceof Error && (err.constructor as any).isKpoError
);
}

export function isOpenError(err: any): err is OpenError<any> {
Expand Down

0 comments on commit b75f82c

Please sign in to comment.