Function: mapError()

Applies a transformation function to the error value of a Result or ResultAsync. If the input is a Success, it will be returned unchanged.

Type Param

The input Result or ResultAsync.

Type Param

The transformed error value type.

Examples

import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<never, Error>
result
=
import Result
Result
.
const pipe: <Result.Result<never, "NotFound">, Result.Result<never, Error>>(a: Result.Result<never, "NotFound">, ab: (a: Result.Result<never, "NotFound">) => Result.Result<never, Error>) => Result.Result<never, Error> (+25 overloads)
pipe
(
import Result
Result
.
const fail: <"NotFound">(error: "NotFound") => Result.Result<never, "NotFound"> (+1 overload)
fail
('NotFound'),
import Result
Result
.
const mapError: <Result.Result<never, "NotFound">, Error>(fn: (a: "NotFound") => Error) => (result: Result.Result<never, "NotFound">) => Result.Result<never, Error> (+1 overload)
mapError
((
error: "NotFound"
error
) => new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
(
error: "NotFound"
error
)),
); // { type: 'Failure', error: Error('NotFound') }
import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<123, Error>
result
=
import Result
Result
.
const pipe: <Result.Result<123, never>, Result.Result<123, Error>>(a: Result.Result<123, never>, ab: (a: Result.Result<123, never>) => Result.Result<123, Error>) => Result.Result<123, Error> (+25 overloads)
pipe
(
import Result
Result
.
const succeed: <123>(value: 123) => Result.Result<123, never> (+1 overload)
succeed
(123),
import Result
Result
.
const mapError: <Result.Result<123, never>, Error>(fn: (a: never) => Error) => (result: Result.Result<123, never>) => Result.Result<123, Error> (+1 overload)
mapError
((
error: never
error
) => new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
(
error: never
error
)),
); // { type: 'Success', value: 123 }

See

pipe - It is recommended to use this function with the pipe function for better readability and composability.

Call Signature

mapError<R1, E2>(fn): (result) => ResultFor<R1, InferSuccess<R1>, E2>

Type Parameters

R1

R1 extends ResultMaybeAsync<any, any>

E2

E2

Parameters

fn

(a) => E2

Returns

(result): ResultFor<R1, InferSuccess<R1>, E2>

Parameters

result

R1

Returns

ResultFor<R1, InferSuccess<R1>, E2>

Call Signature

mapError<E1, E2>(fn): <R1>(result) => ResultFor<R1, InferSuccess<R1>, E2>

Type Parameters

E1

E1

E2

E2

Parameters

fn

(a) => E2

Returns

<R1>(result): ResultFor<R1, InferSuccess<R1>, E2>

Type Parameters

R1

R1 extends ResultMaybeAsync<any, E1>

Parameters

result

R1

Returns

ResultFor<R1, InferSuccess<R1>, E2>