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
@@ -42,13 +42,8 @@ func (s *Server) handleAppCreate(c *gin.Context) {
|
||||
}
|
||||
|
||||
app, err := s.Datastore.InsertApp(ctx, wapp.App)
|
||||
if err == models.ErrAppsAlreadyExists {
|
||||
log.WithError(err).Debug(models.ErrAppsCreate)
|
||||
c.JSON(http.StatusConflict, simpleError(err))
|
||||
return
|
||||
} else if err != nil {
|
||||
log.WithError(err).Error(models.ErrAppsCreate)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -37,13 +37,8 @@ func (s *Server) handleAppDelete(c *gin.Context) {
|
||||
}
|
||||
|
||||
err = s.Datastore.RemoveApp(ctx, app.Name)
|
||||
if err == models.ErrAppsNotFound {
|
||||
log.WithError(err).Debug(models.ErrAppsRemoving)
|
||||
c.JSON(http.StatusNotFound, simpleError(err))
|
||||
return
|
||||
} else if err != nil {
|
||||
log.WithError(err).Error(models.ErrAppsRemoving)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -6,24 +6,15 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/runner/common"
|
||||
)
|
||||
|
||||
func (s *Server) handleAppGet(c *gin.Context) {
|
||||
ctx := c.MustGet("ctx").(context.Context)
|
||||
log := common.Logger(ctx)
|
||||
|
||||
appName := c.MustGet(api.AppName).(string)
|
||||
app, err := s.Datastore.GetApp(ctx, appName)
|
||||
|
||||
if err != nil && err != models.ErrAppsNotFound {
|
||||
log.WithError(err).Error(models.ErrAppsGet)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsGet))
|
||||
return
|
||||
} else if app == nil {
|
||||
log.WithError(err).Error(models.ErrAppsNotFound)
|
||||
c.JSON(http.StatusNotFound, simpleError(models.ErrAppsNotFound))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -6,19 +6,16 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/runner/common"
|
||||
)
|
||||
|
||||
func (s *Server) handleAppList(c *gin.Context) {
|
||||
ctx := c.MustGet("ctx").(context.Context)
|
||||
log := common.Logger(ctx)
|
||||
|
||||
filter := &models.AppFilter{}
|
||||
|
||||
apps, err := s.Datastore.GetApps(ctx, filter)
|
||||
if err != nil {
|
||||
log.WithError(err).Error(models.ErrAppsList)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -45,13 +45,8 @@ func (s *Server) handleAppUpdate(c *gin.Context) {
|
||||
}
|
||||
|
||||
app, err := s.Datastore.UpdateApp(ctx, wapp.App)
|
||||
if err == models.ErrAppsNotFound {
|
||||
log.WithError(err).Debug(models.ErrAppsUpdate)
|
||||
c.JSON(http.StatusNotFound, simpleError(err))
|
||||
return
|
||||
} else if err != nil {
|
||||
log.WithError(err).Error(models.ErrAppsUpdate)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
@@ -90,13 +90,8 @@ func (s *Server) handleRouteCreate(c *gin.Context) {
|
||||
}
|
||||
|
||||
route, err := s.Datastore.InsertRoute(ctx, wroute.Route)
|
||||
if err == models.ErrRoutesAlreadyExists {
|
||||
log.WithError(err).Debug(models.ErrRoutesCreate)
|
||||
c.JSON(http.StatusConflict, simpleError(models.ErrRoutesAlreadyExists))
|
||||
return
|
||||
} else if err != nil {
|
||||
log.WithError(err).Error(models.ErrRoutesCreate)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -7,25 +7,16 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/runner/common"
|
||||
)
|
||||
|
||||
func (s *Server) handleRouteDelete(c *gin.Context) {
|
||||
ctx := c.MustGet("ctx").(context.Context)
|
||||
log := common.Logger(ctx)
|
||||
|
||||
appName := c.MustGet(api.AppName).(string)
|
||||
routePath := path.Clean(c.MustGet(api.Path).(string))
|
||||
|
||||
if err := s.Datastore.RemoveRoute(ctx, appName, routePath); err != nil {
|
||||
if err == models.ErrRoutesNotFound {
|
||||
log.WithError(err).Debug(models.ErrRoutesRemoving)
|
||||
c.JSON(http.StatusNotFound, simpleError(models.ErrRoutesNotFound))
|
||||
} else {
|
||||
log.WithError(err).Error(models.ErrRoutesRemoving)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesRemoving))
|
||||
}
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -7,25 +7,17 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/runner/common"
|
||||
)
|
||||
|
||||
func (s *Server) handleRouteGet(c *gin.Context) {
|
||||
ctx := c.MustGet("ctx").(context.Context)
|
||||
log := common.Logger(ctx)
|
||||
|
||||
appName := c.MustGet(api.AppName).(string)
|
||||
routePath := path.Clean(c.MustGet(api.Path).(string))
|
||||
|
||||
route, err := s.Datastore.GetRoute(ctx, appName, routePath)
|
||||
if err != nil && err != models.ErrRoutesNotFound {
|
||||
log.WithError(err).Error(models.ErrRoutesGet)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
||||
return
|
||||
} else if route == nil {
|
||||
log.Debug(models.ErrRoutesNotFound)
|
||||
c.JSON(http.StatusNotFound, simpleError(models.ErrRoutesNotFound))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,10 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/runner/common"
|
||||
)
|
||||
|
||||
func (s *Server) handleRouteList(c *gin.Context) {
|
||||
ctx := c.MustGet("ctx").(context.Context)
|
||||
log := common.Logger(ctx)
|
||||
|
||||
filter := &models.RouteFilter{}
|
||||
|
||||
@@ -28,13 +26,8 @@ func (s *Server) handleRouteList(c *gin.Context) {
|
||||
routes, err = s.Datastore.GetRoutes(ctx, filter)
|
||||
}
|
||||
|
||||
if err == models.ErrAppsNotFound {
|
||||
log.WithError(err).Debug(models.ErrRoutesGet)
|
||||
c.JSON(http.StatusNotFound, simpleError(err))
|
||||
return
|
||||
} else if err != nil {
|
||||
log.WithError(err).Error(models.ErrRoutesGet)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -52,13 +52,8 @@ func (s *Server) handleRouteUpdate(c *gin.Context) {
|
||||
}
|
||||
|
||||
route, err := s.Datastore.UpdateRoute(ctx, wroute.Route)
|
||||
if err == models.ErrRoutesNotFound {
|
||||
log.WithError(err).Debug(models.ErrRoutesUpdate)
|
||||
c.JSON(http.StatusNotFound, simpleError(err))
|
||||
return
|
||||
} else if err != nil {
|
||||
log.WithError(err).Error(models.ErrRoutesUpdate)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesUpdate))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
@@ -253,12 +252,6 @@ func (s *Server) bindHandlers() {
|
||||
engine.NoRoute(s.handleSpecial)
|
||||
}
|
||||
|
||||
var ErrInternalServerError = errors.New("Something unexpected happened on the server")
|
||||
|
||||
func simpleError(err error) *models.Error {
|
||||
return &models.Error{&models.ErrorBody{Message: err.Error()}}
|
||||
}
|
||||
|
||||
type appResponse struct {
|
||||
Message string `json:"message"`
|
||||
App *models.App `json:"app"`
|
||||
|
||||
@@ -132,7 +132,7 @@ func TestFullStack(t *testing.T) {
|
||||
{"delete myroute2", "DELETE", "/v1/apps/myapp/routes/myroute2", ``, http.StatusOK, 0},
|
||||
{"delete app (success)", "DELETE", "/v1/apps/myapp", ``, http.StatusOK, 0},
|
||||
{"get deleted app", "GET", "/v1/apps/myapp", ``, http.StatusNotFound, 0},
|
||||
{"get delete route on deleted app", "GET", "/v1/apps/myapp/routes/myroute", ``, http.StatusInternalServerError, 0},
|
||||
{"get deleteds route on deleted app", "GET", "/v1/apps/myapp/routes/myroute", ``, http.StatusNotFound, 0},
|
||||
} {
|
||||
_, rec := routerRequest(t, srv.Router, test.method, test.path, bytes.NewBuffer([]byte(test.body)))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user