mask errors in api response, log real error

we had this _almost_ right, in that we were trying, but we weren't masking the
error from the user response for any error we don't intend to show. this also
adds a stack trace from any internal server errors, so that we might be able
to track them down in the future (looking at you, 'context deadline
exceeded'). in addition, this adds a new `models.APIError` interface which all
of the errors in `models` now implement, and can be caught easily / added to
easily.

the front end now does no status rewriting based on api errors, now when we
get a non-nil error we can call `handleResponse(c, err)` with it and if it's a
proper error, return it to the user with the right status code, otherwise log
a stack trace and return `internal server error`. this cleans up a lot of the
front end code.

also rewrites start task ctx deadline exceeded as timeout. with iw we had
async tasks so we could start the clock later and it didn't matter, but now
with sync tasks time out sometimes just making docker calls, and we want the
task status to show up as timed out. we may want to just catch all this above
in addition to this, but this seems like the right thing to do.

remove squishing together errors. this was weird, now we return the first
error for the purposes of using the new err interface.

removed a lot of 5xx errors that really should have been 4xx errors. changed
some of the 400 errors to 409 errors, since they are from sending in
conflicting info and not a malformed request.

removed unused errors / useless errors (many were used for logging, and didn't
provide any context. now with stack traces we don't need context as much in
the logs).
This commit is contained in:
Reed Allman
2017-07-13 21:14:01 -07:00
parent db35a8cfd2
commit c0aed2fbb0
27 changed files with 293 additions and 332 deletions

View File

@@ -5,38 +5,32 @@ import (
"github.com/gin-gonic/gin"
"gitlab-odx.oracle.com/odx/functions/api/models"
"gitlab-odx.oracle.com/odx/functions/api/runner/common"
)
func (s *Server) handleAppCreate(c *gin.Context) {
ctx := c.MustGet("mctx").(MiddlewareContext)
log := common.Logger(ctx)
var wapp models.AppWrapper
err := c.BindJSON(&wapp)
if err != nil {
log.WithError(err).Debug(models.ErrInvalidJSON)
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
handleErrorResponse(c, models.ErrInvalidJSON)
return
}
if wapp.App == nil {
log.Debug(models.ErrAppsMissingNew)
c.JSON(http.StatusBadRequest, simpleError(models.ErrAppsMissingNew))
handleErrorResponse(c, models.ErrAppsMissingNew)
return
}
if err := wapp.Validate(); err != nil {
log.Error(err)
c.JSON(http.StatusInternalServerError, simpleError(err))
handleErrorResponse(c, err)
return
}
err = s.FireBeforeAppCreate(ctx, wapp.App)
if err != nil {
log.WithError(err).Error(models.ErrAppsCreate)
c.JSON(http.StatusInternalServerError, simpleError(err))
handleErrorResponse(c, err)
return
}
@@ -48,8 +42,7 @@ func (s *Server) handleAppCreate(c *gin.Context) {
err = s.FireAfterAppCreate(ctx, wapp.App)
if err != nil {
log.WithError(err).Error(models.ErrAppsCreate)
c.JSON(http.StatusInternalServerError, simpleError(err))
handleErrorResponse(c, err)
return
}

View File

@@ -17,21 +17,20 @@ func (s *Server) handleAppDelete(c *gin.Context) {
routes, err := s.Datastore.GetRoutesByApp(ctx, app.Name, &models.RouteFilter{})
if err != nil {
log.WithError(err).Error(models.ErrAppsRemoving)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
log.WithError(err).Error("error getting route in app delete")
handleErrorResponse(c, err)
return
}
//TODO allow this? #528
if len(routes) > 0 {
log.WithError(err).Debug(models.ErrDeleteAppsWithRoutes)
c.JSON(http.StatusBadRequest, simpleError(models.ErrDeleteAppsWithRoutes))
handleErrorResponse(c, models.ErrDeleteAppsWithRoutes)
return
}
err = s.FireBeforeAppDelete(ctx, app)
if err != nil {
log.WithError(err).Error(models.ErrAppsRemoving)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
log.WithError(err).Error("error firing before app delete")
handleErrorResponse(c, err)
return
}
@@ -49,8 +48,8 @@ func (s *Server) handleAppDelete(c *gin.Context) {
err = s.FireAfterAppDelete(ctx, app)
if err != nil {
log.WithError(err).Error(models.ErrAppsRemoving)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
log.WithError(err).Error("error firing after app delete")
handleErrorResponse(c, err)
return
}

View File

@@ -39,10 +39,10 @@ func TestAppCreate(t *testing.T) {
{datastore.NewMock(), logs.NewMock(), "/v1/apps", ``, http.StatusBadRequest, models.ErrInvalidJSON},
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{}`, http.StatusBadRequest, models.ErrAppsMissingNew},
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "name": "Test" }`, http.StatusBadRequest, models.ErrAppsMissingNew},
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "" } }`, http.StatusInternalServerError, models.ErrAppsValidationMissingName},
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "1234567890123456789012345678901" } }`, http.StatusInternalServerError, models.ErrAppsValidationTooLongName},
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "&&%@!#$#@$" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName},
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "&&%@!#$#@$" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName},
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "" } }`, http.StatusBadRequest, models.ErrAppsValidationMissingName},
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "1234567890123456789012345678901" } }`, http.StatusBadRequest, models.ErrAppsValidationTooLongName},
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "&&%@!#$#@$" } }`, http.StatusBadRequest, models.ErrAppsValidationInvalidName},
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "&&%@!#$#@$" } }`, http.StatusBadRequest, models.ErrAppsValidationInvalidName},
// success
{datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "teste" } }`, http.StatusOK, nil},
@@ -215,7 +215,7 @@ func TestAppUpdate(t *testing.T) {
[]*models.App{{
Name: "myapp",
}}, nil, nil, nil,
), logs.NewMock(), "/v1/apps/myapp", `{ "app": { "name": "othername" } }`, http.StatusBadRequest, nil},
), logs.NewMock(), "/v1/apps/myapp", `{ "app": { "name": "othername" } }`, http.StatusConflict, nil},
} {
rnr, cancel := testRunner(t)
srv := testServer(test.mock, &mqs.Mock{}, test.logDB, rnr)

View File

@@ -6,31 +6,26 @@ import (
"github.com/gin-gonic/gin"
"gitlab-odx.oracle.com/odx/functions/api"
"gitlab-odx.oracle.com/odx/functions/api/models"
"gitlab-odx.oracle.com/odx/functions/api/runner/common"
)
func (s *Server) handleAppUpdate(c *gin.Context) {
ctx := c.MustGet("mctx").(MiddlewareContext)
log := common.Logger(ctx)
wapp := models.AppWrapper{}
err := c.BindJSON(&wapp)
if err != nil {
log.WithError(err).Debug(models.ErrInvalidJSON)
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
handleErrorResponse(c, models.ErrInvalidJSON)
return
}
if wapp.App == nil {
log.Debug(models.ErrAppsMissingNew)
c.JSON(http.StatusBadRequest, simpleError(models.ErrAppsMissingNew))
handleErrorResponse(c, models.ErrAppsMissingNew)
return
}
if wapp.App.Name != "" {
log.Debug(models.ErrAppsNameImmutable)
c.JSON(http.StatusBadRequest, simpleError(models.ErrAppsNameImmutable))
handleErrorResponse(c, models.ErrAppsNameImmutable)
return
}
@@ -38,8 +33,7 @@ func (s *Server) handleAppUpdate(c *gin.Context) {
err = s.FireAfterAppUpdate(ctx, wapp.App)
if err != nil {
log.WithError(err).Error(models.ErrAppsUpdate)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
handleErrorResponse(c, err)
return
}
@@ -51,8 +45,7 @@ func (s *Server) handleAppUpdate(c *gin.Context) {
err = s.FireAfterAppUpdate(ctx, wapp.App)
if err != nil {
log.WithError(err).Error(models.ErrAppsUpdate)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
handleErrorResponse(c, err)
return
}

View File

@@ -14,24 +14,24 @@ func (s *Server) handleCallList(c *gin.Context) {
appName, ok := c.MustGet(api.AppName).(string)
if ok && appName == "" {
c.JSON(http.StatusBadRequest, models.ErrRoutesValidationMissingAppName)
handleErrorResponse(c, models.ErrRoutesValidationMissingAppName)
return
}
_, err := s.Datastore.GetApp(c, appName)
if err != nil {
c.JSON(http.StatusNotFound, models.ErrAppsNotFound)
handleErrorResponse(c, err)
return
}
appRoute, ok := c.MustGet(api.Path).(string)
if ok && appRoute == "" {
c.JSON(http.StatusBadRequest, models.ErrRoutesValidationMissingPath)
handleErrorResponse(c, models.ErrRoutesValidationMissingPath)
return
}
_, err = s.Datastore.GetRoute(c, appName, appRoute)
if err != nil {
c.JSON(http.StatusNotFound, models.ErrRoutesNotFound)
handleErrorResponse(c, err)
return
}

View File

@@ -3,35 +3,31 @@ package server
import (
"context"
"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"
"net/http"
)
var ErrInternalServerError = errors.New("Something unexpected happened on the server")
var ErrInternalServerError = errors.New("internal server error")
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))
if aerr, ok := err.(models.APIError); ok {
log.WithFields(logrus.Fields{"code": aerr.Code()}).WithError(err).Error("api error")
c.JSON(aerr.Code(), simpleError(err))
} else if err != nil {
// get a stack trace so we can trace this error
log.WithError(err).WithFields(logrus.Fields{"stack": string(debug.Stack())}).Error("internal server error")
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
}
}

View File

@@ -45,11 +45,13 @@ func (s *Server) apiAppHandlerWrapperFunc(apiHandler ApiAppHandler) gin.HandlerF
appName := c.Param(api.CApp)
app, err := s.Datastore.GetApp(c.Request.Context(), appName)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
handleErrorResponse(c, err)
c.Abort()
return
}
if app == nil {
c.AbortWithStatus(http.StatusNotFound)
handleErrorResponse(c, models.ErrAppsNotFound)
c.Abort()
return
}

View File

@@ -9,7 +9,6 @@ import (
"github.com/gin-gonic/gin"
"gitlab-odx.oracle.com/odx/functions/api"
"gitlab-odx.oracle.com/odx/functions/api/models"
"gitlab-odx.oracle.com/odx/functions/api/runner/common"
)
/* handleRouteCreateOrUpdate is used to handle POST PUT and PATCH for routes.
@@ -25,23 +24,20 @@ import (
*/
func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
ctx := c.MustGet("mctx").(MiddlewareContext)
log := common.Logger(ctx)
method := strings.ToUpper(c.Request.Method)
var wroute models.RouteWrapper
err, resperr := s.bindAndValidate(ctx, c, method, &wroute)
if err != nil || resperr != nil {
log.WithError(err).Debug(resperr)
c.JSON(http.StatusBadRequest, simpleError(resperr))
err := s.bindAndValidate(ctx, c, method, &wroute)
if err != nil {
handleErrorResponse(c, err)
return
}
// Create the app if it does not exist.
err, resperr = s.ensureApp(ctx, &wroute, method)
if err != nil || resperr != nil {
log.WithError(err).Debug(resperr)
handleErrorResponse(c, resperr)
err = s.ensureApp(ctx, &wroute, method)
if err != nil {
handleErrorResponse(c, err)
return
}
@@ -57,53 +53,51 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
}
// ensureApp will only execute if it is on post or put. Patch is not allowed to create apps.
func (s *Server) ensureApp(ctx MiddlewareContext, wroute *models.RouteWrapper, method string) (error, error) {
func (s *Server) ensureApp(ctx MiddlewareContext, wroute *models.RouteWrapper, method string) error {
if !(method == http.MethodPost || method == http.MethodPut) {
return nil, nil
return nil
}
var app *models.App
var err error
app, err = s.Datastore.GetApp(ctx, wroute.Route.AppName)
app, err := s.Datastore.GetApp(ctx, wroute.Route.AppName)
if err != nil && err != models.ErrAppsNotFound {
return err, models.ErrAppsGet
return err
} else if app == nil {
// Create a new application
newapp := &models.App{Name: wroute.Route.AppName}
if err = newapp.Validate(); err != nil {
return err, err
return err
}
err = s.FireBeforeAppCreate(ctx, newapp)
if err != nil {
return err, models.ErrAppsCreate
return err
}
_, err = s.Datastore.InsertApp(ctx, newapp)
if err != nil {
return err, models.ErrAppsCreate
return err
}
err = s.FireAfterAppCreate(ctx, newapp)
if err != nil {
return err, models.ErrAppsCreate
return err
}
}
return nil, nil
return nil
}
/* bindAndValidate binds the RouteWrapper to the json from the request and validates that it is correct.
If it is a put or patch it makes sure that the path in the url matches the provideed one in the body.
Defaults are set and if patch skipZero is true for validating the RouteWrapper
*/
func (s *Server) bindAndValidate(ctx context.Context, c *gin.Context, method string, wroute *models.RouteWrapper) (error, error) {
func (s *Server) bindAndValidate(ctx context.Context, c *gin.Context, method string, wroute *models.RouteWrapper) error {
err := c.BindJSON(wroute)
if err != nil {
return err, models.ErrInvalidJSON
return models.ErrInvalidJSON
}
if wroute.Route == nil {
return err, models.ErrRoutesMissingNew
return models.ErrRoutesMissingNew
}
wroute.Route.AppName = c.MustGet(api.AppName).(string)
@@ -111,17 +105,14 @@ func (s *Server) bindAndValidate(ctx context.Context, c *gin.Context, method str
p := path.Clean(c.MustGet(api.Path).(string))
if wroute.Route.Path != "" && wroute.Route.Path != p {
return models.ErrRoutesPathImmutable, models.ErrRoutesPathImmutable
return models.ErrRoutesPathImmutable
}
wroute.Route.Path = p
}
wroute.Route.SetDefaults()
if err = wroute.Validate(method == http.MethodPatch); err != nil {
return models.ErrRoutesCreate, err
}
return nil, nil
return wroute.Validate(method == http.MethodPatch)
}
// updateOrInsertRoute will either update or insert the route respective the method.

View File

@@ -63,7 +63,7 @@ func TestRouteCreate(t *testing.T) {
{datastore.NewMock(), logs.NewMock(), http.MethodPost, "/v1/apps/a/routes", `{ "route": { "path": "/myroute" } }`, http.StatusBadRequest, models.ErrRoutesValidationMissingImage},
{datastore.NewMock(), logs.NewMock(), http.MethodPost, "/v1/apps/a/routes", `{ "route": { "image": "funcy/hello" } }`, http.StatusBadRequest, models.ErrRoutesValidationMissingPath},
{datastore.NewMock(), logs.NewMock(), http.MethodPost, "/v1/apps/a/routes", `{ "route": { "image": "funcy/hello", "path": "myroute" } }`, http.StatusBadRequest, models.ErrRoutesValidationInvalidPath},
{datastore.NewMock(), logs.NewMock(), http.MethodPost, "/v1/apps/$/routes", `{ "route": { "image": "funcy/hello", "path": "/myroute" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName},
{datastore.NewMock(), logs.NewMock(), http.MethodPost, "/v1/apps/$/routes", `{ "route": { "image": "funcy/hello", "path": "/myroute" } }`, http.StatusBadRequest, models.ErrAppsValidationInvalidName},
{datastore.NewMockInit(nil,
[]*models.Route{
{
@@ -84,16 +84,16 @@ func TestRoutePut(t *testing.T) {
buf := setLogBuffer()
for i, test := range []routeTestCase{
// errors
// errors (NOTE: this route doesn't exist yet)
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ }`, http.StatusBadRequest, models.ErrRoutesMissingNew},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "path": "/myroute" }`, http.StatusBadRequest, models.ErrRoutesMissingNew},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { } }`, http.StatusBadRequest, models.ErrRoutesValidationMissingImage},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { "path": "/myroute" } }`, http.StatusBadRequest, models.ErrRoutesValidationMissingImage},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { "image": "funcy/hello", "path": "myroute" } }`, http.StatusBadRequest, models.ErrRoutesPathImmutable},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { "image": "funcy/hello", "path": "diffRoute" } }`, http.StatusBadRequest, models.ErrRoutesPathImmutable},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/$/routes/myroute", `{ "route": { "image": "funcy/hello", "path": "/myroute" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { "type": "invalid-type" } }`, http.StatusBadRequest, models.ErrRoutesValidationInvalidType},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { "format": "invalid-format" } }`, http.StatusBadRequest, models.ErrRoutesValidationInvalidFormat},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { "image": "funcy/hello", "path": "myroute" } }`, http.StatusConflict, models.ErrRoutesPathImmutable},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { "image": "funcy/hello", "path": "diffRoute" } }`, http.StatusConflict, models.ErrRoutesPathImmutable},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/$/routes/myroute", `{ "route": { "image": "funcy/hello", "path": "/myroute" } }`, http.StatusBadRequest, models.ErrAppsValidationInvalidName},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { "image": "funcy/hello", "path": "/myroute", "type": "invalid-type" } }`, http.StatusBadRequest, models.ErrRoutesValidationInvalidType},
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { "image": "funcy/hello", "path": "/myroute", "format": "invalid-format" } }`, http.StatusBadRequest, models.ErrRoutesValidationInvalidFormat},
// success
{datastore.NewMock(), logs.NewMock(), http.MethodPut, "/v1/apps/a/routes/myroute", `{ "route": { "image": "funcy/hello", "path": "/myroute" } }`, http.StatusOK, nil},
@@ -259,7 +259,7 @@ func TestRouteUpdate(t *testing.T) {
Path: "/myroute/do",
},
}, nil, nil,
), logs.NewMock(), http.MethodPatch, "/v1/apps/a/routes/myroute/do", `{ "route": { "path": "/otherpath" } }`, http.StatusBadRequest, models.ErrRoutesPathImmutable},
), logs.NewMock(), http.MethodPatch, "/v1/apps/a/routes/myroute/do", `{ "route": { "path": "/otherpath" } }`, http.StatusConflict, models.ErrRoutesPathImmutable},
} {
test.run(t, i, buf)
}

