diff --git a/integration/batching-with-context-esModuleInterop/batching.ts b/integration/batching-with-context-esModuleInterop/batching.ts index fc7c79a77..a806c090e 100644 --- a/integration/batching-with-context-esModuleInterop/batching.ts +++ b/integration/batching-with-context-esModuleInterop/batching.ts @@ -779,5 +779,5 @@ function isSet(value: any): boolean { } function fail(message?: string): never { - throw new Error(message ?? "Failed"); + throw new globalThis.Error(message ?? "Failed"); } diff --git a/integration/batching-with-context/batching.ts b/integration/batching-with-context/batching.ts index b99cecca2..b698224cd 100644 --- a/integration/batching-with-context/batching.ts +++ b/integration/batching-with-context/batching.ts @@ -779,5 +779,5 @@ function isSet(value: any): boolean { } function fail(message?: string): never { - throw new Error(message ?? "Failed"); + throw new globalThis.Error(message ?? "Failed"); } diff --git a/integration/extensions/test.ts b/integration/extensions/test.ts index f0ac4bfdd..e114ff2e7 100644 --- a/integration/extensions/test.ts +++ b/integration/extensions/test.ts @@ -625,5 +625,5 @@ export interface Extension { } function fail(message?: string): never { - throw new Error(message ?? "Failed"); + throw new globalThis.Error(message ?? "Failed"); } diff --git a/integration/grpc-js/google/protobuf/struct.ts b/integration/grpc-js/google/protobuf/struct.ts index 3a8e3f4b9..5760ebc59 100644 --- a/integration/grpc-js/google/protobuf/struct.ts +++ b/integration/grpc-js/google/protobuf/struct.ts @@ -432,7 +432,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/map-bigint-optional/test.ts b/integration/map-bigint-optional/test.ts index 5a7977640..527097d04 100644 --- a/integration/map-bigint-optional/test.ts +++ b/integration/map-bigint-optional/test.ts @@ -131,13 +131,13 @@ export const MapBigInt_MapEntry = { encode(message: MapBigInt_MapEntry, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { if (message.key !== BigInt("0")) { if (BigInt.asUintN(64, message.key) !== message.key) { - throw new Error("value provided for field message.key of type fixed64 too large"); + throw new globalThis.Error("value provided for field message.key of type fixed64 too large"); } writer.uint32(9).fixed64(message.key.toString()); } if (message.value !== BigInt("0")) { if (BigInt.asIntN(64, message.value) !== message.value) { - throw new Error("value provided for field message.value of type int64 too large"); + throw new globalThis.Error("value provided for field message.value of type int64 too large"); } writer.uint32(16).int64(message.value.toString()); } diff --git a/integration/nestjs-simple/google/protobuf/struct.ts b/integration/nestjs-simple/google/protobuf/struct.ts index 957649d66..a3485c05d 100644 --- a/integration/nestjs-simple/google/protobuf/struct.ts +++ b/integration/nestjs-simple/google/protobuf/struct.ts @@ -126,7 +126,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = Struct.wrap(value); } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new gt.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/nice-grpc/google/protobuf/struct.ts b/integration/nice-grpc/google/protobuf/struct.ts index 1c9ada703..c14e39613 100644 --- a/integration/nice-grpc/google/protobuf/struct.ts +++ b/integration/nice-grpc/google/protobuf/struct.ts @@ -432,7 +432,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/oneof-unions-snake/google/protobuf/struct.ts b/integration/oneof-unions-snake/google/protobuf/struct.ts index 1a528b610..4208674b9 100644 --- a/integration/oneof-unions-snake/google/protobuf/struct.ts +++ b/integration/oneof-unions-snake/google/protobuf/struct.ts @@ -453,7 +453,7 @@ export const Value = { } else if (typeof value === "object") { result.kind = { $case: "struct_value", struct_value: value }; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/oneof-unions/google/protobuf/struct.ts b/integration/oneof-unions/google/protobuf/struct.ts index 93f15fe19..e7ee042b7 100644 --- a/integration/oneof-unions/google/protobuf/struct.ts +++ b/integration/oneof-unions/google/protobuf/struct.ts @@ -444,7 +444,7 @@ export const Value = { } else if (typeof value === "object") { result.kind = { $case: "structValue", structValue: value }; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/simple-long-bigint/google/protobuf/timestamp.ts b/integration/simple-long-bigint/google/protobuf/timestamp.ts index f005cadf4..8ddcf6b68 100644 --- a/integration/simple-long-bigint/google/protobuf/timestamp.ts +++ b/integration/simple-long-bigint/google/protobuf/timestamp.ts @@ -119,7 +119,7 @@ export const Timestamp = { encode(message: Timestamp, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { if (message.seconds !== BigInt("0")) { if (BigInt.asIntN(64, message.seconds) !== message.seconds) { - throw new Error("value provided for field message.seconds of type int64 too large"); + throw new globalThis.Error("value provided for field message.seconds of type int64 too large"); } writer.uint32(8).int64(message.seconds.toString()); } diff --git a/integration/simple-long-bigint/google/protobuf/wrappers.ts b/integration/simple-long-bigint/google/protobuf/wrappers.ts index c1d6b8f8a..b89f02f15 100644 --- a/integration/simple-long-bigint/google/protobuf/wrappers.ts +++ b/integration/simple-long-bigint/google/protobuf/wrappers.ts @@ -216,7 +216,7 @@ export const Int64Value = { encode(message: Int64Value, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { if (message.value !== BigInt("0")) { if (BigInt.asIntN(64, message.value) !== message.value) { - throw new Error("value provided for field message.value of type int64 too large"); + throw new globalThis.Error("value provided for field message.value of type int64 too large"); } writer.uint32(8).int64(message.value.toString()); } @@ -276,7 +276,7 @@ export const UInt64Value = { encode(message: UInt64Value, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { if (message.value !== BigInt("0")) { if (BigInt.asUintN(64, message.value) !== message.value) { - throw new Error("value provided for field message.value of type uint64 too large"); + throw new globalThis.Error("value provided for field message.value of type uint64 too large"); } writer.uint32(8).uint64(message.value.toString()); } diff --git a/integration/simple-long-bigint/simple.ts b/integration/simple-long-bigint/simple.ts index c65d8cbf7..78af89d65 100644 --- a/integration/simple-long-bigint/simple.ts +++ b/integration/simple-long-bigint/simple.ts @@ -57,7 +57,7 @@ export const Numbers = { } if (message.int64 !== BigInt("0")) { if (BigInt.asIntN(64, message.int64) !== message.int64) { - throw new Error("value provided for field message.int64 of type int64 too large"); + throw new globalThis.Error("value provided for field message.int64 of type int64 too large"); } writer.uint32(32).int64(message.int64.toString()); } @@ -66,7 +66,7 @@ export const Numbers = { } if (message.uint64 !== BigInt("0")) { if (BigInt.asUintN(64, message.uint64) !== message.uint64) { - throw new Error("value provided for field message.uint64 of type uint64 too large"); + throw new globalThis.Error("value provided for field message.uint64 of type uint64 too large"); } writer.uint32(48).uint64(message.uint64.toString()); } @@ -75,7 +75,7 @@ export const Numbers = { } if (message.sint64 !== BigInt("0")) { if (BigInt.asIntN(64, message.sint64) !== message.sint64) { - throw new Error("value provided for field message.sint64 of type sint64 too large"); + throw new globalThis.Error("value provided for field message.sint64 of type sint64 too large"); } writer.uint32(64).sint64(message.sint64.toString()); } @@ -84,7 +84,7 @@ export const Numbers = { } if (message.fixed64 !== BigInt("0")) { if (BigInt.asUintN(64, message.fixed64) !== message.fixed64) { - throw new Error("value provided for field message.fixed64 of type fixed64 too large"); + throw new globalThis.Error("value provided for field message.fixed64 of type fixed64 too large"); } writer.uint32(81).fixed64(message.fixed64.toString()); } @@ -93,7 +93,7 @@ export const Numbers = { } if (message.sfixed64 !== BigInt("0")) { if (BigInt.asIntN(64, message.sfixed64) !== message.sfixed64) { - throw new Error("value provided for field message.sfixed64 of type sfixed64 too large"); + throw new globalThis.Error("value provided for field message.sfixed64 of type sfixed64 too large"); } writer.uint32(97).sfixed64(message.sfixed64.toString()); } @@ -106,7 +106,7 @@ export const Numbers = { writer.uint32(122).fork(); for (const v of message.uint64s) { if (BigInt.asUintN(64, v) !== v) { - throw new Error("a value provided in array field uint64s of type uint64 is too large"); + throw new globalThis.Error("a value provided in array field uint64s of type uint64 is too large"); } writer.uint64(v.toString()); } diff --git a/integration/simple-snake/google/protobuf/struct.ts b/integration/simple-snake/google/protobuf/struct.ts index c9995ba7e..5dc64396f 100644 --- a/integration/simple-snake/google/protobuf/struct.ts +++ b/integration/simple-snake/google/protobuf/struct.ts @@ -432,7 +432,7 @@ export const Value = { } else if (typeof value === "object") { result.struct_value = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/simple-string-enums/google/protobuf/struct.ts b/integration/simple-string-enums/google/protobuf/struct.ts index 6e9861e1e..e866f6505 100644 --- a/integration/simple-string-enums/google/protobuf/struct.ts +++ b/integration/simple-string-enums/google/protobuf/struct.ts @@ -442,7 +442,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/static-only-type-registry/google/protobuf/struct.ts b/integration/static-only-type-registry/google/protobuf/struct.ts index ca3db4fc0..6d44332d0 100644 --- a/integration/static-only-type-registry/google/protobuf/struct.ts +++ b/integration/static-only-type-registry/google/protobuf/struct.ts @@ -443,7 +443,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/static-only/google/protobuf/struct.ts b/integration/static-only/google/protobuf/struct.ts index 404116da3..4831a6f92 100644 --- a/integration/static-only/google/protobuf/struct.ts +++ b/integration/static-only/google/protobuf/struct.ts @@ -438,7 +438,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/struct/google/protobuf/struct.ts b/integration/struct/google/protobuf/struct.ts index 3a8e3f4b9..5760ebc59 100644 --- a/integration/struct/google/protobuf/struct.ts +++ b/integration/struct/google/protobuf/struct.ts @@ -432,7 +432,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/type-annotations/google/protobuf/struct.ts b/integration/type-annotations/google/protobuf/struct.ts index 3679b516c..e1cd21542 100644 --- a/integration/type-annotations/google/protobuf/struct.ts +++ b/integration/type-annotations/google/protobuf/struct.ts @@ -449,7 +449,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/type-registry/google/protobuf/struct.ts b/integration/type-registry/google/protobuf/struct.ts index 9c6151278..122718b12 100644 --- a/integration/type-registry/google/protobuf/struct.ts +++ b/integration/type-registry/google/protobuf/struct.ts @@ -454,7 +454,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/use-map-type/google/protobuf/struct.ts b/integration/use-map-type/google/protobuf/struct.ts index 4309baf71..b5294734a 100644 --- a/integration/use-map-type/google/protobuf/struct.ts +++ b/integration/use-map-type/google/protobuf/struct.ts @@ -427,7 +427,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/use-numeric-enum-json/google/protobuf/struct.ts b/integration/use-numeric-enum-json/google/protobuf/struct.ts index ab56a6b9b..276b617e8 100644 --- a/integration/use-numeric-enum-json/google/protobuf/struct.ts +++ b/integration/use-numeric-enum-json/google/protobuf/struct.ts @@ -432,7 +432,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/use-readonly-types/google/protobuf/struct.ts b/integration/use-readonly-types/google/protobuf/struct.ts index 93299eb71..1c1c45bca 100644 --- a/integration/use-readonly-types/google/protobuf/struct.ts +++ b/integration/use-readonly-types/google/protobuf/struct.ts @@ -444,7 +444,7 @@ export const Value = { } else if (typeof value === "object") { result.kind = { $case: "structValue", structValue: value }; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/integration/value/google/protobuf/struct.ts b/integration/value/google/protobuf/struct.ts index 3a8e3f4b9..5760ebc59 100644 --- a/integration/value/google/protobuf/struct.ts +++ b/integration/value/google/protobuf/struct.ts @@ -432,7 +432,7 @@ export const Value = { } else if (typeof value === "object") { result.structValue = value; } else if (typeof value !== "undefined") { - throw new Error("Unsupported any value type: " + typeof value); + throw new globalThis.Error("Unsupported any value type: " + typeof value); } return result; }, diff --git a/src/generate-struct-wrappers.ts b/src/generate-struct-wrappers.ts index 7a55ae34d..bcf65d4d9 100644 --- a/src/generate-struct-wrappers.ts +++ b/src/generate-struct-wrappers.ts @@ -62,7 +62,7 @@ export function generateWrapDeep(ctx: Context, fullProtoTypeName: string, fieldN } else if (typeof value === 'object') { result.${fieldNames.structValue} = Struct.wrap(value); } else if (typeof value !== 'undefined') { - throw new Error('Unsupported any value type: ' + typeof value); + throw new ${ctx.utils.globalThis}.Error('Unsupported any value type: ' + typeof value); } return result; }`); @@ -196,7 +196,7 @@ export function generateWrapShallow(ctx: Context, fullProtoTypeName: string, fie } else if (typeof value === 'object') { result.kind = {$case: '${fieldNames.structValue}', ${fieldNames.structValue}: value}; } else if (typeof value !== 'undefined') { - throw new Error('Unsupported any value type: ' + typeof value); + throw new ${ctx.utils.globalThis}.Error('Unsupported any value type: ' + typeof value); } return result; }`); @@ -216,7 +216,7 @@ export function generateWrapShallow(ctx: Context, fullProtoTypeName: string, fie } else if (typeof value === 'object') { result.${fieldNames.structValue} = value; } else if (typeof value !== 'undefined') { - throw new Error('Unsupported any value type: ' + typeof value); + throw new ${ctx.utils.globalThis}.Error('Unsupported any value type: ' + typeof value); } return result; }`); diff --git a/src/main.ts b/src/main.ts index 9dd0ad900..c771130aa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -430,7 +430,7 @@ export function makeUtils(options: Options): Utils { ...makeNiceGrpcServerStreamingMethodResult(), ...makeGrpcWebErrorClass(bytes), ...makeExtensionClass(options), - ...makeAssertionUtils(), + ...makeAssertionUtils(bytes), }; } @@ -869,12 +869,12 @@ function makeExtensionClass(options: Options) { return { Extension }; } -function makeAssertionUtils() { +function makeAssertionUtils(bytes: ReturnType) { const fail = conditionalOutput( "fail", code` function fail(message?: string): never { - throw new Error(message ?? "Failed"); + throw new ${bytes.globalThis}.Error(message ?? "Failed"); } `, ); @@ -1298,13 +1298,13 @@ function getEncodeWriteSnippet(ctx: Context, field: FieldDescriptorProto): (plac case "sint64": case "sfixed64": return (place) => code`if (BigInt.asIntN(64, ${place}) !== ${place}) { - throw new Error('value provided for field ${place} of type ${fieldType} too large'); + throw new ${utils.globalThis}.Error('value provided for field ${place} of type ${fieldType} too large'); } writer.uint32(${tag}).${toReaderCall(field)}(${place}.toString())`; case "uint64": case "fixed64": return (place) => code`if (BigInt.asUintN(64, ${place}) !== ${place}) { - throw new Error('value provided for field ${place} of type ${fieldType} too large'); + throw new ${utils.globalThis}.Error('value provided for field ${place} of type ${fieldType} too large'); } writer.uint32(${tag}).${toReaderCall(field)}(${place}.toString())`; default: @@ -1474,7 +1474,9 @@ function generateEncode(ctx: Context, fullName: string, messageDesc: DescriptorP writer.uint32(${tag}).fork(); for (const v of ${messageProperty}) { if (BigInt.asIntN(64, v) !== v) { - throw new Error('a value provided in array field ${fieldName} of type ${fieldType} is too large'); + throw new ${ + utils.globalThis + }.Error('a value provided in array field ${fieldName} of type ${fieldType} is too large'); } writer.${toReaderCall(field)}(${rhs("v")}); } @@ -1487,7 +1489,9 @@ function generateEncode(ctx: Context, fullName: string, messageDesc: DescriptorP writer.uint32(${tag}).fork(); for (const v of ${messageProperty}) { if (BigInt.asUintN(64, v) !== v) { - throw new Error('a value provided in array field ${fieldName} of type ${fieldType} is too large'); + throw new ${ + utils.globalThis + }.Error('a value provided in array field ${fieldName} of type ${fieldType} is too large'); } writer.${toReaderCall(field)}(${rhs("v")}); }