Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add context to errors thrown on cbor decode #874

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, aren't you gonna get something like Failed to decode CBOR: [object Object]... - i.e. e is always a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks. I think having the input: ... part is definitely an improvement though.
So, what's the simplest way of converting unknown to something readable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what's the best practice or if there is an elegant solution.

In Oisy I got such a parser:

export const errorDetailToString = (err: unknown): string | undefined =>
	typeof err === 'string'
		? (err as string)
		: err instanceof Error
			? (err as Error).message
			: 'message' in (err as unknown as { message: string })
				? (err as unknown as { message: string }).message
				: undefined;

Another option is maybe to create an error type that extends the error and bubble the new types with the information you need plus the original error. Just thinking out at loud though.

But, again, maybe e: unknown is actually always e: string, don't know, was just a curiosity question in case not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, again, maybe e: unknown is actually always e: string, don't know, was just a curiosity question in case not.

That is definitely not the case, because one of the errors we're expecting to catch here is this one. Thanks for pointing that out.

@krpeacock: is there a utility for that in agent-js somewhere already? Then I'll adjust it.

}
}
Loading