Skip to content

sdoerger/TryCatchFin-A-OneLiner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tryCatchFinFin A OneLiner logo


TryCatchFin A OneLiner

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.

Table of Contents

Installation

Clone the repository:

git clone https://github.com/your-username/your-repo-name.git

Usage

tryCatchFin

The tryCatchFin function provides a way to execute asynchronous operations and neatly handle success and failure cases.

Example:

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);

assertError

The assertError function logs an error message along with the error if it exists.

Example:

import { assertError } from './helpers.ts';

assertError(new Error("Some error"), "An error occurred");

Running Tests

To run the tests, execute the following command:

deno test helpers_test.ts

Contributing

Feel free to contribute by submitting pull requests.

License

MIT License. See LICENSE for more information.

About

A Deno module, to simply wrap a try catch block and asssert error, for a slightly better readability

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published