possible-ts
v0.0.7
Published
typed errors
Downloads
35
Readme
possible
serializable typed errors
Install
$ npm install possible-ts
Usage
// Declare error constants.
const ERR_NOT_FOUND = newErr("not found");
// Return Possible<T> values from your methods.
const result: Possible<string> = readFile("...");
// Check for specific error types.
if (hasErr(result, ERR_NOT_FOUND)) {
return result;
}
// Check for errors.
if (hasErr(result)) {
return result.err.describe("read index");
}
API
interface Err {}
export declare type Possible<T> = T | Err;
export declare type AsyncPossible<T> = Promise<Possible<T>>;
// Create and print Errs.
export declare const newErr: (message: string, persistentIdentity?: number | null) => Err;
export declare const describeErr: (err: Err, messageOrErr: string | Err) => Err;
export declare const printErr: (err: Err) => string;
// Type guards to check for errors.
export declare const hasErr: (value: any, matcher?: Err) => value is Err;
// Wrap callbacks and Promises that might throw.
export declare function mightErr<T>(expr: Promise<T>): AsyncPossible<T>;
export declare function mightErr<T>(expr: () => T): Possible<T>;