diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a9778ac1..e006e21f 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,6 +5,7 @@ ### Changed - feat: make `IdbStorage` `get/set` methods generic +- chore: add context to errors thrown when failing to decode CBOR values. ## [1.2.0] - 2024-03-25 diff --git a/packages/agent/src/cbor.ts b/packages/agent/src/cbor.ts index e3dd1658..21c36fef 100644 --- a/packages/agent/src/cbor.ts +++ b/packages/agent/src/cbor.ts @@ -4,7 +4,7 @@ import borc from 'borc'; import * as cbor from 'simple-cbor'; import { CborEncoder, SelfDescribeCborSerializer } from 'simple-cbor'; import { Principal } from '@dfinity/principal'; -import { concat, fromHex } from './utils/buffer'; +import { concat, fromHex, toHex } from './utils/buffer'; // We are using hansl/simple-cbor for CBOR serialization, to avoid issues with // encoding the uint64 values that the HTTP handler of the client expects for @@ -125,5 +125,9 @@ export function decode(input: ArrayBuffer): T { }, }); - return decoder.decodeFirst(buffer); + try { + return decoder.decodeFirst(buffer); + } catch(e: unknown) { + throw new Error(`Failed to decode CBOR: ${e}, input: ${toHex(buffer)}`); + } }