view

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: 3 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// CodeMissing means a resource does not exist
	CodeMissing = "missing"
	// CodeMissingField means a required field on a resource has not been set.
	CodeMissingField = "missing_field"
	// CodeInvalid means the formatting of a field is invalid
	CodeInvalid = "invalid"
	// CodeAlreadyExists means another resource has the same value as this field.
	CodeAlreadyExists = "already_exists"
)

Variables

This section is empty.

Functions

func JSON added in v0.3.2

func JSON(w http.ResponseWriter, resp Response) error

JSON respond to restful request.

func Render

func Render(w http.ResponseWriter, resp Response) error

Render responds to client request

Types

type ClientError

type ClientError struct {
	Message string  `json:"message"`
	Reason  *Reason `json:"error,omitempty"`
	// Integrate Stripe errors
	Code  string `json:"code,omitempty"`  // For some errors that could be handled programmatically, a short string indicating the error code reported.
	Param string `json:"param,omitempty"` // If the error is parameter-specific, the parameter related to the error. For example, you can use this to display a message near the correct form field.
	Type  string `json:"type,omitempty"`  // The type of error returned. One of api_connection_error, api_error, authentication_error, card_error, idempotency_error, invalid_request_error, or rate_limit_error
}

ClientError respond to 4xx http status.

type Reason

type Reason struct {
	Message string `json:"-"`
	Field   string `json:"field"`
	Code    string `json:"code"`
	// contains filtered or unexported fields
}

Reason tells why its unprocessable. Mostly used for validation errors.

func NewInvalid added in v0.0.18

func NewInvalid(m string) *Reason

NewInvalid creates a new instance of invalid reason.

func NewReason

func NewReason() *Reason

NewReason creates a new instance of Reason

func (*Reason) GetMessage

func (r *Reason) GetMessage() string

GetMessage returns Reason's descriptive message.

func (*Reason) SetMessage

func (r *Reason) SetMessage(msg string)

SetMessage set the message to be carried away.

type Response

type Response struct {
	StatusCode int
	Header     http.Header
	Body       interface{}
}

Response collects all data needed for an HTTP response

func NewBadRequest

func NewBadRequest(msg string) Response

NewBadRequest creates a new Response for 400 Bad Request with the specified msg

func NewDBFailure

func NewDBFailure(err error) Response

NewDBFailure handles various errors returned from the model layter MySQL duplicate error when inerting 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) Response

NewForbidden creates response for 403

func NewInternalError

func NewInternalError(msg string) Response

NewInternalError creates response for internal server error

func NewNoContent

func NewNoContent() Response

NewNoContent creates an HTTP 204 No Content response

func NewNotFound

func NewNotFound() Response

NewNotFound creates response 404 Not Found

func NewResponse

func NewResponse() Response

NewResponse creates a new instance of Response with default values

func NewTooManyRequests added in v0.0.5

func NewTooManyRequests(msg string) Response

NewTooManyRequests respond to rate limit.

func NewUnauthorized

func NewUnauthorized(msg string) Response

NewUnauthorized create a new instance of Response for 401 Unauthorized response

func NewUnauthorizedBasic added in v0.0.17

func NewUnauthorizedBasic(r *Reason) Response

NewUnauthorizedBasic creates a new instance of Response for 401 Unauthorized with WWW-Authenticate header.

func NewUnprocessable

func NewUnprocessable(r *Reason) Response

NewUnprocessable creates response 422 Unprocessable Entity

func (Response) NoCache

func (r Response) NoCache() Response

NoCache set headers to prevent the response from being cached.

func (Response) SetBody

func (r Response) SetBody(b interface{}) Response

SetBody sets reponse body to any value

Jump to

Keyboard shortcuts

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