GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `setupKoaErrorHandler` doesn't preserve the error status code and message in the response · Issue #12517 · getsentry/sentry-javascript

setupKoaErrorHandler doesn't preserve the error status code and message in the response #12517

New issue
Copy link
New issue
Copy link
Closed
#12609
Closed
setupKoaErrorHandler doesn't preserve the error status code and message in the response#12517
#12609
Copy link
Assignees
chargome
Labels
Integration: koaIssues related to Koa support for the Sentry Node SDKMeta: Good First Issue

Description

@maximedupre
maximedupre
opened on Jun 17, 2024
Issue body actions

Is there an existing issue for this?

  • I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
  • I have reviewed the documentation https://docs.sentry.io/
  • I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

8.9.2

Framework Version

"@sentry/node": "^8.9.2"

Link to Sentry event

No response

SDK Setup

init({
    dsn: '___DSN',
    tracesSampleRate: 0.1,
    profilesSampleRate: 1.0,
    normalizeDepth: 11,
    integrations: [nodeProfilingIntegration],
    environment: EnvVariablesUtility.NODE_ENV,
    enabled: EnvVariablesUtility.NODE_ENV !== 'local',
    beforeSend(event, hint) {
        const error: any = hint.originalException;

        if (error && error.expose) {
            return null;
        }

        return event;
    },
});

Steps to Reproduce

My middleware:

ctx.status = 201;
ctx.assert(isEmail.default(email), 400, 'The email is invalid');

Expected Result

Status code to be 400 with body text 'The email is invalid'

Actual Result

Status code is 201, body text is 'Created'

However, if I place setupKoaErrorHandler after I have defined all my middlewares, I get

Status code 400, no body

Activity

  1. maximedupre
    added
    Type: Bug
    on Jun 17, 2024
  2. getsantry
    added
    Waiting for: Product Owner
    on Jun 17, 2024
  3. getsantry
    added this to GitHub Issues with 👀 3on Jun 17, 2024
  4. getsantry
    moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jun 17, 2024
  5. github-actions
    added
    Package: node
    on Jun 17, 2024
  6. maximedupre
    changed the title [-]`setupKoaErrorHandler` doesn't preserve the error status code and message[/-] [+]`setupKoaErrorHandler` doesn't preserve the error status code and message in the response[/+] on Jun 17, 2024
  7. AbhiPrasad commented on Jun 17, 2024

    @AbhiPrasad
    AbhiPrasad
    on Jun 17, 2024
    Contributor
    More actions

    Hey @maximedupre! The quick fix here is def to make sure we rethrow errors. Currently we swallow all errors, which makes ctx.assert and similar strategies break 😢.

    We also will add some filtering so that only 5xx errors are captured by Sentry, you can opt-in to capturing 3xx and 4xx errors if you want!

  8. getsantry
    removed
    Waiting for: Product Owner
    on Jun 17, 2024
  9. getsantry
    moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jun 17, 2024
  10. mydea
    added
    Meta: Good First Issue
    on Jun 18, 2024
  11. maximedupre commented on Jun 18, 2024

    @maximedupre
    maximedupre
    on Jun 18, 2024
    Author
    More actions

    Thanks for the response @AbhiPrasad.

    Currently we swallow all errors, which makes ctx.assert and similar strategies break 😢.

    ctx.assert, ctx.throw, or just throwing an error breaks. In other words, it's impossible to respond with an appropriate error response 😅

    Just to be clear, there is currently no workaround except removing setupKoaErrorHandler?

  12. getsantry
    added
    Waiting for: Product Owner
    on Jun 18, 2024
  13. getsantry
    moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jun 18, 2024
  14. AbhiPrasad commented on Jun 18, 2024

    @AbhiPrasad
    AbhiPrasad
    on Jun 18, 2024
    Contributor
    More actions

    Just to be clear, there is currently no workaround except removing setupKoaErrorHandler?

    You can implement your own by following https://github.com/koajs/koa/blob/master/docs/error-handling.md

  15. getsantry
    removed
    Waiting for: Product Owner
    on Jun 18, 2024
  16. getsantry
    moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jun 18, 2024
  17. maximedupre commented on Jun 18, 2024

    @maximedupre
    maximedupre
    on Jun 18, 2024
    Author
    More actions

    Not sure what you mean. I already have a custom error handler middleware:

    async (ctx: Koa.Context, next: Next) => {
        try {
            await next();
        } catch (e: any) {
            if (e.status === 400 || e.status === 401) {
                const error: any = createError(e.status, e.message);
                error.headers = ctx.response.header;
                error.expose = e.expose;
    
                throw error;
            } else {
                throw e;
            }
        }
    };
    

    Removing it or leaving it place has no effect, the sentry koa error handler doesn't consider the error's status code or message.

  18. getsantry
    added
    Waiting for: Product Owner
    on Jun 18, 2024
  19. getsantry
    moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jun 18, 2024
  20. AbhiPrasad commented on Jun 18, 2024

    @AbhiPrasad
    AbhiPrasad
    on Jun 18, 2024
    Contributor
    More actions

    Meaning you remove setupKoaErrorHandler and add a Sentry.captureException to your error handling middleware yourself.

    async (ctx: Koa.Context, next: Next) => {
      try {
        await next();
      } catch (e: any) {
        if (e.status === 400 || e.status === 401) {
          const error: any = createError(e.status, e.message);
          error.headers = ctx.response.header;
          error.expose = e.expose;
          throw error;
        } else {
    +     Sentry.captureException(error);
          throw e;
        }
      }
    };
    Reacted by Maxime Dupré
  21. getsantry
    removed
    Waiting for: Product Owner
    on Jun 18, 2024
  22. getsantry
    moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jun 18, 2024
  23. maximedupre
    closed this as completedon Jun 18, 2024
  24. maximedupre
    reopened this on Jun 18, 2024
  25. AbhiPrasad
    added
    Integration: koaIssues related to Koa support for the Sentry Node SDK
    on Jun 18, 2024
  26. chargome
    self-assigned this
    on Jun 21, 2024
  27. chargome
    closed this as completedin #12609on Jun 24, 2024
  28. chargome
    closed this as completedin 41c51e9on Jun 24, 2024
  29. ryan953
    added a commit that references this issue on Jun 24, 2024

    fix(node): Re-throw errors from koa middleware (#12609)

    This issue will close once commit 41c51e9 is merged into the 'develop' branch.
    41c51e9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @chargome
    chargome

Labels

Integration: koaIssues related to Koa support for the Sentry Node SDKMeta: Good First Issue

Projects

  • GitHub Issues with 👀 3
    Status
    No status
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.