Skip to content

Commit

Permalink
Added error set examples (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
chung-leong committed Apr 8, 2024
1 parent be4e71e commit f4e0090
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/examples/error-set-example-1.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub const FileOpenError = error{
access_denied,
out_of_memory,
file_not_found,
};

pub const AllocationError = error{
out_of_memory,
};

pub fn fail(reason: u32) !bool {
return switch (reason) {
1 => FileOpenError.access_denied,
2 => FileOpenError.out_of_memory,
3 => FileOpenError.file_not_found,
else => false,
};
}
8 changes: 8 additions & 0 deletions docs/examples/error-set-example-1a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FileOpenError, fail } from './error-set-example-1.zig';

try {
fail(2);
} catch (err) {
console.log(err.message);
console.log(err === FileOpenError.out_of_memory);
}
5 changes: 5 additions & 0 deletions docs/examples/error-set-example-1b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AllocationError, FileOpenError } from './error-set-example-1.zig';

console.log(FileOpenError.out_of_memory === AllocationError.out_of_memory);
console.log(FileOpenError.out_of_memory instanceof FileOpenError);
console.log(AllocationError.out_of_memory instanceof AllocationError);
9 changes: 9 additions & 0 deletions docs/examples/error-set-example-1c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AllocationError, fail } from './error-set-example-1.zig';

for (let i = 1; i <= 3; i++) {
try {
fail(i);
} catch (err) {
console.log(`${err.message}: ${err in AllocationError}`);
}
}
6 changes: 6 additions & 0 deletions docs/examples/error-set-example-1d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AllocationError, FileOpenError } from './error-set-example-1.zig';

console.log(Number(FileOpenError.access_denied));
console.log(Number(FileOpenError.out_of_memory));
console.log(Number(FileOpenError.file_not_found));
console.log(Number(AllocationError.out_of_memory));
6 changes: 6 additions & 0 deletions docs/examples/error-set-example-1e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AllocationError, FileOpenError } from './error-set-example-1.zig';

console.log(String(FileOpenError.access_denied));
console.log(String(FileOpenError.out_of_memory));
console.log(String(FileOpenError.file_not_found));
console.log(String(AllocationError.out_of_memory));
7 changes: 7 additions & 0 deletions docs/examples/error-set-example-1f.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FileOpenError } from './error-set-example-1.zig';

console.log(FileOpenError(47));
console.log(FileOpenError('Error: Access denied'));
console.log(FileOpenError('access_denied'));
console.log(FileOpenError(42));

7 changes: 7 additions & 0 deletions docs/examples/error-set-example-1g.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fail } from './error-set-example-1.zig';

try {
fail(3);
} catch (err) {
console.log(JSON.stringify(err));
}
3 changes: 3 additions & 0 deletions docs/examples/error-set-example-1h.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { FileOpenError } from './error-set-example-1.zig';

console.log(FileOpenError(JSON.parse('{"error":"File not found"}')));
5 changes: 5 additions & 0 deletions docs/examples/error-set-example-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AnyError } from './error-set-example-2.zig';

for (const [ name, err ] of AnyError) {
console.log(err.message);
}
14 changes: 14 additions & 0 deletions docs/examples/error-set-example-2.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub const FileOpenError = error{
access_denied,
out_of_memory,
file_not_found,
};

pub const HumanError = error{
got_into_crypto_currencies,
ran_out_of_beer,
did_not_know_how_to_use_a_condom,
hung_out_with_clifford_banes,
};

pub const AnyError = anyerror;
7 changes: 7 additions & 0 deletions docs/examples/error-set-example-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fail } from './error-set-example-3.zig';

try {
fail();
} catch (err) {
console.log(err.message);
}
5 changes: 5 additions & 0 deletions docs/examples/error-set-example-3.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const PrivateError = error{just_being_evil};

pub fn fail() anyerror!bool {
return PrivateError.just_being_evil;
}
7 changes: 7 additions & 0 deletions docs/examples/error-set-example-4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fail } from './error-set-example-4.zig';

try {
fail();
} catch (err) {
console.log(err.message);
}
5 changes: 5 additions & 0 deletions docs/examples/error-set-example-4.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const PrivateError = error{just_being_evil};

pub fn fail() !bool {
return PrivateError.just_being_evil;
}

0 comments on commit f4e0090

Please sign in to comment.