Skip to content

Commit

Permalink
Merge pull request #659 from moritzploss/custom-error-response-writer
Browse files Browse the repository at this point in the history
add injectable error response writer
  • Loading branch information
kpacha authored May 11, 2023
2 parents 20f8788 + 46fbfc1 commit faec9df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
24 changes: 16 additions & 8 deletions router/gin/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ const requestParamsAsterisk string = "*"
// HandlerFactory creates a handler function that adapts the gin router with the injected proxy
type HandlerFactory func(*config.EndpointConfig, proxy.Proxy) gin.HandlerFunc

// EndpointHandler implements the HandleFactory interface using the default ToHTTPError function
// ErrorResponseWriter writes the string representation of an error into the response body
// and sets a Content-Type header for errors that implement the encodedResponseError interface.
var ErrorResponseWriter = func(c *gin.Context, statusCode int, err error) {
if te, ok := err.(encodedResponseError); ok && te.Encoding() != "" {
c.Header("Content-Type", te.Encoding())
}
c.Writer.WriteString(err.Error())
}

// EndpointHandler implements the HandlerFactory interface using the default ToHTTPError function
var EndpointHandler = CustomErrorEndpointHandler(logging.NoOp, server.DefaultToHTTPError)

// CustomErrorEndpointHandler returns a HandleFactory using the injected ToHTTPError function and logger
// CustomErrorEndpointHandler returns a HandlerFactory using the injected ToHTTPError function and logger
func CustomErrorEndpointHandler(logger logging.Logger, errF server.ToHTTPError) HandlerFactory {
return func(configuration *config.EndpointConfig, prxy proxy.Proxy) gin.HandlerFunc {
cacheControlHeaderValue := fmt.Sprintf("public, max-age=%d", int(configuration.CacheTTL.Seconds()))
Expand Down Expand Up @@ -83,16 +92,15 @@ func CustomErrorEndpointHandler(logger logging.Logger, errF server.ToHTTPError)
}

if response == nil {
var statusCode int
if t, ok := err.(responseError); ok {
c.Status(t.StatusCode())
statusCode = t.StatusCode()
} else {
c.Status(errF(err))
statusCode = errF(err)
}
c.Status(statusCode)
if returnErrorMsg {
if te, ok := err.(encodedResponseError); ok && te.Encoding() != "" {
c.Header("Content-Type", te.Encoding())
}
c.Writer.WriteString(err.Error())
ErrorResponseWriter(c, statusCode, err)
}
cancel()
return
Expand Down
5 changes: 3 additions & 2 deletions router/gin/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package gin

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -136,12 +137,12 @@ func paramChecker() gin.HandlerFunc {
for _, param := range c.Params {
s, err := url.PathUnescape(param.Value)
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("error: %s", err))
ErrorResponseWriter(c, http.StatusBadRequest, fmt.Errorf("error: %s", err))
c.AbortWithStatus(http.StatusBadRequest)
return
}
if s != param.Value || strings.Contains(s, "?") || strings.Contains(s, "#") {
c.String(http.StatusBadRequest, "error: encoded url params")
ErrorResponseWriter(c, http.StatusBadRequest, errors.New("error: encoded url params"))
c.AbortWithStatus(http.StatusBadRequest)
return
}
Expand Down

0 comments on commit faec9df

Please sign in to comment.