-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be4e71e
commit f4e0090
Showing
15 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
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
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, | ||
}; | ||
} |
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
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); | ||
} |
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
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); |
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
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}`); | ||
} | ||
} |
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
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)); |
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
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)); |
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
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)); | ||
|
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
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)); | ||
} |
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
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"}'))); |
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
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); | ||
} |
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
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; |
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
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); | ||
} |
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
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; | ||
} |
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
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); | ||
} |
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
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; | ||
} |