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

🤗 [Question]: Error Handler : Sentry Stack Trace Highlights hub.CaptureException Instead of Original Source #3103

Closed
3 tasks done
ImEins opened this issue Aug 17, 2024 · 2 comments

Comments

@ImEins
Copy link

ImEins commented Aug 17, 2024

Question Description

I am integrating Sentry into a Go application that uses Fiber and Fx for dependency injection. The issue I am facing is that the stack traces captured by Sentry consistently highlight the line where hub.CaptureException(err) is called, rather than the original source of the error. This makes it challenging to identify where the error originated.

Let's say I have some function that is going to return some error

tasks, err := tc.ts.ListByProjectID(c.Context(), projectID)
if err != nil {
  return &fiber.Error{
	  Code: fiber.ErrInternalServerError.Code,
  }
}

Code Snippet (optional)

func() *fiber.App {
  app := fiber.New(fiber.Config{
	  ErrorHandler: utils.NewErrorHandler(),
	  JSONEncoder:  json.Marshal,
	  JSONDecoder:  json.Unmarshal,
  })
  
  // Sentry Middleware
  sentryHandler := sentryfiber.New(sentryfiber.Options{
	  Repanic:         true,
	  WaitForDelivery: true,
  })
  
  app.Use(sentryHandler)
  
  return app
}

// utils/error.go
// NewErrorHandler returns a custom error handler for Fiber
func NewErrorHandler() fiber.ErrorHandler {
  return func(c *fiber.Ctx, err error) error {
    // Determine status code and message based on the error
    statusCode, message := getErrorDetails(err)
    
    // Capture error in Sentry
    if hub := sentryfiber.GetHubFromContext(c); hub != nil {
	    hub.WithScope(func(scope *sentry.Scope) {
		    scope.SetTag("status_code", fmt.Sprintf("%d", statusCode))
		    scope.SetTag("endpoint", c.Path())
    
		    // If the error is of type *fiber.Error, add the status code and message
		    if fiberErr, ok := err.(*fiber.Error); ok {
			    scope.SetTag("fiber_error_code", fmt.Sprintf("%d", fiberErr.Code))
			    scope.SetExtra("fiber_error_message", fiberErr.Message)
		    }
    
		    // Capture the exception
		    hub.CaptureException(err)
	    })
    }
    
    // Return a JSON response with the status code and message
    return c.Status(statusCode).JSON(GlobalErrorHandlerResp{
	    Success: false,
	    Message: message,
    })
  }
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
Copy link

welcome bot commented Aug 17, 2024

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ImEins ImEins closed this as completed Aug 23, 2024
@cyaxaress
Copy link

@ImEins as you closed this issue, did you find solution? may you please share it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants