En cours de développement

master
losyme 11 months ago
parent e4aa975543
commit 8c658fa137
  1. 111
      decode/json.go
  2. 9
      kong.go
  3. 10
      middleware/json/json.go

@ -0,0 +1,111 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package decode
import (
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net/http"
"strings"
"forge.chapril.org/losyme/kong"
"forge.chapril.org/losyme/kong/context"
)
const _jsonMediaType = "application/json"
func JSON(c *context.Context, maxBodySize int64, v interface{}) error {
ct := c.Request.Header.Get("Content-Type")
if ct == "" {
return &kong.Error{
Status: http.StatusUnsupportedMediaType,
Message: "header content type must not be empty",
}
}
mt, _, err := mime.ParseMediaType(ct)
if err != nil || mt != _jsonMediaType {
return &kong.Error{
Status: http.StatusUnsupportedMediaType,
Message: fmt.Sprintf("header content type must be '%s' not %s", _jsonMediaType, ct),
}
}
decoder := json.NewDecoder(http.MaxBytesReader(c.Response, c.Request.Body, maxBodySize))
decoder.DisallowUnknownFields()
if err := decoder.Decode(v); err != nil {
var (
jse *json.SyntaxError
jute *json.UnmarshalTypeError
)
switch {
case errors.As(err, &jse):
return &kong.Error{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("request body contains badly-formed JSON (at position %d)", jse.Offset),
}
case errors.Is(err, io.ErrUnexpectedEOF):
return &kong.Error{
Status: http.StatusBadRequest,
Message: "request body contains badly-formed JSON",
}
case errors.As(err, &jute):
return &kong.Error{
Status: http.StatusBadRequest,
Message: fmt.Sprintf(
"request body contains an invalid value for the %q field (at position %d)",
jute.Field,
jute.Offset,
),
}
case strings.HasPrefix(err.Error(), "json: unknown field"):
return &kong.Error{
Status: http.StatusBadRequest,
Message: fmt.Sprintf(
"request body contains unknown field %s",
strings.TrimPrefix(err.Error(), "json: unknown field "),
),
}
case errors.Is(err, io.EOF):
return &kong.Error{
Status: http.StatusBadRequest,
Message: "request body must not be empty",
}
case err.Error() == "http: request body too large":
return &kong.Error{
Status: http.StatusRequestEntityTooLarge,
Message: fmt.Sprintf("request body must not be larger than %d bytes", maxBodySize),
}
default:
return err
}
}
if err := decoder.Decode(&struct{}{}); err != io.EOF {
return &kong.Error{
Status: http.StatusBadRequest,
Message: "request body must only contain a single JSON object",
}
}
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

@ -8,6 +8,15 @@ package kong
import "forge.chapril.org/losyme/kong/context"
type Error struct {
Status int
Message string
}
func (e *Error) Error() string {
return e.Message
}
type Logger interface {
Trace(msg string, kv ...interface{})
Notice(msg string, kv ...interface{})

@ -46,8 +46,16 @@ func OnError(logger kong.Logger) router.Middleware {
"reason", err,
)
status := http.StatusInternalServerError
var ke kong.Error
if errors.As(err, &ke) {
status = ke.Status
}
if err = c.JSON(
http.StatusInternalServerError,
status,
render.MSI{
"request": rID,
"uri": uri,

Loading…
Cancel
Save