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

feat: Preserve original messages during error serialization by default #158

Merged
merged 2 commits into from
Oct 8, 2024

Conversation

rekmarks
Copy link
Member

@rekmarks rekmarks commented Oct 7, 2024

This ensures that non-empty string error.message properties of serialized errors are preserved by default, even if the serialized error is not a valid JSON-RPC error. This behavior can be overridden by setting shouldPreserveMessage: false.

In #61, our error serialization logic was considerably improved. One of the behavioral changes made at the time was to always overwrite the message property with that of the fallback error (practically always the "internal JSON-RPC-error"), regardless of whether a non-empty string message was present on the original error object. We have yet to ship this everywhere in our stack, in part because such a change may be breaking for our consumers. By reverting to the old behavior for the message property only, we avoid these potential breakages and improve the accessibility of potentially useful information to consumers (i.e. directly in the error message as opposed to buried in error.data.cause.message).

@rekmarks rekmarks marked this pull request as ready for review October 7, 2024 23:13
@rekmarks rekmarks requested a review from a team as a code owner October 7, 2024 23:13
@rekmarks rekmarks requested review from FrederikBolding and removed request for adonesky1 and tommasini October 7, 2024 23:14
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 8, 2024
@legobeat
Copy link
Contributor

legobeat commented Oct 8, 2024

legobeat
legobeat previously approved these changes Oct 8, 2024
src/utils.ts Outdated
// If the error does not match the JsonRpcError type, use the fallback error, but try to include the original error as `cause`.
const cause = serializeCause(error);
const fallbackWithCause = {
...fallbackError,
...(originalMessage && { message: originalMessage }),
Copy link
Member

Choose a reason for hiding this comment

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

Can we add shouldPreserveMessage = true to the options in serializeError and only use this behavior if it is true?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done: cb3a6b4

I fixed a number of annoying formatting things with the errors, but only added one case for the new param.

@rekmarks rekmarks changed the title feat: Always preserve original messages during error serialization feat: Preserve original messages during error serialization by default Oct 8, 2024
Comment on lines +227 to +243
it('overwrites the original message with `shouldPreserveMessage: false`', () => {
const error = new Error('foo');
const result = serializeError(error, {
shouldPreserveMessage: false,
fallbackError: validError0,
});
expect(result).toStrictEqual({
code: validError0.code,
message: validError0.message,
data: {
cause: {
message: error.message,
stack: error.stack,
},
},
});
});
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the only addition to this file.

@rekmarks rekmarks merged commit 807a177 into main Oct 8, 2024
20 checks passed
@rekmarks rekmarks deleted the rekm/preserve-original-messages branch October 8, 2024 19:40
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 8, 2024
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 9, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 9, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 10, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 11, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 14, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 14, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 15, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 15, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 15, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
legobeat added a commit to legobeat/metamask-extension that referenced this pull request Oct 16, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
Gudahtt pushed a commit to legobeat/metamask-extension that referenced this pull request Oct 16, 2024
MetaMask/rpc-errors#158

- Update test according to new @metamask/rpc-errors behavior

This partially reverts commit aeca6d7.
Gudahtt pushed a commit to MetaMask/metamask-extension that referenced this pull request Oct 16, 2024
…edition) (#24496)

## **Description**

- Upgrade from obsolete `eth-rpc-errors` to `@metamask/rpc-errors`
  - This introduce handling of error causes 


See [here](MetaMask/rpc-errors#140) for some
context.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/24496?quickstart=1)

## **Related issues**

- #22871

#### Blocked by
- [x] MetaMask/rpc-errors#158
- [x] MetaMask/rpc-errors#144
  - [x] MetaMask/rpc-errors#140

#### Blocking
- #22875

## **Manual testing steps**

## **Screenshots/Recordings**

### **Before**

### **After**

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
Gudahtt pushed a commit to MetaMask/metamask-extension that referenced this pull request Oct 21, 2024
…edition) (#24496)

- Upgrade from obsolete `eth-rpc-errors` to `@metamask/rpc-errors`
  - This introduce handling of error causes

See [here](MetaMask/rpc-errors#140) for some
context.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/24496?quickstart=1)

- #22871

- [x] MetaMask/rpc-errors#158
- [x] MetaMask/rpc-errors#144
  - [x] MetaMask/rpc-errors#140

- #22875

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
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