Skip to content

Instantly share code, notes, and snippets.

@MaryamZi
Created December 31, 2019 10:44
Show Gist options
  • Save MaryamZi/dc90ff82377986c076f1642627b04da6 to your computer and use it in GitHub Desktop.
Save MaryamZi/dc90ff82377986c076f1642627b04da6 to your computer and use it in GitHub Desktop.
import ballerina/io;
const MY_REASON = "MyReason";
type MyDetail record {|
string message;
error cause?;
int code?;
anydata|error...;
|};
type MyError error<MY_REASON, MyDetail>;
function foo() returns error? {
return MyError(message = "err message", code = 123, bar = 12.3, baz = false);
}
public function main() {
// Say I call a function returning either an `error` or `()`.
error? res = foo();
if res is MyError {
string errMessage;
int? errCode;
map<anydata|error> rest;
// Using an indirect error binding pattern.
MyError(message = errMessage, code = errCode, ...rest) = res;
io:println("errMessage: ", errMessage); // err message
io:println("errCode is int: ", errCode is int); // true
io:println("length of rest: ", rest.length()); // 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment