-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unify dal tables into db, add method overloads to error-or
- Loading branch information
Showing
10 changed files
with
154 additions
and
120 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
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,82 @@ | ||
import { expect, it, vi } from "vitest"; | ||
import { catchError } from "./catch-error.ts"; | ||
|
||
it("should return value if no error was thrown", () => { | ||
const result = "123"; | ||
const callback = () => result; | ||
|
||
expect(catchError(callback)).toEqual([undefined, result]); | ||
}); | ||
|
||
it("should throw if unknown error was thrown", () => { | ||
const callback = () => { | ||
throw new Error("test"); | ||
}; | ||
|
||
expect(() => catchError(callback, [CustomError])).toThrowError("test"); | ||
}); | ||
|
||
it("should return error if known error was thrown", () => { | ||
const error = new CustomError(); | ||
const callback = () => { | ||
throw error; | ||
}; | ||
|
||
const consoleSpy = vi.spyOn(console, "error"); | ||
|
||
expect(catchError(callback, [CustomError])).toEqual([error]); | ||
expect(consoleSpy, "should log error to console").toBeCalledWith(error); | ||
}); | ||
|
||
it("should return error if no known errors were passed", () => { | ||
const error = new CustomError(); | ||
const callback = () => { | ||
throw error; | ||
}; | ||
|
||
const consoleSpy = vi.spyOn(console, "error"); | ||
|
||
expect(catchError(callback)).toEqual([error]); | ||
expect(consoleSpy, "should log error to console").toBeCalledWith(error); | ||
}); | ||
|
||
it("should resolve value if no error was thrown", async () => { | ||
const result = "123"; | ||
const promise = Promise.resolve(result); | ||
|
||
expect(await catchError(promise)).toEqual([undefined, result]); | ||
}); | ||
|
||
it("should reject if unknown error was thrown", async () => { | ||
const promise = Promise.reject("test"); | ||
|
||
await expect( | ||
async () => await catchError(promise, [CustomError]), | ||
).rejects.toThrowError("test"); | ||
}); | ||
|
||
it("should resolve error if known error was thrown", async () => { | ||
const error = new CustomError(); | ||
const promise = Promise.reject(error); | ||
|
||
const consoleSpy = vi.spyOn(console, "error"); | ||
|
||
expect(await catchError(promise, [CustomError])).toEqual([error]); | ||
expect(consoleSpy, "should log error to console").toBeCalledWith(error); | ||
}); | ||
|
||
it("should resolve error if no known errors were passed", async () => { | ||
const error = new CustomError(); | ||
const promise = Promise.reject(error); | ||
|
||
const consoleSpy = vi.spyOn(console, "error"); | ||
|
||
expect(await catchError(promise)).toEqual([error]); | ||
expect(consoleSpy, "should log error to console").toBeCalledWith(error); | ||
}); | ||
|
||
class CustomError extends Error { | ||
constructor() { | ||
super("test"); | ||
} | ||
} |
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,5 @@ | ||
import type { Constructor } from "../constructor.ts"; | ||
|
||
export type ErrorOr<T, E extends Constructor<Error>> = | ||
| [undefined, T] | ||
| [InstanceType<E>]; |
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,3 @@ | ||
import { describe } from "vitest"; | ||
|
||
describe.todo("reThrowError"); |
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,40 @@ | ||
import type { Constructor } from "../constructor.ts"; | ||
import type { ErrorOr } from "./error-or.ts"; | ||
|
||
export async function reThrowError<T, E extends Constructor<Error>>( | ||
result: Promise<ErrorOr<T, E>>, | ||
): Promise<T>; | ||
export function reThrowError<T, E extends Constructor<Error>>( | ||
result: ErrorOr<T, E>, | ||
): T; | ||
export function reThrowError<T, E extends Constructor<Error>>( | ||
result: Promise<ErrorOr<T, E>> | ErrorOr<T, E>, | ||
): Promise<T> | T { | ||
if (result instanceof Promise) { | ||
return reThrowErrorAsync(result); | ||
} | ||
|
||
return reThrowErrorSync(result); | ||
} | ||
|
||
function reThrowErrorSync<T, E extends Constructor<Error>>( | ||
result: ErrorOr<T, E>, | ||
): T { | ||
const [error, data] = result; | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
return data as T; | ||
} | ||
|
||
async function reThrowErrorAsync<T, E extends Constructor<Error>>( | ||
result: Promise<ErrorOr<T, E>>, | ||
): Promise<T> { | ||
const [error, data] = await result; | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
return data as T; | ||
} |