0.0.7 • Published 2 years ago

possible-ts v0.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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>;

License

MIT

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.0.0

2 years ago