mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
"gitlab-odx.oracle.com/odx/functions/api/models"
|
|
"gitlab-odx.oracle.com/odx/functions/api/runner/common"
|
|
"net/http"
|
|
)
|
|
|
|
var ErrInternalServerError = errors.New("Something unexpected happened on the server")
|
|
|
|
func simpleError(err error) *models.Error {
|
|
return &models.Error{Error: &models.ErrorBody{Message: err.Error()}}
|
|
}
|
|
|
|
var errStatusCode = map[error]int{
|
|
models.ErrAppsNotFound: http.StatusNotFound,
|
|
models.ErrAppsAlreadyExists: http.StatusConflict,
|
|
models.ErrRoutesNotFound: http.StatusNotFound,
|
|
models.ErrRoutesAlreadyExists: http.StatusConflict,
|
|
models.ErrCallNotFound: http.StatusNotFound,
|
|
models.ErrCallLogNotFound: http.StatusNotFound,
|
|
}
|
|
|
|
func handleErrorResponse(c *gin.Context, err error) {
|
|
ctx := c.MustGet("ctx").(context.Context)
|
|
log := common.Logger(ctx)
|
|
log.Error(err)
|
|
|
|
if code, ok := errStatusCode[err]; ok {
|
|
c.JSON(code, simpleError(err))
|
|
} else {
|
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
|
}
|
|
}
|