This repository contains utility functions designed to make try catch and error-handling in Deno applications a tiny notch more easier and more convenient. Inspired by Go's approach.
Clone the repository:
git clone https://github.com/your-username/your-repo-name.gitThe tryCatchFin function provides a way to execute asynchronous operations and neatly handle success and failure cases.
Basic
import * as tcUtils from 'https://raw.githubusercontent.com/sdoerger/tryCatchFin-A-OneLiner/main/mod.ts';
const [result, error] = await tcUtils.tryCatchFin(() => 5 + 5);
tcUtils.assertError(error, 'An issue occurred');
console.log(result);Async
import * as tcUtils from 'https://raw.githubusercontent.com/sdoerger/tryCatchFinFin-A-OneLiner/main/mod.ts';
async function fetchAnimals() {
const response = await fetch(
"https://api.publicapis.org/entries?category=Animals",
{
method: "GET",
}
);
return await response.json();
};
const [result, error] = await tcUtils.tryCatchFin(fetchAnimals);
tcUtils.assertError(error, 'An issue occurred');
console.log(result);The assertError function logs an error message along with the error if it exists.
import { assertError } from './helpers.ts';
assertError(new Error("Some error"), "An error occurred");To run the tests, execute the following command:
deno test helpers_test.tsFeel free to contribute by submitting pull requests.
MIT License. See LICENSE for more information.