mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Largely a removal job, however many tests, particularly system level ones relied on Routes. These have been migrated to use Fns. * Add 410 response to swagger * No app names in log tags * Adding constraint in GetCall for FnID * Adding test to check FnID is required on call * Add fn_id to call selector * Fix text in docker mem warning * Correct buildConfig func name * Test fix up * Removing CPU setting from Agent test CPU setting has been deprecated, but the code base is still riddled with it. This just removes it from this layer. Really we need to remove it from Call. * Remove fn id check on calls * Reintroduce fn id required on call * Adding fnID to calls for execute test * Correct setting of app id in middleware * Removes root middlewares ability to redirect fun invocations * Add over sized test check * Removing call fn id check
66 lines
2.0 KiB
Go
66 lines
2.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"runtime/debug"
|
|
|
|
"github.com/fnproject/fn/api/common"
|
|
"github.com/fnproject/fn/api/models"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// ErrInternalServerError returned when something exceptional happens.
|
|
var ErrInternalServerError = errors.New("internal server error")
|
|
|
|
func simpleError(err error) *models.Error {
|
|
return &models.Error{Message: err.Error()}
|
|
}
|
|
|
|
func handleErrorResponse(c *gin.Context, err error) {
|
|
HandleErrorResponse(c.Request.Context(), c.Writer, err)
|
|
}
|
|
|
|
// HandleErrorResponse used to handle response errors in the same way.
|
|
func HandleErrorResponse(ctx context.Context, w http.ResponseWriter, err error) {
|
|
log := common.Logger(ctx)
|
|
|
|
if ctx.Err() == context.Canceled {
|
|
log.Info("client context cancelled")
|
|
w.WriteHeader(models.ErrClientCancel.Code())
|
|
return
|
|
}
|
|
|
|
var statuscode int
|
|
if e, ok := err.(models.APIError); ok {
|
|
if e.Code() >= 500 {
|
|
log.WithFields(logrus.Fields{"code": e.Code()}).WithError(e).Error("api error")
|
|
}
|
|
if err == models.ErrCallTimeoutServerBusy {
|
|
// TODO: Determine a better delay value here (perhaps ask Agent). For now 15 secs with
|
|
// the hopes that fnlb will land this on a better server immediately.
|
|
w.Header().Set("Retry-After", "15")
|
|
}
|
|
statuscode = e.Code()
|
|
} else {
|
|
log.WithError(err).WithFields(logrus.Fields{"stack": string(debug.Stack())}).Error("internal server error")
|
|
statuscode = http.StatusInternalServerError
|
|
err = ErrInternalServerError
|
|
}
|
|
WriteError(ctx, w, statuscode, err)
|
|
}
|
|
|
|
// WriteError easy way to do standard error response, but can set statuscode and error message easier than handleV1ErrorResponse
|
|
func WriteError(ctx context.Context, w http.ResponseWriter, statuscode int, err error) {
|
|
log := common.Logger(ctx)
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.WriteHeader(statuscode)
|
|
err = json.NewEncoder(w).Encode(simpleError(err))
|
|
if err != nil {
|
|
log.WithError(err).Errorln("error encoding error json")
|
|
}
|
|
}
|