#Debugging
When a pipeline doesn't behave as expected, you want to see the value at each step.
inspect and inspectError let you run a side effect, such as logging, without affecting the Result that flows through the pipeline.
#Inspecting Success Values with inspect
inspect(fn)Select a case to follow its pathinspect calls the given function with the success value and then returns the original Result unchanged.
If the Result is a Failure, the function is not called:
import { import Result Result } from '@praha/byethrow';
const const success: Result.Result<number, never> success = import Result Result .function pipe<Result.Result<42, never>, Result.Result<42, never>, Result.Result<number, never>>(a: Result.Result<42, never>, ab: (a: Result.Result<42, never>) => Result.Result<42, never>, bc: (b: Result.Result<42, never>) => Result.Result<number, never>): Result.Result<number, never> (+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<42>(value: 42): Result.Result<42, 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 (42),
import Result Result .function inspect<Result.Result<42, never>, void>(fn: (a: 42) => void): (result: Result.Result<42, never>) => Result.Result<42, never> (+1 overload)Executes a side effect function on the success value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Result: { type: 'Success', value: 42 }
@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspect((value) => console.log('Debug:', value)),
);
// No console output
// Result: { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: 42 value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('Value:', value: 42 value )),
import Result Result .function map<Result.Result<42, never>, number>(fn: (a: 42) => number): (result: Result.Result<42, never>) => Result.Result<number, never> (+1 overload)Applies a transformation function to the success value of a
Result
or
ResultAsync
.
If the input is a
Failure
, it will be returned unchanged.
@typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(2),
Result.map((x) => x * 10),
);
// { type: 'Success', value: 20 }
@exampleFailure Case (unchanged)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.map((x) => x * 10),
);
// { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators map ((value: 42 value ) => value: 42 value * 2),
);
// Console: "Value: 42"
// { type: 'Success', value: 84 }
const const failure: Result.Result<never, "error"> failure = import Result Result .function pipe<Result.Result<never, "error">, Result.Result<never, "error">>(a: Result.Result<never, "error">, ab: (a: Result.Result<never, "error">) => 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<"error">(error: "error"): Result.Result<never, "error"> (+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 ('error'),
import Result Result .function inspect<Result.Result<never, "error">, void>(fn: (a: never) => void): (result: Result.Result<never, "error">) => Result.Result<never, "error"> (+1 overload)Executes a side effect function on the success value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Result: { type: 'Success', value: 42 }
@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspect((value) => console.log('Debug:', value)),
);
// No console output
// Result: { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: never value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('Value:', value: never value )), // Not called
);
// { type: 'Failure', error: 'error' }#Inspecting Errors with inspectError
inspectError(fn)Select a case to follow its pathinspectError is the counterpart of inspect for errors.
It calls the given function with the error value, and does nothing for a Success:
import { import Result Result } from '@praha/byethrow';
const const failure: Result.Result<never, "Something went wrong"> failure = import Result Result .function pipe<Result.Result<never, "Something went wrong">, Result.Result<never, "Something went wrong">>(a: Result.Result<never, "Something went wrong">, ab: (a: Result.Result<never, "Something went wrong">) => Result.Result<never, "Something went wrong">): Result.Result<never, "Something went wrong"> (+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<"Something went wrong">(error: "Something went wrong"): Result.Result<never, "Something went wrong"> (+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 ('Something went wrong'),
import Result Result .function inspectError<Result.Result<never, "Something went wrong">, void>(fn: (a: "Something went wrong") => void): (result: Result.Result<never, "Something went wrong">) => Result.Result<never, "Something went wrong"> (+1 overload)Executes a side effect function on the error value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// Console output: "Debug error: error"
// Result: { type: 'Failure', error: 'error' }
@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// No console output
// Result: { type: 'Success', value: 42 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspectError ((error: "Something went wrong" error ) => var console: Console console .Console.error(...data: any[]): voidThe console.error() static method outputs a message to the console at the "error" log level. The message is only displayed to the user if the console is configured to display error output. In most cases, the log level is configured within the console UI. The message may be formatted as an error, with red colors and call stack information.
error ('Error:', error: "Something went wrong" error )),
);
// Console: "Error: Something went wrong"
// { type: 'Failure', error: 'Something went wrong' }
const const success: Result.Result<42, never> success = import Result Result .function pipe<Result.Result<42, never>, Result.Result<42, never>>(a: Result.Result<42, never>, ab: (a: Result.Result<42, never>) => Result.Result<42, never>): Result.Result<42, never> (+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<42>(value: 42): Result.Result<42, 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 (42),
import Result Result .function inspectError<Result.Result<42, never>, void>(fn: (a: never) => void): (result: Result.Result<42, never>) => Result.Result<42, never> (+1 overload)Executes a side effect function on the error value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// Console output: "Debug error: error"
// Result: { type: 'Failure', error: 'error' }
@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// No console output
// Result: { type: 'Success', value: 42 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspectError ((error: never error ) => var console: Console console .Console.error(...data: any[]): voidThe console.error() static method outputs a message to the console at the "error" log level. The message is only displayed to the user if the console is configured to display error output. In most cases, the log level is configured within the console UI. The message may be formatted as an error, with red colors and call stack information.
error ('Error:', error: never error )), // Not called
);
// { type: 'Success', value: 42 }#Example: Tracing a Pipeline
Insert inspect between the steps you want to observe, and inspectError at the end to log whichever step failed:
import { import Result Result } from '@praha/byethrow';
const const processData: (input: Input) => Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed"> processData = (input: Input input : type Input = {
data: string;
}
Input ) => {
return import Result Result .function pipe<Result.Result<Input, never>, Result.Result<Input, never>, Result.Result<Input, "ValidateFailed">, Result.Result<Transformed, "ValidateFailed" | "TransformFailed">, Result.Result<Transformed, "ValidateFailed" | "TransformFailed">, Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">, Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">, Result.Result<Saved, "ValidateFailed" | ... 1 more ... | "SaveFailed">>(a: Result.Result<...>, ab: (a: Result.Result<...>) => Result.Result<...>, bc: (b: Result.Result<...>) => Result.Result<...>, cd: (c: Result.Result<...>) => Result.Result<...>, de: (d: Result.Result<...>) => Result.Result<...>, ef: (e: Result.Result<...>) => Result.Result<...>, fg: (f: Result.Result<...>) => Result.Result<...>, gh: (g: Result.Result<...>) => Result.Result<...>): Result.Result<...> (+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<Input>(value: Input): Result.Result<Input, 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 (input: Input input ),
import Result Result .function inspect<Result.Result<Input, never>, void>(fn: (a: Input) => void): (result: Result.Result<Input, never>) => Result.Result<Input, never> (+1 overload)Executes a side effect function on the success value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Result: { type: 'Success', value: 42 }
@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspect((value) => console.log('Debug:', value)),
);
// No console output
// Result: { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: Input value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('Input:', value: Input value )),
import Result Result .function andThrough<Result.Result<Input, never>, Result.Result<void, "ValidateFailed">>(fn: (a: Input) => Result.Result<void, "ValidateFailed">): (result: Result.Result<Input, never>) => Result.Result<Input, "ValidateFailed"> (+1 overload)Runs an additional computation using the success value of a
Result
or
ResultAsync
,
but returns the original result if the additional computation is successful.
If either the original result or the side effect result is a
Failure
, that failure is returned.
Useful for running validations or side effects without altering the main result on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (input is a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(-10),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'Must be > 0' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThrough (const validate: (input: Input) => Result.Result<void, "ValidateFailed"> validate ),
import Result Result .function andThen<Result.Result<Input, "ValidateFailed">, Result.Result<Transformed, "TransformFailed">>(fn: (a: Input) => Result.Result<Transformed, "TransformFailed">): (result: Result.Result<Input, "ValidateFailed">) => Result.Result<Transformed, "ValidateFailed" | "TransformFailed"> (+1 overload)Chains the next computation using the success value of a
Result
or
ResultAsync
.
If the original result is a
Failure
, it is returned unchanged.
Otherwise, the provided function is called, and its result is returned as-is.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(3),
Result.andThen((x) => Result.succeed(x * 2)),
);
// { type: 'Success', value: 6 }
@exampleFailure Case (input is a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.andThen((x) => Result.succeed(x * 2)),
);
// result: { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(3),
Result.andThen((x) => Result.fail('error: ' + x)),
);
// result: { type: 'Failure', error: 'error: 3' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThen (const transform: (input: Input) => Result.Result<Transformed, "TransformFailed"> transform ),
import Result Result .function inspect<Result.Result<Transformed, "ValidateFailed" | "TransformFailed">, void>(fn: (a: Transformed) => void): (result: Result.Result<Transformed, "ValidateFailed" | "TransformFailed">) => Result.Result<Transformed, "ValidateFailed" | "TransformFailed"> (+1 overload)Executes a side effect function on the success value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Result: { type: 'Success', value: 42 }
@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspect((value) => console.log('Debug:', value)),
);
// No console output
// Result: { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: Transformed value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('After transform:', value: Transformed value )),
import Result Result .function andThen<Result.Result<Transformed, "ValidateFailed" | "TransformFailed">, Result.Result<Saved, "SaveFailed">>(fn: (a: Transformed) => Result.Result<Saved, "SaveFailed">): (result: Result.Result<Transformed, "ValidateFailed" | "TransformFailed">) => Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed"> (+1 overload)Chains the next computation using the success value of a
Result
or
ResultAsync
.
If the original result is a
Failure
, it is returned unchanged.
Otherwise, the provided function is called, and its result is returned as-is.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(3),
Result.andThen((x) => Result.succeed(x * 2)),
);
// { type: 'Success', value: 6 }
@exampleFailure Case (input is a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.andThen((x) => Result.succeed(x * 2)),
);
// result: { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(3),
Result.andThen((x) => Result.fail('error: ' + x)),
);
// result: { type: 'Failure', error: 'error: 3' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThen (const save: (input: Transformed) => Result.Result<Saved, "SaveFailed"> save ),
import Result Result .function inspect<Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">, void>(fn: (a: Saved) => void): (result: Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">) => Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed"> (+1 overload)Executes a side effect function on the success value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Result: { type: 'Success', value: 42 }
@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspect((value) => console.log('Debug:', value)),
);
// No console output
// Result: { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: Saved value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('After save:', value: Saved value )),
import Result Result .function inspectError<Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">, void>(fn: (a: "ValidateFailed" | "TransformFailed" | "SaveFailed") => void): (result: Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">) => Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed"> (+1 overload)Executes a side effect function on the error value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// Console output: "Debug error: error"
// Result: { type: 'Failure', error: 'error' }
@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// No console output
// Result: { type: 'Success', value: 42 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspectError ((error: "ValidateFailed" | "TransformFailed" | "SaveFailed" error ) => var console: Console console .Console.error(...data: any[]): voidThe console.error() static method outputs a message to the console at the "error" log level. The message is only displayed to the user if the console is configured to display error output. In most cases, the log level is configured within the console UI. The message may be formatted as an error, with red colors and call stack information.
error ('Pipeline failed:', error: "ValidateFailed" | "TransformFailed" | "SaveFailed" error )),
);
};#Things to Keep in Mind
- The return value is ignored. Even if the function returns a
Failure, the originalResultis passed on. If a side effect can fail and its failure matters, useandThroughororThroughinstead. - Async functions are awaited. If the function returns a
Promise, the pipeline waits for it before continuing, and becomes asynchronous. - Not only for debugging. Placing
inspectandinspectErrorat the end of a pipeline is also a simple way to act on the final outcome, such as printing output or setting an exit code. See the Examples for this pattern.
#References
| API | Purpose |
|---|---|
| inspect(fn) | Run a side effect on the success value |
| inspectError(fn) | Run a side effect on the error value |
