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:
Pedro Nasser
2017-01-26 20:41:18 -02:00
committed by Travis Reeder
parent 5a91710dbf
commit a80fe9c897
15 changed files with 65 additions and 100 deletions

View 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))
}
}