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

Conversation

frederikrothenberger
Copy link
Contributor

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.

How Has This Been Tested?

Against existing test suite.

Checklist:

  • My changes follow the guidelines in CONTRIBUTING.md.
  • The title of this PR complies with Conventional Commits.
  • I have edited the CHANGELOG accordingly.
  • I have made corresponding changes to the documentation.

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.
@frederikrothenberger frederikrothenberger requested a review from a team as a code owner April 25, 2024 09:17
@krpeacock krpeacock merged commit 4d7008e into dfinity:main Apr 25, 2024
14 of 15 checks passed
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.

@frederikrothenberger frederikrothenberger deleted the frederik/borc-errors branch April 26, 2024 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants