diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig index 0721ee88fbdd06..274bc68f46f170 100644 --- a/src/bun.js/webcore/blob.zig +++ b/src/bun.js/webcore/blob.zig @@ -910,9 +910,7 @@ pub const Blob = struct { switch (result) { .success => this.promise.resolve(globalObject, JSC.jsNumber(0)), .failure => |err| { - const js_err = globalObject.createErrorInstance("{s}", .{err.message}); - js_err.put(globalObject, JSC.ZigString.static("code"), JSC.ZigString.init(err.code).toJS(globalObject)); - this.promise.rejectOnNextTick(globalObject, js_err); + this.promise.rejectOnNextTick(globalObject, err.toJS(globalObject)); }, } } @@ -1061,9 +1059,7 @@ pub const Blob = struct { switch (result) { .success => this.promise.resolve(globalObject, JSC.jsNumber(this.store.data.bytes.len)), .failure => |err| { - const js_err = globalObject.createErrorInstance("{s}", .{err.message}); - js_err.put(globalObject, JSC.ZigString.static("code"), JSC.ZigString.init(err.code).toJS(globalObject)); - this.promise.rejectOnNextTick(globalObject, js_err); + this.promise.rejectOnNextTick(globalObject, err.toJS(globalObject)); }, } } @@ -3676,9 +3672,7 @@ pub const Blob = struct { self.promise.reject(globalObject, js_err); }, .failure => |err| { - const js_err = globalObject.createErrorInstance("{s}", .{err.message}); - js_err.put(globalObject, ZigString.static("code"), ZigString.init(err.code).toJS(globalObject)); - self.promise.rejectOnNextTick(globalObject, js_err); + self.promise.rejectOnNextTick(globalObject, err.toJS(globalObject)); }, } } @@ -4018,9 +4012,7 @@ pub const Blob = struct { this.promise.reject(this.globalThis, js_err); }, .failure => |err| { - const js_err = this.globalThis.createErrorInstance("{s}", .{err.message}); - js_err.put(this.globalThis, ZigString.static("code"), ZigString.init(err.code).toJS(this.globalThis)); - this.promise.rejectOnNextTick(this.globalThis, js_err); + this.promise.rejectOnNextTick(this.globalThis, err.toJS(this.globalThis)); }, } } @@ -4082,9 +4074,7 @@ pub const Blob = struct { this.promise.resolve(globalThis, .true); }, .failure => |err| { - const js_err = globalThis.createErrorInstance("{s}", .{err.message}); - js_err.put(globalThis, ZigString.static("code"), ZigString.init(err.code).toJS(globalThis)); - this.promise.rejectOnNextTick(globalThis, js_err); + this.promise.rejectOnNextTick(globalThis, err.toJS(globalThis)); }, } } @@ -4103,9 +4093,7 @@ pub const Blob = struct { this.promise.resolve(globalThis, JSValue.jsNumber(stat.size)); }, .failure => |err| { - const js_err = globalThis.createErrorInstance("{s}", .{err.message}); - js_err.put(globalThis, ZigString.static("code"), ZigString.init(err.code).toJS(globalThis)); - this.promise.rejectOnNextTick(globalThis, js_err); + this.promise.rejectOnNextTick(globalThis, err.toJS(globalThis)); }, } } diff --git a/src/s3.zig b/src/s3.zig index 7bf438808e6f8f..d85ae1818e8915 100644 --- a/src/s3.zig +++ b/src/s3.zig @@ -459,6 +459,12 @@ pub const AWSCredentials = struct { pub const S3Error = struct { code: []const u8, message: []const u8, + + pub fn toJS(err: *const @This(), globalObject: *JSC.JSGlobalObject) JSC.JSValue { + const js_err = globalObject.createErrorInstance("{s}", .{err.message}); + js_err.put(globalObject, JSC.ZigString.static("code"), JSC.ZigString.init(err.code).toJS(globalObject)); + return js_err; + } }; pub const S3StatResult = union(enum) { success: struct { @@ -1147,11 +1153,9 @@ pub const AWSCredentials = struct { if (readable.ptr == .Bytes) { const globalThis = this.readable_stream_ref.globalThis().?; if (request_err) |err| { - const js_err = globalThis.createErrorInstance("{s}", .{err.message}); - js_err.put(globalThis, JSC.ZigString.static("code"), JSC.ZigString.init(err.code).toJS(globalThis)); readable.ptr.Bytes.onData( .{ - .err = .{ .JSValue = js_err }, + .err = .{ .JSValue = err.toJS(globalThis) }, }, bun.default_allocator, ); @@ -1221,9 +1225,7 @@ pub const AWSCredentials = struct { sink.abort(); return; } - const js_err = globalObject.createErrorInstance("{s}", .{err.message}); - js_err.put(globalObject, JSC.ZigString.static("code"), JSC.ZigString.init(err.code).toJS(globalObject)); - sink.endPromise.rejectOnNextTick(globalObject, js_err); + sink.endPromise.rejectOnNextTick(globalObject, err.toJS(globalObject)); }, } } @@ -1406,9 +1408,7 @@ pub const AWSCredentials = struct { sink.abort(); return; } - const js_err = globalObject.createErrorInstance("{s}", .{err.message}); - js_err.put(globalObject, JSC.ZigString.static("code"), JSC.ZigString.init(err.code).toJS(globalObject)); - sink.endPromise.rejectOnNextTick(globalObject, js_err); + sink.endPromise.rejectOnNextTick(globalObject, err.toJS(globalObject)); }, } }