View File

@@ -3,7 +3,6 @@ package server
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
@@ -30,7 +29,6 @@ type runnerResponse struct {
func (s *Server) handleSpecial(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
log := common.Logger(ctx)
ctx = context.WithValue(ctx, api.AppName, "")
c.Set(api.AppName, "")
@@ -38,21 +36,15 @@ func (s *Server) handleSpecial(c *gin.Context) {
c.Set(api.Path, c.Request.URL.Path)
ctx, err := s.UseSpecialHandlers(ctx, c.Request, c.Writer)
if err == ErrNoSpecialHandlerFound {
log.WithError(err).Errorln("Not special handler found")
c.JSON(http.StatusNotFound, http.StatusText(http.StatusNotFound))
return
} else if err != nil {
log.WithError(err).Errorln("Error using special handler!")
c.JSON(http.StatusInternalServerError, simpleError(errors.New("Failed to run function")))
if err != nil {
handleErrorResponse(c, err)
return
}
c.Set("ctx", ctx)
c.Set(api.AppName, ctx.Value(api.AppName).(string))
if c.MustGet(api.AppName).(string) == "" {
log.WithError(err).Errorln("Specialhandler returned empty app name")
c.JSON(http.StatusBadRequest, simpleError(models.ErrRunnerRouteNotFound))
handleErrorResponse(c, models.ErrRunnerRouteNotFound)
return
}
@@ -110,23 +102,23 @@ func (s *Server) handleRequest(c *gin.Context, enqueue models.Enqueue) {
path := reqRoute.Path
app, err := s.Datastore.GetApp(ctx, appName)
if err != nil || app == nil {
log.WithError(err).Error(models.ErrAppsNotFound)
c.JSON(http.StatusNotFound, simpleError(models.ErrAppsNotFound))
if err != nil {
handleErrorResponse(c, err)
return
} else if app == nil {
handleErrorResponse(c, models.ErrAppsNotFound)
return
}
log.WithFields(logrus.Fields{"app": appName, "path": path}).Debug("Finding route on datastore")
routes, err := s.loadroutes(ctx, models.RouteFilter{AppName: appName, Path: path})
if err != nil {
log.WithError(err).Error(models.ErrRoutesList)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesList))
handleErrorResponse(c, err)
return
}
if len(routes) == 0 {
log.WithError(err).Error(models.ErrRunnerRouteNotFound)
c.JSON(http.StatusNotFound, simpleError(models.ErrRunnerRouteNotFound))
handleErrorResponse(c, models.ErrRunnerRouteNotFound)
return
}
@@ -139,8 +131,7 @@ func (s *Server) handleRequest(c *gin.Context, enqueue models.Enqueue) {
return
}
log.Error(models.ErrRunnerRouteNotFound)
c.JSON(http.StatusNotFound, simpleError(models.ErrRunnerRouteNotFound))
handleErrorResponse(c, models.ErrRunnerRouteNotFound)
}
func (s *Server) loadroutes(ctx context.Context, filter models.RouteFilter) ([]*models.Route, error) {
@@ -241,8 +232,7 @@ func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, rout
// Read payload
pl, err := ioutil.ReadAll(cfg.Stdin)
if err != nil {
log.WithError(err).Error(models.ErrInvalidPayload)
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidPayload))
handleErrorResponse(c, models.ErrInvalidPayload)
return true
}
// Create Task

View File

@@ -205,28 +205,24 @@ func (s *Server) handleTaskRequest(c *gin.Context) {
case "GET":
task, err := s.MQ.Reserve(ctx)
if err != nil {
logrus.WithError(err).Error()
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesList))
handleErrorResponse(c, err)
return
}
c.JSON(http.StatusAccepted, task)
case "DELETE":
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
logrus.WithError(err).Error()
c.JSON(http.StatusInternalServerError, err)
handleErrorResponse(c, err)
return
}
var task models.Task
if err = json.Unmarshal(body, &task); err != nil {
logrus.WithError(err).Error()
c.JSON(http.StatusInternalServerError, err)
handleErrorResponse(c, err)
return
}
if err := s.MQ.Delete(ctx, &task); err != nil {
logrus.WithError(err).Error()
c.JSON(http.StatusInternalServerError, err)
handleErrorResponse(c, err)
return
}
c.JSON(http.StatusAccepted, task)

