Back to Examples

Rest binding pattern in error binding pattern

You can use the rest binding pattern (...r) to bind the detail mappings that are not explicitly bound in the error binding pattern. The type of the rest binding will be a map holding the fields that have not been matched.

import ballerina/io;

type SampleErrorData record {|
    int code;
    string reason;
|};

type SampleError error<SampleErrorData>;

public function main() {
    // The detail mapping can be destructured using a rest parameter.
    // `details` is of type `map<string|boolean>` having the `code` and `reason` fields.
    var error(message, ...details) = getSampleError();
    io:println("Message: ", message);

    map<int|string> detailsMap = details;
    io:println("Details: ", detailsMap);

    // Here, the `...filteredDetails` rest parameter contains only the detail fields
    // that are not matched.
    var error(_, code = code, ...filteredDetails) = getSampleError();
    io:println("Code: ", code);
    io:println("Filtered Details: ", filteredDetails);

    map<int|string> moreDetails;

    // The detail mapping can be destructured into a `map<int|string>` typed variable
    // by using a rest parameter.
    error(_, ...moreDetails) = getSampleError();
    io:println("All Details: ", moreDetails);
}

function getSampleError() returns SampleError {
    return error("Transaction Failure", error("Database Error"), code = 20,
                            reason = "deadlock condition");
}
$ bal run rest_binding_pattern_in_error_binding_pattern.balMessage: Transaction FailureDetails: {"code":20,"reason":"deadlock condition"}Code: 20Filtered Details: {"reason":"deadlock condition"}All Details: {"code":20,"reason":"deadlock condition"}

Related links

PreviousError binding pattern
NextSingle use of typed binding patterns