Releases: wiktor-obrebski/type-safe-errors
Introduce `resultify` function
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
Summary of Changes:
- Improved Asynchronous Support in
Result.combine
- Introduction of
UnknownError
Type - Bugfix: Resolved Type Issue
- Breaking Change: Syntax Change for Declaring
Ok
andErr
Types
Detailed Changes:
-
Improved Asynchronous Support in
Result.combine
The function now accepts both
Result
andPromise<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 );
-
Introduction of
UnknownError
TypeThe library now includes an
UnknownError
type for capturing unexpected errors during theResult
lifecycle. Theerr.cause
property refers to the original error that triggered theUnknownError
.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); } });
-
Bugfix: Resolved Type Issue
A bug affecting the return type of mapping functions has been fixed, enhancing type safety.
-
Breaking Change: Syntax Change for Declaring
Ok
andErr
TypesThe syntax for declaring
Ok
andErr
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`
Added support for undefined
/null
value results in Result.combine
API.
Error standarization and `Result.from`
Two big changes in this version:
-
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 toes5
or below, check here for more details:
https://github.com/wiktor-obrebski/type-safe-errors#errors-seems-not-be-catched-by-maperr-function -
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
First version with quite complete documentation of the api.
First usable version
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