diff --git a/docs/examples/error-set-example-1.zig b/docs/examples/error-set-example-1.zig new file mode 100644 index 00000000..76e31eec --- /dev/null +++ b/docs/examples/error-set-example-1.zig @@ -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, + }; +} diff --git a/docs/examples/error-set-example-1a.js b/docs/examples/error-set-example-1a.js new file mode 100644 index 00000000..f57d5bc5 --- /dev/null +++ b/docs/examples/error-set-example-1a.js @@ -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); +} diff --git a/docs/examples/error-set-example-1b.js b/docs/examples/error-set-example-1b.js new file mode 100644 index 00000000..56d11d1d --- /dev/null +++ b/docs/examples/error-set-example-1b.js @@ -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); diff --git a/docs/examples/error-set-example-1c.js b/docs/examples/error-set-example-1c.js new file mode 100644 index 00000000..a4ce3955 --- /dev/null +++ b/docs/examples/error-set-example-1c.js @@ -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}`); + } +} diff --git a/docs/examples/error-set-example-1d.js b/docs/examples/error-set-example-1d.js new file mode 100644 index 00000000..cf9b684f --- /dev/null +++ b/docs/examples/error-set-example-1d.js @@ -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)); diff --git a/docs/examples/error-set-example-1e.js b/docs/examples/error-set-example-1e.js new file mode 100644 index 00000000..a6710921 --- /dev/null +++ b/docs/examples/error-set-example-1e.js @@ -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)); diff --git a/docs/examples/error-set-example-1f.js b/docs/examples/error-set-example-1f.js new file mode 100644 index 00000000..f4d086bd --- /dev/null +++ b/docs/examples/error-set-example-1f.js @@ -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)); + diff --git a/docs/examples/error-set-example-1g.js b/docs/examples/error-set-example-1g.js new file mode 100644 index 00000000..605aba1c --- /dev/null +++ b/docs/examples/error-set-example-1g.js @@ -0,0 +1,7 @@ +import { fail } from './error-set-example-1.zig'; + +try { + fail(3); +} catch (err) { + console.log(JSON.stringify(err)); +} diff --git a/docs/examples/error-set-example-1h.js b/docs/examples/error-set-example-1h.js new file mode 100644 index 00000000..46c3253e --- /dev/null +++ b/docs/examples/error-set-example-1h.js @@ -0,0 +1,3 @@ +import { FileOpenError } from './error-set-example-1.zig'; + +console.log(FileOpenError(JSON.parse('{"error":"File not found"}'))); diff --git a/docs/examples/error-set-example-2.js b/docs/examples/error-set-example-2.js new file mode 100644 index 00000000..7c9fd81b --- /dev/null +++ b/docs/examples/error-set-example-2.js @@ -0,0 +1,5 @@ +import { AnyError } from './error-set-example-2.zig'; + +for (const [ name, err ] of AnyError) { + console.log(err.message); +} diff --git a/docs/examples/error-set-example-2.zig b/docs/examples/error-set-example-2.zig new file mode 100644 index 00000000..98c6899f --- /dev/null +++ b/docs/examples/error-set-example-2.zig @@ -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; diff --git a/docs/examples/error-set-example-3.js b/docs/examples/error-set-example-3.js new file mode 100644 index 00000000..67230108 --- /dev/null +++ b/docs/examples/error-set-example-3.js @@ -0,0 +1,7 @@ +import { fail } from './error-set-example-3.zig'; + +try { + fail(); +} catch (err) { + console.log(err.message); +} diff --git a/docs/examples/error-set-example-3.zig b/docs/examples/error-set-example-3.zig new file mode 100644 index 00000000..9e7f6c29 --- /dev/null +++ b/docs/examples/error-set-example-3.zig @@ -0,0 +1,5 @@ +const PrivateError = error{just_being_evil}; + +pub fn fail() anyerror!bool { + return PrivateError.just_being_evil; +} diff --git a/docs/examples/error-set-example-4.js b/docs/examples/error-set-example-4.js new file mode 100644 index 00000000..ac59bc03 --- /dev/null +++ b/docs/examples/error-set-example-4.js @@ -0,0 +1,7 @@ +import { fail } from './error-set-example-4.zig'; + +try { + fail(); +} catch (err) { + console.log(err.message); +} diff --git a/docs/examples/error-set-example-4.zig b/docs/examples/error-set-example-4.zig new file mode 100644 index 00000000..98c1fb30 --- /dev/null +++ b/docs/examples/error-set-example-4.zig @@ -0,0 +1,5 @@ +const PrivateError = error{just_being_evil}; + +pub fn fail() !bool { + return PrivateError.just_being_evil; +}