mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Fix datastore error for inexistent app (#493)
* fix datastore error inexistent app * fix get route error handling * fix API errors handling and tests
This commit is contained in:
committed by
Travis Reeder
parent
5a91710dbf
commit
a80fe9c897
35
api/server/error_response.go
Normal file
35
api/server/error_response.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/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,
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user