Documentation ¶
Index ¶
- Variables
- func IsJSONError(err error) bool
- type DefaultImpl
- func (d *DefaultImpl) ErrorCode(r *http.Request, w http.ResponseWriter, rLogger logrus.FieldLogger, err error, ...)
- func (d *DefaultImpl) HandleError(r *http.Request, w http.ResponseWriter, err error)
- func (*DefaultImpl) IDFromRequest(r *http.Request) (uint, error)
- func (d *DefaultImpl) Logger(r *http.Request) logrus.FieldLogger
- func (*DefaultImpl) MustJSON(_ *http.Request, w http.ResponseWriter, v interface{})
- func (d *DefaultImpl) WriteError(r *http.Request, w http.ResponseWriter, code int, err error, msg string)
- func (d *DefaultImpl) WriteInternalServerError(r *http.Request, w http.ResponseWriter, err error, msg string)
- func (d *DefaultImpl) WriteInvalidMethodError(r *http.Request, w http.ResponseWriter)
- func (d *DefaultImpl) WriteInvalidParameterError(r *http.Request, w http.ResponseWriter, err error)
- func (d *DefaultImpl) WriteJSONEncodeError(r *http.Request, w http.ResponseWriter, err error)
- func (*DefaultImpl) WriteResponseFile(_ *http.Request, w http.ResponseWriter, filename string, content []byte)
- func (d *DefaultImpl) WriteResponseJSON(r *http.Request, w http.ResponseWriter, res interface{})
- type Errors
- type JSONError
- type Utils
Constants ¶
This section is empty.
Variables ¶
var ( ErrParamIDRequired = model.ValidationError{Err: errors.New("id parameter is required")} ErrRequestBodyRequired = model.ValidationError{Err: errors.New("request body required")} ErrRequestBodyJSONInvalid = model.ValidationError{Err: errors.New("request body contains malformed JSON")} )
Functions ¶
func IsJSONError ¶
Types ¶
type DefaultImpl ¶
type DefaultImpl struct {
// contains filtered or unexported fields
}
func NewDefaultHelper ¶
func NewDefaultHelper(logger logrus.FieldLogger) *DefaultImpl
func (*DefaultImpl) ErrorCode ¶
func (d *DefaultImpl) ErrorCode(r *http.Request, w http.ResponseWriter, rLogger logrus.FieldLogger, err error, code int)
ErrorCode replies to the request with the specified error message as JSON-encoded body and writes corresponding message to the log.
If HTTP code is less than or equal zero, it will be deduced based on the error. If it fails, StatusInternalServerError will be returned without the response body. The error can be of 'multierror.Error' type.
The call writes messages with the debug log level except the case when the code is StatusInternalServerError which is logged as an error.
It does not end the HTTP request; the caller should ensure no further writes are done to w.
func (*DefaultImpl) HandleError ¶
func (d *DefaultImpl) HandleError(r *http.Request, w http.ResponseWriter, err error)
HandleError replies to the request with an appropriate message as JSON-encoded body and writes a corresponding message to the log with debug log level.
Any error of a type not defined in this package or pkg/model, will be treated as an internal server error causing response code 500. Such errors are not sent but only logged with error log level.
func (*DefaultImpl) IDFromRequest ¶
func (*DefaultImpl) IDFromRequest(r *http.Request) (uint, error)
func (*DefaultImpl) Logger ¶
func (d *DefaultImpl) Logger(r *http.Request) logrus.FieldLogger
Logger creates a new logger scoped to the request and enriches it with the known fields.
func (*DefaultImpl) MustJSON ¶
func (*DefaultImpl) MustJSON(_ *http.Request, w http.ResponseWriter, v interface{})
func (*DefaultImpl) WriteError ¶
func (d *DefaultImpl) WriteError(r *http.Request, w http.ResponseWriter, code int, err error, msg string)
func (*DefaultImpl) WriteInternalServerError ¶
func (d *DefaultImpl) WriteInternalServerError(r *http.Request, w http.ResponseWriter, err error, msg string)
func (*DefaultImpl) WriteInvalidMethodError ¶
func (d *DefaultImpl) WriteInvalidMethodError(r *http.Request, w http.ResponseWriter)
func (*DefaultImpl) WriteInvalidParameterError ¶
func (d *DefaultImpl) WriteInvalidParameterError(r *http.Request, w http.ResponseWriter, err error)
func (*DefaultImpl) WriteJSONEncodeError ¶
func (d *DefaultImpl) WriteJSONEncodeError(r *http.Request, w http.ResponseWriter, err error)
func (*DefaultImpl) WriteResponseFile ¶
func (*DefaultImpl) WriteResponseFile(_ *http.Request, w http.ResponseWriter, filename string, content []byte)
func (*DefaultImpl) WriteResponseJSON ¶
func (d *DefaultImpl) WriteResponseJSON(r *http.Request, w http.ResponseWriter, res interface{})
type Utils ¶
type Utils interface { // these methods were originally extracted from api package MustJSON(r *http.Request, w http.ResponseWriter, v interface{}) HandleError(r *http.Request, w http.ResponseWriter, err error) IDFromRequest(r *http.Request) (uint, error) Logger(r *http.Request) logrus.FieldLogger // these methods were originally extracted from server package WriteResponseJSON(r *http.Request, w http.ResponseWriter, res interface{}) WriteResponseFile(r *http.Request, w http.ResponseWriter, filename string, content []byte) WriteInvalidMethodError(r *http.Request, w http.ResponseWriter) WriteInvalidParameterError(r *http.Request, w http.ResponseWriter, err error) WriteInternalServerError(r *http.Request, w http.ResponseWriter, err error, msg string) WriteJSONEncodeError(r *http.Request, w http.ResponseWriter, err error) WriteError(r *http.Request, w http.ResponseWriter, code int, err error, msg string) }
TODO(petethepig): this interface is pretty large, we can probably simplify it, some methods do pretty similar things