#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.
mapError(fn)ケースを選ぶと、その流れだけを追えます#Type Param
R1
The input Result or ResultAsync.
#Type Param
E2
The transformed error value type.
#Examples
Failure Case (unchanged)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<never, Error> result = import Result Result .function 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)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Result Result .function fail<"NotFound">(error: "NotFound"): Result.Result<never, "NotFound"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('NotFound'),
import Result Result .function mapError<Result.Result<never, "NotFound">, Error>(fn: (a: "NotFound") => Error): (result: Result.Result<never, "NotFound">) => Result.Result<never, Error> (+1 overload)Applies a transformation function to the error value of a
Result
or
ResultAsync
.
If the input is a
Success
, it will be returned unchanged.
@typeParamR1 - The input Result or ResultAsync .@typeParamE2 - The transformed error value type.@exampleFailure Case (unchanged)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('NotFound'),
Result.mapError((error) => new Error(error)),
);
// { type: 'Failure', error: Error('NotFound') }
@exampleSuccess Case (unchanged)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(123),
Result.mapError((error) => new Error(error)),
);
// { type: 'Success', value: 123 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators 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') }Success Case (unchanged)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<123, Error> result = import Result Result .function 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)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Result Result .function succeed<123>(value: 123): Result.Result<123, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (123),
import Result Result .function mapError<Result.Result<123, never>, Error>(fn: (a: never) => Error): (result: Result.Result<123, never>) => Result.Result<123, Error> (+1 overload)Applies a transformation function to the error value of a
Result
or
ResultAsync
.
If the input is a
Success
, it will be returned unchanged.
@typeParamR1 - The input Result or ResultAsync .@typeParamE2 - The transformed error value type.@exampleFailure Case (unchanged)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('NotFound'),
Result.mapError((error) => new Error(error)),
);
// { type: 'Failure', error: Error('NotFound') }
@exampleSuccess Case (unchanged)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(123),
Result.mapError((error) => new Error(error)),
);
// { type: 'Success', value: 123 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators 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>
#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>
