Documentation ¶
Overview ¶
Package response defines the how the default microservice response must look and behave like.
Index ¶
- func UnmarshalJSONResponse(d []byte, dst interface{}) error
- func WriteTo(code int, i interface{}, w http.ResponseWriter) error
- type Data
- type Responder
- type Response
- func ConflictErr(msg string) *Response
- func DBError(err error) *Response
- func DBErrorf(format string, err error) *Response
- func InternalError(err error) *Response
- func JSONError(err error) *Response
- func NotFoundErr(msg string) *Response
- func ParamError(name string) *Response
- func ValidationError(err error, name string) *Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func UnmarshalJSONResponse ¶
UnmarshalJSONResponse will unmarshal the data from legacy response envelope.
Types ¶
type Data ¶
type Data struct { Type string Content interface{} }
Data represents the collection data the the response will return to the consumer. Type ends up being the name of the key containing the collection of Content
func (*Data) MarshalJSON ¶
MarshalJSON implements the Marshaler interface and is there to ensure the output is correct when we return data to the consumer
func (*Data) UnmarshalJSON ¶
UnmarshalJSON implements the Unmarshaler interface this implementation will fill the type in the case we're been provided a valid single collection and set the content to the contents of said collection. for every other options, it behaves like normal. Despite the fact that we are not supposed to marshal without a type set, this is purposefully left open to unmarshal without a collection name set, in case you may want to set it later, and for interop with other systems which may not send the collection properly.
type Responder ¶
type Responder interface {
WriteTo(w http.ResponseWriter) error
}
Responder defines the behaviour of a response for JSON over HTTP.
type Response ¶
type Response struct { Code int `json:"code"` // Any valid HTTP response code Message string `json:"message"` // Any relevant message (optional) Data *Data `json:"data,omitempty"` // Data to pass along to the response (optional) Pagination *pagination.Response `json:"pagination,omitempty"` // Pagination data }
Response defines a JSON response body over HTTP.
func ConflictErr ¶
ConflictErr returns a prepared 409 Conflict response, including the message passed by the user in the message field of the response object.
func DBErrorf ¶
DBErrorf returns a prepared 500 Internal Server Error response, using the user provided formatted message.
func InternalError ¶
InternalError returns a prepared 500 Internal Server Error, including the error message in the message field of the response object
func JSONError ¶
JSONError returns a prepared 422 Unprocessable Entity response if the JSON is found to contain syntax errors, or invalid values for types.
func NotFoundErr ¶
NotFoundErr returns a prepared 404 Not Found response, including the message passed by the user in the message field of the response object.
func ParamError ¶
ParamError returns a prepared 422 Unprocessable Entity response, including the name of the failing parameter in the message field of the response object.
func ValidationError ¶
ValidationError returns a prepared 422 Unprocessable Entity response, including the name of the failing validation/validator in the message field of the response object.