Documentation ¶
Index ¶
- type InvalidCode
- type Render
- func (r *Render) BadRequest(msg string) error
- func (r *Render) DBError(err error) error
- func (r *Render) EscapeHTML() *Render
- func (r *Render) Forbidden(msg string) error
- func (r *Render) HTML(code int, body string) error
- func (r *Render) HandleError(re *ResponseError) error
- func (r *Render) InternalServerError(msg string) error
- func (r *Render) JSON(code int, body interface{}) error
- func (r *Render) NoCache() *Render
- func (r *Render) NoContent() error
- func (r *Render) NotFound(msg string) error
- func (r *Render) OK(body interface{}) error
- func (r *Render) Text(code int, body string) error
- func (r *Render) TooManyRequests(msg string) error
- func (r *Render) Unauthorized(msg string) error
- func (r *Render) Unprocessable(ve *ValidationError) error
- type ResponseError
- func NewAlreadyExists(field string) *ResponseError
- func NewBadRequest(msg string) *ResponseError
- func NewDBError(err error) *ResponseError
- func NewForbidden(msg string) *ResponseError
- func NewInternalError(msg string) *ResponseError
- func NewNotFound(msg string) *ResponseError
- func NewResponseError(code int, msg string) *ResponseError
- func NewTooManyRequests(msg string) *ResponseError
- func NewUnauthorized(msg string) *ResponseError
- func NewUnprocessable(ve *ValidationError) *ResponseError
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type InvalidCode ¶
type InvalidCode string
InvalidCode is the reason why validation failed.
const ( // CodeMissing means a resource does not exist CodeMissing InvalidCode = "missing" // CodeMissingField means a required field on a resource has not been set. CodeMissingField InvalidCode = "missing_field" // CodeInvalid means the formatting of a field is invalid CodeInvalid InvalidCode = "invalid" // CodeAlreadyExists means another resource has the same value as this field. CodeAlreadyExists InvalidCode = "already_exists" )
type Render ¶
type Render struct {
// contains filtered or unexported fields
}
Render wraps http.ResponseWrtier and configuration of how to send reponse content.
func New ¶
func New(w http.ResponseWriter) *Render
New createa a new instance of Render. By default, HTML in the content is not escaped, and JSON is indented with a tab.
func (*Render) BadRequest ¶
BadRequest sends 400 reponse.
func (*Render) HandleError ¶
func (r *Render) HandleError(re *ResponseError) error
HandleError sends response above 400
func (*Render) InternalServerError ¶
InternalServerError sends 500 response.
func (*Render) TooManyRequests ¶
TooManyRequests sends 429 response.
func (*Render) Unauthorized ¶
Unauthorized sends 401 Unauthorized response.
func (*Render) Unprocessable ¶
func (r *Render) Unprocessable(ve *ValidationError) error
Unprocessable sends 422 response
type ResponseError ¶
type ResponseError struct { StatusCode int `json:"-"` Message string `json:"message"` Invalid *ValidationError `json:"error,omitempty"` }
ResponseError is the response body for http code above 400.
func NewAlreadyExists ¶
func NewAlreadyExists(field string) *ResponseError
NewAlreadyExists is a convenience func to handle MySQL 1062 error.
func NewBadRequest ¶
func NewBadRequest(msg string) *ResponseError
NewBadRequest creates a new Response for 400 Bad Request with the specified msg
func NewDBError ¶
func NewDBError(err error) *ResponseError
NewDBError handles various errors returned from the model layer MySQL duplicate error when inserting into uniquely constraint column; ErrNoRows if it cannot retrieve any rows of the specified criteria; `field` is used to identify which field is causing duplicate error.
func NewForbidden ¶
func NewForbidden(msg string) *ResponseError
NewForbidden creates response for 403
func NewInternalError ¶
func NewInternalError(msg string) *ResponseError
NewInternalError creates response for internal server error
func NewNotFound ¶
func NewNotFound(msg string) *ResponseError
NewNotFound creates response 404 Not Found
func NewResponseError ¶
func NewResponseError(code int, msg string) *ResponseError
NewResponseError creates a new ResponseError instance.
func NewTooManyRequests ¶
func NewTooManyRequests(msg string) *ResponseError
NewTooManyRequests respond to rate limit.
func NewUnauthorized ¶
func NewUnauthorized(msg string) *ResponseError
NewUnauthorized create a new instance of Response for 401 Unauthorized response
func NewUnprocessable ¶
func NewUnprocessable(ve *ValidationError) *ResponseError
NewUnprocessable creates response 422 Unprocessable Entity
func (*ResponseError) Error ¶
func (re *ResponseError) Error() string
type ValidationError ¶
type ValidationError struct { Message string `json:"-"` Field string `json:"field"` Code InvalidCode `json:"code"` }
ValidationError tells the field that failed validation.
func NewVEAlreadyExists ¶ added in v0.4.7
func NewVEAlreadyExists(field string) *ValidationError
NewVEAlreadyExists creates a ValidationError for MySQL unique key constraint failure.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string