Last active
December 31, 2019 10:38
-
-
Save MaryamZi/9ccf9172ff296f0086916b5d6af03569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ballerina/io; | |
function foo() returns error? { | |
return error("ErrReason", message = "err message", code = 123); | |
} | |
public function main() { | |
// Say I call a function returning either an `error` or `()`. | |
error? res = foo(); | |
if res is error { | |
// Assume if the value is an error, I want to access the error | |
// reason and the `message`, `code`, and `priority` fields from | |
// the detail if present. | |
// A reason is always present and is a string. | |
// Thus, we use a `string` variable for the reason. | |
string errReason; | |
string errReason2; | |
// `message` is an optional field in the detail, | |
// but if present is of type `string`. | |
// Thus, we use a `string?` variable for `message`. | |
string? errMessage; | |
string? errMessage2; | |
// `code` and `priority` if present would be part of the rest | |
// field of the error detail, which is of type `anydata|error`. | |
// Since `()` (representing the absence) is already part of | |
// `anydata`, we use variables of type `anydata|error`. | |
anydata|error errCode; | |
anydata|error errPriority; | |
anydata|error errCode2; | |
anydata|error errPriority2; | |
// Accessing the reason can be done via `.reason()` and the | |
// detail fields can be accessed by first accessing the mapping | |
// via `.detail()`. | |
// --------------- VIA `.reason()` and `.detail()` --------------- | |
errReason = res.reason(); | |
errMessage = res.detail()?.message; | |
errCode = res.detail()["code"]; | |
errPriority = res.detail()["priority"]; | |
io:println("--------------- VIA `.reason()` and `.detail()` ---------------"); | |
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 | |
// Alternately, the same could be achieved in one line using | |
// destructuring binding patterns. We don't even have to call | |
// the `.reason()` or `.detail()` methods to access to access these values. | |
// --------------- VIA binding patterns --------------- | |
error(errReason2, message = errMessage2, code = errCode2, priority = errPriority2) = res; | |
// If any binding patterns are present in a direct error binding pattern, | |
// the first pattern must be a simple binding pattern (`errReason`) which | |
// can hold the `string` reason. | |
// Subsequent patterns (other than a rest pattern), must be named arg binding patterns | |
// (e.g., `message = errMessage2`) which take a field from the detail (`message`), | |
// and assign it to the variable specified on the RHS (`errMessage2`). | |
// If the particular field is not present, `()` is set as the value. | |
io:println("\n--------------- VIA binding patterns ---------------"); | |
io:println("errReason2: ", errReason2); // ErrReason | |
io:println("errMessage2: ", errMessage2); // err message | |
io:println("errCode2 is int: ", errCode2 is int); // true | |
io:println("errPriority2 is (): ", errPriority2 is ()); // true | |
// The reason can be ignored using `_` | |
// error(_, message = errMessage) = res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment