github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/server/error_response.go (about) 1 package server 2 3 import ( 4 "context" 5 "errors" 6 "github.com/gin-gonic/gin" 7 "github.com/iron-io/functions/api/models" 8 "github.com/iron-io/runner/common" 9 "net/http" 10 ) 11 12 var ErrInternalServerError = errors.New("Something unexpected happened on the server") 13 14 func simpleError(err error) *models.Error { 15 return &models.Error{Error: &models.ErrorBody{Message: err.Error()}} 16 } 17 18 var errStatusCode = map[error]int{ 19 models.ErrAppsNotFound: http.StatusNotFound, 20 models.ErrAppsAlreadyExists: http.StatusConflict, 21 models.ErrRoutesNotFound: http.StatusNotFound, 22 models.ErrRoutesAlreadyExists: http.StatusConflict, 23 } 24 25 func handleErrorResponse(c *gin.Context, err error) { 26 ctx := c.MustGet("ctx").(context.Context) 27 log := common.Logger(ctx) 28 log.Error(err) 29 30 if code, ok := errStatusCode[err]; ok { 31 c.JSON(code, simpleError(err)) 32 } else { 33 c.JSON(http.StatusInternalServerError, simpleError(err)) 34 } 35 }