Last active
November 30, 2020 13:11
-
-
Save pongo/4cf944c4f4d62fcf08ba6c8ca4b31b22 to your computer and use it in GitHub Desktop.
StacklessError and Result for typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tryParse(str: string): Result<unknown> { | |
try { | |
return Result.ok(JSON.parse(str)); | |
} catch (e) { | |
return Result.err((e as Error).message); | |
} | |
} | |
const parseResult = tryParse('{ "hello": "world" }'); | |
if (parseResult.isErr) { | |
console.error(parseResult.error); | |
return; | |
} | |
const obj = parseResult.value; | |
console.log(obj); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { StacklessError } from 'StacklessError'; | |
export type Ok<T> = { | |
readonly isOk: true; | |
readonly isErr: false; | |
readonly value: T; | |
}; | |
export type Err<E extends Error = Error> = { | |
readonly isOk: false; | |
readonly isErr: true; | |
readonly error: E; | |
}; | |
export type Result<T = undefined, E extends Error = Error> = Ok<T> | Err<E>; | |
function ok<T = undefined>(value?: T): Result<T, never> { | |
return { | |
isOk: true, | |
isErr: false, | |
value: value as T, | |
} as const; | |
} | |
function err<E extends Error = Error>(error: E): Result<never, E>; | |
function err<E extends Error = Error>(error: string, data?: unknown): Result<never, E>; | |
function err<E extends Error = Error>(error: E | string, data?: unknown): Result<never, E> { | |
return { | |
isOk: false, | |
isErr: true, | |
error: typeof error === 'string' ? (new StacklessError(error, data) as E) : error, | |
} as const; | |
} | |
function resultTry<T, E extends Error = Error>(fn: () => T): Result<T, E> { | |
try { | |
return ok(fn()); | |
} catch (e) { | |
return err<E>(e); | |
} | |
} | |
export const Result = { | |
ok, | |
err, | |
try: resultTry, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { inherits } from 'util'; | |
export class StacklessError { | |
readonly data?: unknown; | |
readonly message: string; | |
readonly name: string; | |
constructor(message = '', data?: unknown) { | |
this.name = this.constructor.name; | |
this.message = message; | |
this.data = data; | |
} | |
} | |
// new StacklessError('') instanceof Error === true | |
inherits(StacklessError, Error); | |
// Object.prototype.toString.call(new StacklessError) === '[object Error]' | |
Object.defineProperty(StacklessError.prototype, Symbol.toStringTag, { | |
value: 'Error', writable: false, configurable: false, enumerable: false, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment