render

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2021 License: MIT Imports: 4 Imported by: 10

Documentation

Index

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

func (r *Render) BadRequest(msg string) error

BadRequest sends 400 reponse.

func (*Render) DBError

func (r *Render) DBError(err error) error

DBError sends 404 or 500 response.

func (*Render) EscapeHTML

func (r *Render) EscapeHTML() *Render

EscapeHTML set escaping HTML.

func (*Render) Forbidden

func (r *Render) Forbidden(msg string) error

Forbidden sends 403 response.

func (*Render) HTML added in v0.7.0

func (r *Render) HTML(code int, body string) error

func (*Render) HandleError

func (r *Render) HandleError(re *ResponseError) error

HandleError sends response above 400

func (*Render) InternalServerError

func (r *Render) InternalServerError(msg string) error

InternalServerError sends 500 response.

func (*Render) JSON

func (r *Render) JSON(code int, body interface{}) error

JSON renders JSON response.

func (*Render) NoCache

func (r *Render) NoCache() *Render

NoCache set headers to avoid caching repsonse.

func (*Render) NoContent

func (r *Render) NoContent() error

NoContent send a 204 response.

func (*Render) NotFound

func (r *Render) NotFound(msg string) error

NotFound sends 404 Not Found response.

func (*Render) OK

func (r *Render) OK(body interface{}) error

OK sends 200 OK response for JSON.

func (*Render) Text added in v0.7.0

func (r *Render) Text(code int, body string) error

func (*Render) TooManyRequests

func (r *Render) TooManyRequests(msg string) error

TooManyRequests sends 429 response.

func (*Render) Unauthorized

func (r *Render) Unauthorized(msg string) error

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL