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(core): Improve error formatting in ZodErrors integration #15111

Merged
merged 5 commits into from
Jan 23, 2025

Conversation

jahands
Copy link
Contributor

@jahands jahands commented Jan 21, 2025

  • Include full key path rather than the top level key in title
  • Improve message for validation issues with no path
  • Add option to include extended issue information as an attachment

Background

I tried using this integration, but found the error format in Sentry to be lacking.
I added tests to demonstrate how this will format issues.

Screenshots

top-level-object nested-keys attachments

Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

"sideEffects": false
"sideEffects": false,
"devDependencies": {
"zod": "^3.24.1"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wasn't sure if adding deps was okay, but it made adding tests a lot easier

Copy link
Member

Choose a reason for hiding this comment

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

This is fine for testing

@jahands jahands changed the title feat(core): Improve error formatting in ZodErrors integration feat(integrations): Improve error formatting in ZodErrors integration Jan 21, 2025
- Include full key path rather than the top level key in title
- Improve message for validation issues with no path
- Add option to include extended issue information as an attachment
@jahands jahands force-pushed the zod-sentry-improvements branch from faa1518 to 06232f3 Compare January 21, 2025 15:03
@AbhiPrasad
Copy link
Member

@jahands thanks for the PR! could you share a link to a sentry event / screenshot of a sentry event that shows the changes from this PR?

@jahands
Copy link
Contributor Author

jahands commented Jan 21, 2025

@AbhiPrasad done!

@AbhiPrasad AbhiPrasad requested review from AbhiPrasad, a team and chargome and removed request for a team January 21, 2025 17:29
@AbhiPrasad AbhiPrasad self-assigned this Jan 21, 2025
@AbhiPrasad
Copy link
Member

Assigning myself so I can help review and merge this in! On my todo to review tomorrow morning :)

@AbhiPrasad AbhiPrasad changed the title feat(integrations): Improve error formatting in ZodErrors integration feat(core): Improve error formatting in ZodErrors integration Jan 22, 2025
packages/core/src/integrations/zoderrors.ts Outdated Show resolved Hide resolved
"sideEffects": false
"sideEffects": false,
"devDependencies": {
"zod": "^3.24.1"
Copy link
Member

Choose a reason for hiding this comment

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

This is fine for testing

* Takes ZodError issue path array and returns a flattened version as a string.
* This makes it easier to display paths within a Sentry error message.
*
* Array indexes are normalized to reduce duplicate entries
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we want to duplicate entries here, I think [0].foo.[1].bar is more valuable than <array>.foo.<array>.bar, and we want to make sure that different array entries will lead to different issues.

What do you think?

Copy link
Contributor Author

@jahands jahands Jan 23, 2025

Choose a reason for hiding this comment

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

This is only applied to the issue message, not event.extra or attachments which still include all unique issues with the full path including indexes.

In practice, we found that having the indexes in the message is more noise than helpful for understanding the issue at a glance, which was the primary motivation for me making this improvement in our internal fork.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me, let's keep what is here then

packages/core/src/integrations/zoderrors.ts Outdated Show resolved Hide resolved
packages/core/src/integrations/zoderrors.ts Outdated Show resolved Hide resolved
@AbhiPrasad
Copy link
Member

Hmm tests seem to be failing because the attachment doesn't match the issues being sent anymore. I wonder why 🤔

 FAIL  test/lib/integrations/zoderrrors.test.ts
  ● applyZodErrorsToEvent() › should add all ZodError issues as attachment

    expect(received).toMatchInlineSnapshot(snapshot)

    Snapshot name: `applyZodErrorsToEvent() should add all ZodError issues as attachment 1`

    - Snapshot  - 8
    + Received  + 0

    @@ -6,15 +6,7 @@
            "keys": "[\"extra\"]",
            "message": "Invalid input: expected string, received number",
            "path": "names.1",
            "received": "number",
          },
    -     Object {
    -       "code": "invalid_type",
    -       "expected": "string",
    -       "keys": "[\"extra2\"]",
    -       "message": "Invalid input: expected string, received number",
    -       "path": "foo.1",
    -       "received": "number",
    -     },
        ],
      }

      168 |     }
      169 |     expect(attachment.filename).toBe('zod_issues.json');
    > 170 |     expect(JSON.parse(attachment.data.toString())).toMatchInlineSnapshot(`
          |                                                    ^
      171 | Object {
      172 |   "issues": Array [
      173 |     Object {

      at Object.<anonymous> (test/lib/integrations/zoderrrors.test.ts:170:52)

@jahands
Copy link
Contributor Author

jahands commented Jan 23, 2025

Hmm tests seem to be failing because the attachment doesn't match the issues being sent anymore. I wonder why 🤔

 FAIL  test/lib/integrations/zoderrrors.test.ts
  ● applyZodErrorsToEvent() › should add all ZodError issues as attachment

    expect(received).toMatchInlineSnapshot(snapshot)

    Snapshot name: `applyZodErrorsToEvent() should add all ZodError issues as attachment 1`

    - Snapshot  - 8
    + Received  + 0

    @@ -6,15 +6,7 @@
            "keys": "[\"extra\"]",
            "message": "Invalid input: expected string, received number",
            "path": "names.1",
            "received": "number",
          },
    -     Object {
    -       "code": "invalid_type",
    -       "expected": "string",
    -       "keys": "[\"extra2\"]",
    -       "message": "Invalid input: expected string, received number",
    -       "path": "foo.1",
    -       "received": "number",
    -     },
        ],
      }

      168 |     }
      169 |     expect(attachment.filename).toBe('zod_issues.json');
    > 170 |     expect(JSON.parse(attachment.data.toString())).toMatchInlineSnapshot(`
          |                                                    ^
      171 | Object {
      172 |   "issues": Array [
      173 |     Object {

      at Object.<anonymous> (test/lib/integrations/zoderrrors.test.ts:170:52)

Oh I see, we need to skip slicing the list of issues before flattening if save attachment is enabled so that the full list is included in the attachment. Will fix

@jahands
Copy link
Contributor Author

jahands commented Jan 23, 2025

@AbhiPrasad fixed that bug, tests pass for me locally now

@AbhiPrasad AbhiPrasad merged commit 965185c into getsentry:develop Jan 23, 2025
131 checks passed
AbhiPrasad added a commit that referenced this pull request Jan 23, 2025
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #15111

Co-authored-by: Abhijeet Prasad <aprasad@sentry.io>
AbhiPrasad pushed a commit that referenced this pull request Jan 24, 2025
- Include full key path rather than the top level key in title
- Improve message for validation issues with no path
- Add option to include extended issue information as an attachment
AbhiPrasad pushed a commit that referenced this pull request Jan 24, 2025
- Include full key path rather than the top level key in title
- Improve message for validation issues with no path
- Add option to include extended issue information as an attachment
jahands added a commit to jahands/toucan-js that referenced this pull request Feb 11, 2025
- Adds improvements based on feedback I got while PR'ing this to
sentry-javascript:
getsentry/sentry-javascript#15111
- Exports zodErrorsIntegration in the root index.ts (missed this in the
original PR)
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.

2 participants