-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error handling for Todoist API requests (#51)
- Loading branch information
1 parent
6787575
commit 7e65a3e
Showing
6 changed files
with
236 additions
and
79 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
export class Result<T, E> { | ||
private ok?: T; | ||
private error?: E; | ||
|
||
static Capture<T>(closure: () => T): Result<T, Error> { | ||
try { | ||
return Result.Ok<T, Error>(closure()); | ||
} catch (e) { | ||
return Result.Err<T, Error>(e); | ||
} | ||
} | ||
|
||
static Ok<T, E>(value: T): Result<T, E> { | ||
let result = new Result<T, E>(); | ||
result.ok = value; | ||
return result; | ||
} | ||
|
||
static Err<T, E>(err: E): Result<T, E> { | ||
let result = new Result<T, E>(); | ||
result.error = err; | ||
return result; | ||
} | ||
|
||
static All<T1, T2, T3, E>( | ||
first: Result<T1, E>, | ||
second: Result<T2, E>, | ||
third: Result<T3, E> | ||
): Result<[T1, T2, T3], E> { | ||
if (first.isErr()) { | ||
return first.intoErr(); | ||
} | ||
|
||
if (second.isErr()) { | ||
return second.intoErr(); | ||
} | ||
|
||
if (third.isErr()) { | ||
return third.intoErr(); | ||
} | ||
|
||
return Result.Ok([first.unwrap(), second.unwrap(), third.unwrap()]); | ||
} | ||
|
||
public isOk(): boolean { | ||
return this.ok != null; | ||
} | ||
|
||
public isErr(): boolean { | ||
return this.error != null; | ||
} | ||
|
||
public unwrap(): T { | ||
if (!this.isOk()) { | ||
throw new Error("Called 'unwrap' on a Result with an error."); | ||
} | ||
|
||
return this.ok; | ||
} | ||
|
||
public unwrapErr(): E { | ||
if (!this.isErr()) { | ||
throw new Error("Called 'unwrapErr' on a Result with a value."); | ||
} | ||
|
||
return this.error; | ||
} | ||
|
||
public intoErr<T2>(): Result<T2, E> { | ||
return Result.Err(this.error); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script lang="ts"> | ||
export let error: Error = null; | ||
</script> | ||
|
||
<div class="todoist-error"> | ||
<p>Oh no, something went wrong!</p> | ||
<code>{error}</code> | ||
</div> |
Oops, something went wrong.