Skip to content

Releases: wiktor-obrebski/type-safe-errors

Introduce `resultify` function

22 Dec 12:02
Compare
Choose a tag to compare

Summary of Changes:

  • introduce new helper function resultify

Detailed Changes:

resultify is a helper function akin to Node.js's utils.promisify.
It modifies the return type of any given function, synchronous or asynchronous, to Result.

Examples:

import { Result, Err, resultify } from 'type-safe-errors';

class FetchFailedError extends Error {
  name = "FetchFailedError" as const;
}

async function fetchDataOrErrorResult () {
  try {
    const res = await fetchRemoteData();
    return res.data;
  } catch (err) {
    console.log(err);
    return Err.of(new FetchFailedError());
  }
};

const fetchRemoteDataAsResult = resultify(fetchDataOrErrorResult);
fetchRemoteDataAsResult()
  .map(data => data)
  .mapErr(FetchFailedError, (err) => console.log(err));

Enhanced Asynchronous Support, Introduction of UnknownError, and Syntax Standardization

10 Oct 04:59
Compare
Choose a tag to compare

Summary of Changes:

  • Improved Asynchronous Support in Result.combine
  • Introduction of UnknownError Type
  • Bugfix: Resolved Type Issue
  • Breaking Change: Syntax Change for Declaring Ok and Err Types

Detailed Changes:

  1. Improved Asynchronous Support in Result.combine

    The function now accepts both Result and Promise<Result> instances, broadening its utility in asynchronous programming.

    import { Ok, Result } from 'type-safe-errors';
    const ok1Result = Ok.of(5);
    const ok2ResultFactory = async () => Ok.of(9);
    const okSumResult = Result.combine([ok1Result, ok2ResultFactory()]).map(
      ([val1, val2]) => val1 + val2
    );

    API Reference

  2. Introduction of UnknownError Type

    The library now includes an UnknownError type for capturing unexpected errors during the Result lifecycle. The err.cause property refers to the original error that triggered the UnknownError.

    Ok.of(5)
      .map(val => {
        throw new Error('Problem!');
      })
      .mapErr(UnknownError, err => console.error(err.cause));
    Ok.of(5)
      .map(val => {
        throw new Error('Problem!');
      })
      .mapAnyErr(err => {
        if (err instanceof UnknownError) {
          console.error(err.cause);
        }
      });

    API Reference

  3. Bugfix: Resolved Type Issue

    A bug affecting the return type of mapping functions has been fixed, enhancing type safety.

  4. Breaking Change: Syntax Change for Declaring Ok and Err Types

    The syntax for declaring Ok and Err types has been standardized. The previously supported but undocumented alternative is no longer valid. The recommended format is as follows:

    type FetchUserAgeResult = Ok<number> | Err<UserNotFoundError> | Err<ForbiddenError>;

    Using the older, now unsupported syntax, will result in a type error:

    // Not Supported
    type FetchUserAgeResult = Ok<number> | Err<UserNotFoundError | ForbiddenError>;

Feedback is encouraged and can be submitted through GitHub issues.

Improve `Result.combine`

10 Mar 15:06
Compare
Choose a tag to compare

Added support for undefined/null value results in Result.combine API.

Error standarization and `Result.from`

29 Jul 12:53
Compare
Choose a tag to compare

Two big changes in this version:

  1. All example projects and examples in the documentation has been migrated to new recommended way of handling custom domain error. The error class should now inherit by JS Error object. Motivation described here:
    #7
    This change can make problems if you compile your code to es5 or below, check here for more details:
    https://github.com/wiktor-obrebski/type-safe-errors#errors-seems-not-be-catched-by-maperr-function

  2. New API has been added Result.from. Before starting result chains from async function has been a little akward. It need one of this things:

const userResult = await fetchUserResult();
return userResult
  .map(user => console.log(user))
  .mapErr(UserNotFoundError, err => console.error(err));

or

return Ok.of(null)
  .map(() => fetchUserResult())
  .map(user => console.log(user))
  .mapErr(UserNotFoundError, err => console.error(err));

New API allows to standarize such cases to:

return Result.from(() => fetchUserResult())
  .map(user => console.log(user))
  .mapErr(UserNotFoundError, err => console.error(err));

Rich documentation release

28 Aug 08:37
Compare
Choose a tag to compare

First version with quite complete documentation of the api.

First usable version

06 Feb 10:31
Compare
Choose a tag to compare

The version provide first interface that support async results and can be imported directly from npm.
Included api:

  • Result.combine
  • Err.of
  • Ok.of
  • result::map
  • result::mapErr
  • result::promise