Files
fn-serverless/api/server/error_response.go
2017-07-19 13:44:26 -07:00

34 lines
981 B
Go

package server
import (
"errors"
"net/http"
"runtime/debug"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"gitlab-odx.oracle.com/odx/functions/api/models"
"gitlab-odx.oracle.com/odx/functions/api/runner/common"
)
// ErrInternalServerError returned when something exceptional happens.
var ErrInternalServerError = errors.New("internal server error")
func simpleError(err error) *models.Error {
return &models.Error{Error: &models.ErrorBody{Message: err.Error()}}
}
func handleErrorResponse(c *gin.Context, err error) {
log := common.Logger(c.Request.Context())
switch e := err.(type) {
case models.APIError:
if e.Code() >= 500 {
log.WithFields(logrus.Fields{"code": e.Code()}).WithError(e).Error("api error")
}
c.JSON(e.Code(), simpleError(e))
default:
log.WithError(err).WithFields(logrus.Fields{"stack": string(debug.Stack())}).Error("internal server error")
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
}
}