For AI agents: the complete documentation index is available at /byethrow/llms.txt, the full documentation bundle is available at /byethrow/llms-full.txt, and this page is available as Markdown at /byethrow/guide/ecosystem/linter/no-try-catch-in-callback.md.
  • English
  • no-try-catch-in-callback

    Disallows try-catch blocks inside callbacks passed to @praha/byethrow functions, such as Result.andThen and Result.map.

    Rule Details

    A try-catch inside a callback is a sign that error handling has escaped the Result model. Wrap the throwing code with Result.fn() or Result.try() instead, and keep the pipeline itself free of try-catch.

    Incorrect

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // ❌ try-catch inside a callback const
    const result: Result.Result<any, ParseError>
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<"{\"key\": \"value\"}", never>, Result.Result<any, ParseError>>(a: Result.Result<"{\"key\": \"value\"}", never>, ab: (a: Result.Result<"{\"key\": \"value\"}", never>) => Result.Result<any, ParseError>): Result.Result<any, ParseError> (+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.@example
    import { 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<"{\"key\": \"value\"}">(value: "{\"key\": \"value\"}"): Result.Result<"{\"key\": \"value\"}", 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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With 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
    ('{"key": "value"}'),
    import Result
    Result
    .
    function andThen<Result.Result<"{\"key\": \"value\"}", never>, Result.Success<any> | Result.Failure<ParseError>>(fn: (a: "{\"key\": \"value\"}") => Result.Success<any> | Result.Failure<ParseError>): (result: Result.Result<"{\"key\": \"value\"}", never>) => Result.Result<any, ParseError> (+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.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // { type: 'Success', value: 6 }
    @example

    Failure 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' }
    @example

    Failure 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
    ((
    value: "{\"key\": \"value\"}"
    value
    ) => {
    try { return
    import Result
    Result
    .
    function succeed<any>(value: any): Result.Result<any, 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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With 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
    (
    var JSON: JSON

    An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

    JSON
    .
    JSON.parse(text: string, reviver?: (this: any, key: string, value: any) => any): any

    Converts a JavaScript Object Notation (JSON) string into an object.

    @paramtext A valid JSON string.@paramreviver A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is.@throws{SyntaxError} If text is not valid JSON.
    parse
    (
    value: "{\"key\": \"value\"}"
    value
    ));
    } catch { return
    import Result
    Result
    .
    function fail<ParseError>(error: ParseError): Result.Result<never, ParseError> (+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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With 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
    (new
    constructor ParseError(message?: string, options?: ErrorOptions): ParseError (+1 overload)
    ParseError
    ());
    } }), );

    Correct

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const parseJson: (value: string) => Result.Result<unknown, ParseError>
    parseJson
    =
    import Result
    Result
    .
    function fn<(value: string) => unknown, ParseError>(options: {
        try: (value: string) => unknown;
        catch: (error: unknown) => ParseError;
    }): (value: string) => Result.Result<unknown, ParseError> (+3 overloads)

    Wraps a function that may throw and returns a new function that returns a Result or ResultAsync .

    You can use either a custom catch handler or rely on the safe: true option to assume the function cannot throw.

    @typeParamT - The function type to execute (sync or async) or a Promise type.@typeParamE - The error type to return if catch is used.@returnsA new function that returns a Result or ResultAsync wrapping the original function's return value or the caught error.@example

    Sync try-catch

    import { Result } from '@praha/byethrow';
    
    const fn = Result.fn({
    try: (x: number) => {
    if (x < 0) throw new Error('Negative!');
    return x * 2;
    },
    catch: (error) => new Error('Oops!', { cause: error }),
    });
    
    const result = fn(5); // Result.Result<number, Error>
    @example

    Sync safe

    import { Result } from '@praha/byethrow';
    
    const fn = Result.fn({
    safe: true,
    try: (x: number) => x + 1,
    });
    
    const result = fn(1); // Result.Result<number, never>
    @example

    Async try-catch

    import { Result } from '@praha/byethrow';
    
    const fn = Result.fn({
    try: async (id: string) => await fetch(`/api/data/${id}`),
    catch: (error) => new Error('Oops!', { cause: error }),
    });
    
    const result = await fn('abc'); // Result.ResultAsync<Response, Error>
    @example

    Async safe

    import { Result } from '@praha/byethrow';
    
    const fn = Result.fn({
    safe: true,
    try: async () => await Promise.resolve('ok'),
    });
    
    const result = await fn(); // Result.ResultAsync<string, never>
    @categoryCreators
    fn
    ({
    try: (value: string) => unknown
    try
    : (
    value: string
    value
    : string) =>
    var JSON: JSON

    An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

    JSON
    .
    JSON.parse(text: string, reviver?: (this: any, key: string, value: any) => any): any

    Converts a JavaScript Object Notation (JSON) string into an object.

    @paramtext A valid JSON string.@paramreviver A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is.@throws{SyntaxError} If text is not valid JSON.
    parse
    (
    value: string
    value
    ) as unknown,
    catch: (error: unknown) => ParseError
    catch
    : (
    error: unknown
    error
    ) => new
    constructor ParseError(message?: string, options?: ErrorOptions): ParseError (+1 overload)
    ParseError
    ('Invalid JSON', {
    ErrorOptions.cause?: unknown
    cause
    :
    error: unknown
    error
    }),
    }); // ✅ Wrapping the throwing code with Result.fn() const
    const result: Result.Result<unknown, ParseError>
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<"{\"key\": \"value\"}", never>, Result.Result<unknown, ParseError>>(a: Result.Result<"{\"key\": \"value\"}", never>, ab: (a: Result.Result<"{\"key\": \"value\"}", never>) => Result.Result<unknown, ParseError>): Result.Result<unknown, ParseError> (+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.@example
    import { 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<"{\"key\": \"value\"}">(value: "{\"key\": \"value\"}"): Result.Result<"{\"key\": \"value\"}", 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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With 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
    ('{"key": "value"}'),
    import Result
    Result
    .
    function andThen<Result.Result<"{\"key\": \"value\"}", never>, Result.Result<unknown, ParseError>>(fn: (a: "{\"key\": \"value\"}") => Result.Result<unknown, ParseError>): (result: Result.Result<"{\"key\": \"value\"}", never>) => Result.Result<unknown, ParseError> (+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.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // { type: 'Success', value: 6 }
    @example

    Failure 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' }
    @example

    Failure 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 parseJson: (value: string) => Result.Result<unknown, ParseError>
    parseJson
    ),
    );

    Options

    This rule has no options.