Skip to content

Instantly share code, notes, and snippets.

@MaryamZi
Created December 24, 2019 10:35
Show Gist options
  • Save MaryamZi/2af573d67b702ee69e216fb7d17a78de to your computer and use it in GitHub Desktop.
Save MaryamZi/2af573d67b702ee69e216fb7d17a78de to your computer and use it in GitHub Desktop.
import ballerina/io;
const REASON = "ErrorReason";
type Detail record {|
string message;
error cause?;
int code;
|};
type CustomError error<REASON, Detail>;
public function main() {
// Use the direct error constructor to create an error
// similar to what could be created as a `CustomError`.
error err = error("ErrorReason",
message = "error message",
code = 1122);
// Since the error value is immutable it cannot be changed
// now. The type test evaluates to `true` since the reason is
// the same as what is expected by `CustomError` and the expected
// detail fields (i.e., `message`, `code`) are also available
// and are of the expected types.
io:println(err is CustomError);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment