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 DefaultDecoder
- type DefaultStreamDecoder
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 ( 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 DefaultDecoder ¶
type DefaultDecoder struct {
// contains filtered or unexported fields
}
DefaultDecoder struct
func NewDefaultDecoder ¶
func NewDefaultDecoder( mode *goflagsmode.Flag, encoder gonethttpresponse.Encoder, ) (*DefaultDecoder, error)
NewDefaultDecoder creates a new JSON decoder
Parameters:
- mode: The flag mode
- encoder: The JSON encoder
Returns:
- *DefaultDecoder: The default decoder
- error: The error if any
func (DefaultDecoder) Decode ¶
func (d DefaultDecoder) 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
type DefaultStreamDecoder ¶
type DefaultStreamDecoder struct {
// contains filtered or unexported fields
}
DefaultStreamDecoder is the JSON decoder struct
func NewDefaultStreamDecoder ¶
func NewDefaultStreamDecoder( mode *goflagsmode.Flag, encoder gonethttpresponse.Encoder, ) (*DefaultStreamDecoder, error)
NewDefaultStreamDecoder creates a new JSON decoder
Parameters:
- mode: The flag mode
- encoder: The JSON encoder
Returns:
- *DefaultStreamDecoder: The default decoder
- error: The error if any
func (DefaultStreamDecoder) Decode ¶
func (d DefaultStreamDecoder) 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