func index(ctx context.Context, w http.ResponseWriter, r *http.Request) { err := fmt.Errorf("Error: %s", "hogehoge") sendError1Func := func(err error) { rollbar.RequestError(rollbar.ERR, r, fmt.Errorf("sendError1 : %s", err)) } sendError2Func := func(err error) { rollbar.RequestError(rollbar.ERR, r, err) } // そのままerr rollbar.RequestError(rollbar.ERR, r, err) // fmt.Errorfでラップ rollbar.RequestError(rollbar.ERR, r, fmt.Errorf("Error: %s", err.Error())) sendError1Func(err) sendError2Func(fmt.Errorf("sendError2 : %s", err)) panic("hogehoge") }
func panicSendAirbrake(ctx context.Context, req *http.Request) { exception := kami.Exception(ctx) log.Println("ERROR:", exception) log.Println("ERROR:", string(debug.Stack())) defer func() { if err := recover(); err != nil { log.Println("ERROR:", err) } }() rollbar.RequestError(rollbar.ERR, req, fmt.Errorf("%s", exception)) }
// This method collects all errors and submits them to Rollbar func Errors(env, token string, logger service.Logger) gin.HandlerFunc { rollbar.Environment = env rollbar.Token = token return func(c *gin.Context) { c.Next() // Only run if there are some errors to handle if len(c.Errors) > 0 { for _, e := range c.Errors { // Find out what type of error it is switch e.Type { case gin.ErrorTypePublic: // Only output public errors if nothing has been written yet if !c.Writer.Written() { c.JSON(c.Writer.Status(), gin.H{"Error": e.Error()}) } case gin.ErrorTypeBind: errs := e.Err.(*validator.StructErrors) list := make(map[string]string) for field, err := range errs.Errors { list[field] = ValidationErrorToText(err) } // Make sure we maintain the preset response status status := http.StatusBadRequest if c.Writer.Status() != http.StatusOK { status = c.Writer.Status() } c.JSON(status, gin.H{"Errors": list}) default: // Log all other errors rollbar.RequestError(rollbar.ERR, c.Request, e.Err) if logger != nil { logger.Error(e.Err) } } } // If there was no public or bind error, display default 500 message if !c.Writer.Written() { c.JSON(http.StatusInternalServerError, gin.H{"Error": ErrorInternalError.Error()}) } } } }