mirror of
https://github.com/oven-sh/bun
synced 2026-02-15 05:12:29 +00:00
refactor: move error files to api/error/
Step 4.3 of source reorganization - move error-related files: - DeferredError.zig - ErrorCode.cpp/h/ts/zig - ErrorStackFrame.cpp/h - ErrorStackTrace.cpp/h - Errorable.zig - JSErrorCode.zig - SystemError.zig - ZigErrorType.zig 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
43
src/buntime/api/error/Errorable.zig
Normal file
43
src/buntime/api/error/Errorable.zig
Normal file
@@ -0,0 +1,43 @@
|
||||
pub fn Errorable(comptime Type: type) type {
|
||||
return extern struct {
|
||||
result: Result,
|
||||
success: bool,
|
||||
|
||||
pub const Result = extern union {
|
||||
value: Type,
|
||||
err: ZigErrorType,
|
||||
};
|
||||
|
||||
pub fn unwrap(errorable: @This()) !Type {
|
||||
if (errorable.success) {
|
||||
return errorable.result.value;
|
||||
} else {
|
||||
return errorable.result.err.code.toError();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn value(val: Type) @This() {
|
||||
return @This(){ .result = .{ .value = val }, .success = true };
|
||||
}
|
||||
|
||||
pub fn ok(val: Type) @This() {
|
||||
return @This(){ .result = .{ .value = val }, .success = true };
|
||||
}
|
||||
|
||||
pub fn err(code: anyerror, err_value: bun.jsc.JSValue) @This() {
|
||||
return @This(){
|
||||
.result = .{
|
||||
.err = .{
|
||||
.code = ErrorCode.from(code),
|
||||
.value = err_value,
|
||||
},
|
||||
},
|
||||
.success = false,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const bun = @import("bun");
|
||||
const ErrorCode = @import("./ErrorCode.zig").ErrorCode;
|
||||
const ZigErrorType = @import("./ZigErrorType.zig").ZigErrorType;
|
||||
Reference in New Issue
Block a user