Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement enhanced safe wrapper. #3

Merged
merged 5 commits into from
Feb 9, 2025
Merged
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
8 changes: 7 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ jobs:
previous_version=$(npm show . version || echo "0.0.0")
current_version=$(node -p "require('./package.json').version")

if [ "$current_version" != "$previous_version" ]; then
if [ "$current_version" > "$previous_version" ]; then
echo -e "[+] current version[$current_version] is greater than previous version[$previous_version]"
echo -e "[+] publishing package to npm registry"

echo "publish=true" >> $GITHUB_ENV
else
echo -e "[-] current version[$current_version] is not greater than previous version[$previous_version]"
echo -e "[-] skipping publishing package to npm registry"

echo "publish=false" >> $GITHUB_ENV
fi

Expand Down
51 changes: 40 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
## safe-wrapper

[![npm version](https://img.shields.io/npm/v/safe-wrapper.svg)](https://www.npmjs.com/package/safe-wrapper)
[![License](https://img.shields.io/npm/l/safe-wrapper.svg)](https://github.com/mcking-07/safe-wrapper/blob/main/LICENSE)
[![Build Status](https://github.com/mcking-07/safe-wrapper/workflows/publish/badge.svg)](https://github.com/mcking-07/safe-wrapper/actions)
[![npm downloads](https://img.shields.io/npm/dm/safe-wrapper.svg)](https://www.npmjs.com/package/safe-wrapper)

safe-wrapper is a lightweight utility for javascript that simplifies error handling for both synchronous and asynchronous functions. inspired by the [safe assignment operator proposal](https://github.com/arthurfiorette/proposal-safe-assignment-operator), this utility allows for graceful error management by wrapping functions in a way that enables error handling without the need for explicit `try-catch` blocks.

#### Features
### Features

- handles synchronous and asynchronous functions.
- supports specifying error types to control which errors are caught and handled.
- returns consistent responses in `[error, result]` format where error is null if no error occurred.
- supports custom error transformation for advanced error handling.

#### Installation
### Installation

```
```sh
npm install safe-wrapper
```

#### Usage
### Usage

import `safe` from `safe-wrapper` to use it with any function.
import `safe` from `safe-wrapper` to use it with any function.

the `safe` function takes a target function (synchronous or asynchronous) and returns a function which handles errors and returns a response in a consistent way. the function returns an array `[error, result]`, where `error` is an instance of the specified error type or `null` if successful, and `result` is the result of the function when there is no error.

Expand Down Expand Up @@ -86,6 +92,29 @@ const safeSync = safe(sync, [TypeError, RangeError]);
const [error, result] = safeSync(args);
```

#### using custom error transformer

you can provide a custom error transformer function to modify how errors are handled:

```javascript
import { safe } from 'safe-wrapper';

const transformer = (error) => ({
code: error.name,
message: error.message,
timestamp: new Date()
});

const safeWithTransform = safe(
() => { throw new Error('custom error'); },
[Error],
transformer
);

const [error, result] = safeWithTransform();
// error will be: { code: 'Error', message: 'custom error', timestamp: Date }
```

#### wrapping built-in functions

we can also wrap built-in functions, like `JSON.parse`, `Object.keys`, and more.
Expand All @@ -99,14 +128,14 @@ const [error, result] = safeJsonParse('invalid_json');
const [error, result] = safe(Object.keys, [TypeError])(null);
```

#### API Reference
### API Reference

`safe(action, types, transformer)`

`safe(action, types)`
- parameters
- action (function): function to be wrapped. it can either be synchronous or asynchronous.
- types (array, optional): an array of error types to catch. if no types are specified, all errors are caught.
- types (array, optional): an array of error types to catch. if no types are specified, all errors are caught. each element must be a valid error constructor.
- transformer (function, optional): a function that takes an error object and returns a transformed version of it. if not provided, the original error is used.
- returns `[error, result]`
- error (error | null): the error object if error occurred, otherwise null.
- error (error | null): the error object if error occurred, otherwise null. if an transformer is provided, this will be the transformed error.
- result (any): the result of the action function if no error occurred, otherwise null.

this structure keeps it concise, approachable, and clear for all levels of users.
Loading