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

axios capture response message #4377

Closed
chr4ss12 opened this issue Jan 7, 2022 · 5 comments
Closed

axios capture response message #4377

chr4ss12 opened this issue Jan 7, 2022 · 5 comments

Comments

@chr4ss12
Copy link

chr4ss12 commented Jan 7, 2022

hi,

in case of axios if you do request and axios returns 400, the error logged by Sentry contains minimum amount of data which in most cases is not useful. It just says "request failed with response 400", but there is actually no response data why it failed.

What is the preferred way/place to make Sentry capture more information in case of AxiosError?

this is what I came up with currently:

if (axios.isAxiosError(ex)) {
    ex.message = ex.message + (JSON.stringify(ex.response?.data, null, 4))
}

Sentry.captureException(ex, scope1);

that seems to be alright, although not sure if it is the correct way. Maybe a way to modify ExtraErrorData and pass some kind of extra function that allows me to check if error is AxiosError and then provide the fields I am interested in?

Or is using context more appropriate?

  • I am using axios 0.21.1
  • I am using ExtraErrorData
  • "@sentry/integrations": "^6.11.0",
    
    "@sentry/react-native": "^2.6.2",
@AbhiPrasad
Copy link
Member

We pass in the originalException in the hint, so you can use the hint + beforeSend to update events accordingly. See our docs on event hints.

You can also override the enhanceEventWithErrorData method on ExtraErrorData to add any context you find appropriate.

public enhanceEventWithErrorData(event: Event, hint?: EventHint): Event {

@github-actions
Copy link
Contributor

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@mljlynch
Copy link

mljlynch commented Oct 7, 2022

@AbhiPrasad it would be helpful if your docs had a more thorough description of handling this as axios is a very commonly used library.

@Freundschaft
Copy link

beforeSend(event, hint) {
          if(hint && hint.originalException && hint.originalException.isAxiosError) {
            if (hint.originalException.response && hint.originalException.response.data) {
              let contexts = {
                ...event.contexts
              };
              contexts.errorResponse = {
                data: hint.originalException.response.data
              };
              event.contexts = contexts;
            }
          }
          
          return event;
        },

did this the trick for me.

@nbolton
Copy link

nbolton commented Mar 25, 2024

My solution, based on @Freundschaft's code:

import { AxiosError, isAxiosError } from "axios";

Sentry.init({
  //...
  beforeSend: handleSentryBeforeSend
});

function handleSentryBeforeSend(event: Event, hint: EventHint) {
  addAxiosContextRecursive(event, hint?.originalException);
}

function addAxiosContextRecursive(event: Event, error: unknown) {
  if (isAxiosError(error)) {
    addAxiosContext(event, error);
  } else if (error instanceof Error && error.cause) {
    addAxiosContextRecursive(event, error.cause);
  }
}

function addAxiosContext(event: Event, error: AxiosError) {
  if (error.response) {
    const contexts = { ...event.contexts };
    contexts.Axios = { Response: error.response };
    event.contexts = contexts;
  }
}

This works in TypeScript and it also handles recursive errors (i.e. when error.cause is set).

Screenshot from 2024-03-25 11-07-22

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

No branches or pull requests

6 participants