-
Notifications
You must be signed in to change notification settings - Fork 71
/
Result.ts
197 lines (181 loc) · 7.01 KB
/
Result.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { invariant } from './ts-helpers/invariant';
import { Narrow } from './ts-helpers/types';
/**
* A `Success<T, E>` represents a successful computation that returns a value of type `T`.
*
* `E` in `Success<T, E>` is the type of the error that would have been returned in case of failure.
* It's present only to allow type safety of the `isFailure` method.
*
* @sealed
* @privateRemarks DO NOT EXPORT, see type export later on
* @typeParam T - the success value type
*/
class Success<T> {
/** @internal */
public constructor(public readonly value: T) {}
public isSuccess(): this is Success<T> {
return true;
}
public isFailure(): this is Failure<unknown> {
return false;
}
unwrap(): T {
return this.value;
}
}
/**
* A `Success<T, E>` represents a successful computation that returns a value of type `T`.
*
* `T` in `Failure<T, E>` is the type of the value that would have been returned in case of success.
* It's present only to allow type safety of the `isSuccess` method.
*
* @sealed
* @privateRemarks DO NOT EXPORT, see type export later on
* @typeParam E - the failure error type
*/
class Failure<E> {
/** @internal */
public constructor(public readonly error: E) {}
public isSuccess(): this is Success<unknown> {
return false;
}
public isFailure(): this is Failure<E> {
return true;
}
unwrap(): never {
throw this.error;
}
}
export type { Success, Failure };
/**
* An `IEquatableError` is an error that can be compared by name.
*
*/
export interface IEquatableError<T extends string = string, P = Narrow<T>> {
name: P;
}
/**
* A `Result` type represents either `Success` or `Failure`.
*
* **TL;DR**
*
* `Result` is a minimalist implementation of a value that can be a "success" or a "failure".
* It borrows from what done in other modern languages (i.e. Rust, Kotlin, Swift, etc.).
*
* The Lens SDK adopts this pattern in order to:
* - be explicit about the known failure scenarios of a task,
* - provide a way for consumers to perform exhaustive error handling,
* - makes control flow easier to reason about.
*
* @remarks
*
* You might be familiar with the `Either` type from functional programming. The `Result` type
* could be seen as a more specific version of `Either` where the left side is reserved for
* success scenarios and the right side is reserved for known failure scenarios.
*
* Think of failure scenarios as alternative outcomes of a given task that although not the "happy path",
* are still legitimate results for the task within the boundary of a correct usage of the SDK.
*
* In promoting exhaustive error handling, the Lens SDK makes it easier to evolve your code
* when a new error case is added or a case is removed.
* For example after a Lens SDK upgrade you can simply run the TS compiler to figure out where you
* need to handle the new error cases, or even better, it guides you to remove obsolescent code
* where an error case is no longer possible. This is virtually impossible with a `try/catch` approach.
*
* Thrown exceptions are historically difficult to trace. They require implicit knowledge
* of the implementation details of the code that might throw exceptions. This might go several
* layers down and leads to tight coupling between modules.
*
* The Lens SDK still throws exceptions where the error is not a "normal execution scenario".
* These are considered real "exceptional circumstances" and not alternative outcomes and it's up to the consumer to `try/catch` them.
*
* An example of errors that are thrown by the SDK is {@link InvariantError}. They are often thrown as result of a misuse of the SDK.
* By throwing them we want to fail fast so the consumer can fix the issue as soon as possible.
* Specifically for `InvariantError`, there is no need to code defensively against these errors. Just rectify the coding issue and move on.
*
* @example
* Control flow
* ```ts
* const result: Result<number, RangeError> = doSomething();
*
* if (result.isFailure()) {
* // because of the `isFailure` check above, TS knows that `result` is a `Failure<RangeError>` here
* console.log(result.error); // result.error gets narrowed to `RangeError`
*
* return; // early return
* }
*
* // because of the `isFailure` check above and the early return, TS knows that `result` is a `Success<number>` here
* console.log(result.value); // result.value gets narrowed to `number`
* ```
*
* @example
* Exhaustive error handling
*
* Given a result type like the following:
*
* ```ts
* const result: Result<number, PendingSigningError | WalletConnectionError> = doSomething();
* ```
* You can use a function with a `switch` statement to perform exhaustive error handling:
* ```ts
* function format(failure: Failure<PendingSigningError | WalletConnectionError>): string {
* switch (failure.error.name) {
* case 'PendingSigningError':
* return 'Please sign the transaction';
* break;
* case 'WalletConnectionError':
* return 'Please connect your wallet and try again';
* break;
* }
* // any code after the switch statement is unreachable
* }
* ```
* The example above assumes `allowUnreachableCode: false` in your `tsconfig.json`.
*
* An even more robust way to perform exhaustive error handling with a `switch` is to use the `never` type: see {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#exhaustiveness-checking | exhaustiveness checking}.
*
* @see https://wiki.c2.com/?AvoidExceptionsWheneverPossible
* @see https://developer.apple.com/documentation/swift/result
* @see https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/
* @see https://the-guild.dev/blog/graphql-error-handling-with-fp#monads-to-the-rescue
*
* @typeParam T - the success value type
* @typeParam E - the failure error type
*/
export type Result<T, E extends IEquatableError> = Success<T> | Failure<E>;
/**
* A `PromiseResult` is a convenience type alias that represents either a {@link Result} in the context of asynchronous tasks.
*
* @typeParam T - the success value type
* @typeParam E - the failure error type
*/
export type PromiseResult<T, E extends IEquatableError> = Promise<Result<T, E>>;
/**
* @internal
*/
export function success(): Success<void>;
export function success<T>(value: T): Success<T>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function success<T>(value: any = undefined): Success<T> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return new Success(value);
}
/**
* @internal
*/
export const failure = <E extends IEquatableError>(error: E): Failure<E> => new Failure(error);
/**
* Ensures the `Result<T, E>` is a `Success<T>`.
*/
export function assertSuccess<T>(result: Result<T, IEquatableError>): asserts result is Success<T> {
invariant(result.isSuccess(), 'Expected a success result');
}
/**
* Ensures the `Result<T, E>` is a `Failure<E>`.
*/
export function assertFailure<T, E extends IEquatableError>(
result: Result<T, E>,
): asserts result is Failure<E> {
invariant(result.isFailure(), 'Expected a failure result');
}