Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions __tests__/try.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Try } from "@src/try";
import { ObjectError } from "@src/error/object.error";
import { AsyncMethodError } from "@src/error/async.method.error";

class FirstCustomError extends Error {}
class ChildOfFirstCustomError extends FirstCustomError {}
Expand All @@ -19,6 +20,48 @@ describe("Try", () => {

expect(errorThrown).toBe(true);
});

it("Should allow sync", async () => {
let errorThrown = false;

Try.to<void>(() => {
throw new FirstCustomError();
})
.catch(FirstCustomError, (error) => {
errorThrown = true;
})
.runSync();

expect(errorThrown).toBe(true);
});

it("Should allow sync return", async () => {
const response = Try.to<boolean>(() => {
return true;
})
.runSync();

expect(response).toBe(true);
});

it("Should complain about sync", async () => {
expect(() => Try.to<void>(async () => {
const a = true;
})
.runSync()).toThrow(AsyncMethodError);
});

it("Should allow sync exception", async () => {
const result = Try.to<boolean>(() => {
throw new FirstCustomError();
})
.catch(FirstCustomError, (error) => {
return true;
})
.runSync();

expect(result).toBe(true);
});

it("Should allow error inheritance", async () => {
let errorThrown = false;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@randock/try",
"version": "0.0.8",
"version": "0.0.11",
"description": "Typed try/catch in typescript",
"author": "Jop Peters",
"private": false,
Expand Down
6 changes: 6 additions & 0 deletions src/error/async.method.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class AsyncMethodError extends Error {
constructor(message: string) {
super(message);
}
}

62 changes: 62 additions & 0 deletions src/try.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AsyncMethodError } from "./error/async.method.error";
import { ObjectError } from "./error/object.error";

export type AcceptFunction = (error: Error) => boolean;
Expand Down Expand Up @@ -115,10 +116,71 @@ export class Try<Response> {
return this.run();
}

runSync<T extends Try<Response>>(this: T): Response {
try {
const result = this.tryFunction();
if (result instanceof Promise) {
throw new AsyncMethodError('Cannot cal runSync if returnType of to() is a Promise. Use run() instead.');
}

return result;

} catch (e: any) {
if (e instanceof AsyncMethodError) {
throw e;
}

// if it is not an error object, we will convert it
if (!(e instanceof Error)) {
e = new ObjectError("Non Error thrown as an error.", e);
}

// we need to sort the catch block, so the "null" (is catch all) type goes last
this.catchBlocks.sort((a, b) => {
if (a.checker || b.checker) {
return a.checker ? -1 : 1;
}

if (a.types.includes(null) && !b.types.includes(null)) {
return 1;
} else if (b.types.includes(null)) {
return -1;
}

return 0;
});

// find the first matching catch block
for (const catchBlock of this.catchBlocks) {
if (catchBlock.types) {
for (const acceptedType of catchBlock.types) {
if (acceptedType === null || e instanceof acceptedType) {
return catchBlock.method(e);
}
}
}

if (catchBlock.checker !== undefined) {
if (catchBlock.checker(e)) {
return catchBlock.method(e);
}
}
}

// no matching block, so throw the error
throw e;
} finally {
if (this.finallyFunction !== null) {
this.finallyFunction();
}
}
}

/**
* You only need to call run, if you don't register a finally method
*/
async run<T extends Try<Response>>(this: T): Promise<Response> {

try {
return await this.tryFunction();
} catch (e: any) {
Expand Down