Skip to content

Commit

Permalink
chore: Add context to errors thrown on cbor decode (#874)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Frederik Rothenberger authored Apr 25, 2024
1 parent a51bd5b commit 4d7008e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions packages/agent/src/cbor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -125,5 +125,9 @@ export function decode<T>(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)}`);
}
}

0 comments on commit 4d7008e

Please sign in to comment.