View File

@@ -123,7 +123,7 @@ func TestFullStack(t *testing.T) {
{"execute myroute", "POST", "/r/myapp/myroute", `{ "name": "Teste" }`, http.StatusOK, 2},
{"execute myroute2", "POST", "/r/myapp/myroute2", `{ "name": "Teste" }`, http.StatusInternalServerError, 2},
{"delete myroute", "DELETE", "/v1/apps/myapp/routes/myroute", ``, http.StatusOK, 1},
{"delete app (fail)", "DELETE", "/v1/apps/myapp", ``, http.StatusBadRequest, 1},
{"delete app (fail)", "DELETE", "/v1/apps/myapp", ``, http.StatusConflict, 1},
{"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},

View File

@@ -2,11 +2,10 @@ package server
import (
"context"
"errors"
"net/http"
)
var ErrNoSpecialHandlerFound = errors.New("Path not found")
"gitlab-odx.oracle.com/odx/functions/api/models"
)
type SpecialHandler interface {
Handle(c HandlerContext) error
@@ -56,7 +55,7 @@ func (s *Server) AddSpecialHandler(handler SpecialHandler) {
// UseSpecialHandlers execute all special handlers
func (s *Server) UseSpecialHandlers(ctx context.Context, req *http.Request, resp http.ResponseWriter) (context.Context, error) {
if len(s.specialHandlers) == 0 {
return ctx, ErrNoSpecialHandlerFound
return ctx, models.ErrNoSpecialHandlerFound
}
c := &SpecialHandlerContext{