Documentation
¶
Index ¶
- Constants
- Variables
- func BodyDecodeErrorHandler(w http.ResponseWriter, err error, encoder gonethttpresponse.Encoder) error
- func CheckContentType(r *http.Request) bool
- func NewMaxBodySizeExceededErrorResponse(limit int64) gonethttpresponse.Response
- func NewSyntaxErrorResponse(offset int64) gonethttpresponse.Response
- func NewUnknownFieldErrorResponse(fieldName string) gonethttpresponse.Response
- func NewUnmarshalTypeErrorResponse(fieldName string, fieldTypeName string) gonethttpresponse.Response
- type Decoder
- type StreamDecoder
Constants ¶
const ( ErrMaxBodySizeExceeded = "json body size exceeds the maximum allowed size, limit is %d bytes" ErrSyntaxError = "json body contains badly-formed JSON at position %d" ErrUnknownField = "json body contains an unknown field %s" )
Variables ¶
var ( ErrCodeInvalidContentType *string ErrCodeNilDestination *string ErrCodeFailedToReadBody *string ErrCodeUnmarshalRequestBodyFailed *string ErrCodeSyntaxError *string ErrCodeUnmarshalTypeError *string ErrCodeUnknownField *string ErrCodeEmptyBody *string ErrCodeMaxBodySizeExceeded *string )
var ( ErrUnexpectedEOF = errors.New("json body contains badly-formed JSON") ErrEmptyBody = errors.New("json body is empty") ErrUnmarshalBodyFailed = errors.New("failed to unmarshal json body") )
var ( ErrNilDestination = errors.New("json destination is nil") ErrInvalidContentType = gonethttpresponse.NewFailResponseError( "Content-Type", "invalid content type, expected application/json", ErrCodeInvalidContentType, http.StatusUnsupportedMediaType, ) )
Functions ¶
func BodyDecodeErrorHandler ¶
func BodyDecodeErrorHandler( w http.ResponseWriter, err error, encoder gonethttpresponse.Encoder, ) error
BodyDecodeErrorHandler handles the error on JSON body decoding
Parameters:
- w: The HTTP response writer
- err: The error that occurred during decoding
- encoder: The encoder to use for the response
Returns:
- error: An error if the encoder is nil or if encoding the response fails
func CheckContentType ¶
CheckContentType checks if the content type is JSON
Parameters:
- r: The HTTP request
Returns:
- bool: True if the content type is JSON, false otherwise
func NewMaxBodySizeExceededErrorResponse ¶
func NewMaxBodySizeExceededErrorResponse(limit int64) gonethttpresponse.Response
NewMaxBodySizeExceededErrorResponse creates a new response for a body size exceeded error
Parameters:
- limit: The maximum allowed body size
Returns:
- gonethttpresponse.Response: The response
func NewSyntaxErrorResponse ¶
func NewSyntaxErrorResponse( offset int64, ) gonethttpresponse.Response
NewSyntaxErrorResponse creates a new response for a SyntaxError
Parameters:
- offset: The offset where the error occurred
func NewUnknownFieldErrorResponse ¶
func NewUnknownFieldErrorResponse(fieldName string) gonethttpresponse.Response
NewUnknownFieldErrorResponse creates a new response for an unknown field error
Parameters:
- fieldName: The name of the unknown field
Returns:
- gonethttpresponse.Response: The response
func NewUnmarshalTypeErrorResponse ¶
func NewUnmarshalTypeErrorResponse( fieldName string, fieldTypeName string, ) gonethttpresponse.Response
NewUnmarshalTypeErrorResponse creates a new response for an UnmarshalTypeError
Parameters:
- fieldName: The name of the field that caused the error
- fieldTypeName: The type name of the field that caused the error
Types ¶
type Decoder ¶ added in v0.11.2
type Decoder struct {
// contains filtered or unexported fields
}
Decoder struct
func NewDecoder ¶ added in v0.11.2
func NewDecoder( mode *goflagsmode.Flag, encoder gonethttpresponse.Encoder, ) (*Decoder, error)
NewDecoder creates a new JSON decoder
Parameters:
- mode: The flag mode
- encoder: The JSON encoder
Returns:
- *Decoder: The default decoder
- error: The error if any
type StreamDecoder ¶ added in v0.11.2
type StreamDecoder struct {
// contains filtered or unexported fields
}
StreamDecoder is the JSON decoder struct
func NewStreamDecoder ¶ added in v0.11.2
func NewStreamDecoder( mode *goflagsmode.Flag, encoder gonethttpresponse.Encoder, ) (*StreamDecoder, error)
NewStreamDecoder creates a new JSON decoder
Parameters:
- mode: The flag mode
- encoder: The JSON encoder
Returns:
- *StreamDecoder: The default decoder
- error: The error if any
func (StreamDecoder) Decode ¶ added in v0.11.2
func (d StreamDecoder) Decode( w http.ResponseWriter, r *http.Request, dest interface{}, ) (err error)
Decode decodes the JSON request body and stores it in the destination
Parameters:
- w: The HTTP response writer
- r: The HTTP request
- dest: The destination to store the decoded body
Returns:
- error: The error if any