From 4d7008e028633735ebd4d2b9468241a501971122 Mon Sep 17 00:00:00 2001 From: Frederik Rothenberger Date: Thu, 25 Apr 2024 18:41:44 +0200 Subject: [PATCH] chore: Add context to errors thrown on cbor decode (#874) We have the issue in Internet Identity that people complain about hard to diagnose issues. In particular, we regularly encounter users faced with the "failed to parse" message thrown out of `borc`. It would be very helpful to know what the input was that caused that error. --- docs/CHANGELOG.md | 1 + packages/agent/src/cbor.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a9778ac15..e006e21fa 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 e3dd16581..21c36feff 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)}`); + } }