Skip to content

Instantly share code, notes, and snippets.

import ballerina/io;
const MY_REASON = "MyReason";
type MyDetail record {|
string message;
error cause?;
int code?;
boolean|float...;
|};
import ballerina/io;
const MY_REASON = "MyReason";
type MyDetail record {|
string message;
error cause?;
int code?;
anydata|error...;
|};
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 {
import ballerina/io;
const MY_REASON = "MyReason";
type MyDetail record {|
string message;
error cause?;
int code?;
anydata|error...;
|};
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();
import ballerina/io;
const REASON = "ErrorReason";
type Detail record {|
string message;
error cause?;
int code;
|};
public function main() {
error err = error("TestError",
message = "error message",
code = 1111);
record {|
string message?;
error cause?;
anydata|error...;
|} detail = err.detail();
import ballerina/log;
const ERR_REASON_FILE_NOT_FOUND = "FileNotFound";
const ERR_REASON_INSUFFICIENT_PERMISSION = "InsufficientPermission";
// The detail type for `FileNotFoundError` defaults to the default detail type now.
type FileNotFoundError error<ERR_REASON_FILE_NOT_FOUND>;
type ErrDetail record {|
string message?;
@MaryamZi
MaryamZi / xml_remove_all_attributes.bal
Last active September 30, 2019 05:18
An example demonstrating how to remove all attributes of a Ballerina XML value recursively.
import ballerina/io;
public function main() {
xmlns "http://abc.def";
xml x = xml `<hello foo="bar">
<ns1:world xmlns:ns1="http://qwe.asd" xyz="pqr">hello world</ns1:world>
</hello>`;
io:println(x);
import ballerina/io;
public function main() {
// If `foo()` does not panic, x will hold
// the value returned by `foo()`
int|string|error x = foo(1);
io:println(x);
// If `foo()` panics, x will hold
// the error associated with the panic.