From cff9e1055e003e1e71a4b8d794282c86418c4c27 Mon Sep 17 00:00:00 2001 From: Aaron Cox Date: Tue, 25 Jun 2024 09:41:26 -0700 Subject: [PATCH] Adding codingPath to serializer errors (#95) --- src/serializer/decoder.ts | 6 +++++- src/serializer/encoder.ts | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/serializer/decoder.ts b/src/serializer/decoder.ts index 89e30679..a8b0bc2f 100644 --- a/src/serializer/decoder.ts +++ b/src/serializer/decoder.ts @@ -245,7 +245,11 @@ function decodeObject(value: any, type: ABI.ResolvedType, ctx: DecodingContext): return null } } - throw new Error(`Unexpectedly encountered ${value} for non-optional`) + throw new Error( + `Unexpectedly encountered ${value} for non-optional (${ctx.codingPath + .map((path) => path.field) + .join('.')})` + ) } else if (type.isArray) { if (!Array.isArray(value)) { throw new Error('Expected array') diff --git a/src/serializer/encoder.ts b/src/serializer/encoder.ts index f3590db4..d4c19bc2 100644 --- a/src/serializer/encoder.ts +++ b/src/serializer/encoder.ts @@ -179,7 +179,11 @@ export function encodeAny(value: any, type: ABI.ResolvedType, ctx: EncodingConte if (type.isExtension) { return } - throw new Error(`Found ${value} for non-optional type: ${type.typeName}`) + throw new Error( + `Found ${value} for non-optional type: ${type.typeName} (${ctx.codingPath + .map((path) => path.field) + .join('.')})` + ) } if (abiType && abiType.toABI) { // type explicitly handles encoding