Skip to content

Instantly share code, notes, and snippets.

@MaryamZi
Last active December 31, 2019 10:31
Show Gist options
  • Save MaryamZi/8b1720059fc2c9db142784cac210f040 to your computer and use it in GitHub Desktop.
Save MaryamZi/8b1720059fc2c9db142784cac210f040 to your computer and use it in GitHub Desktop.
import ballerina/io;
function foo() returns error? {
return error("ErrReason", message = "err message", code = 123, bar = 45.6, baz = false);
}
public function main() {
error? res = foo();
if res is error {
string errReason;
string? errMessage;
anydata|error errCode;
anydata|error errPriority;
// Since the unmatched fields could be of any type belonging to `anydata|error`, we use a
// `map<anydata|error>` to hold the rest of the fields.
map<anydata|error> rest;
// Only one rest binding pattern can be specified.
// Specified as `...var-name`.
error(errReason, message = errMessage, code = errCode, priority = errPriority, ...rest) = res;
io:println("errReason: ", errReason); // ErrReason
io:println("errMessage: ", errMessage); // err message
io:println("errCode is int: ", errCode is int); // true
io:println("errPriority is (): ", errPriority is ()); // true
io:println("length of rest: ", rest.length()); // 2 - bar and baz
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment