Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var GetDefaultCauseView = func() CauseView { return &DefaultCauseView{} }
GetDefaultCauseView provides function to create an empty instance of DefaultCauseView.
var GetDefaultErrView = func() ErrView { return &DefaultErrView{} }
GetDefaultErrView provides function to create an empty instance of DefaultErrView.
Functions ¶
This section is empty.
Types ¶
type Cause ¶
type Cause interface { // Param returns param name of the error cause. Param() string // Value returns param value of the error cause. Value() string // Message returns user friendly error cause message. Message() string }
Cause encapsulates an HTTP error causes and details.
type CauseView ¶
type CauseView interface { SetParam(param string) SetValue(value string) SetMessage(message string) }
CauseView allows to define the error cause format used in the body of the HTTP response. The default CauseView implementation is DefaultCauseView.
type DefaultCauseView ¶
type DefaultCauseView struct { Param string `json:"param,omitempty"` Value string `json:"value,omitempty"` Message string `json:"message,omitempty"` }
DefaultCauseView is a default implementation of CauseView.
Example of the default format:
{ "param":"role", "value":"guest", "message":"Invalid role." }
func (*DefaultCauseView) SetMessage ¶
func (cv *DefaultCauseView) SetMessage(message string)
SetMessage sets the cause message to the view.
func (*DefaultCauseView) SetParam ¶
func (cv *DefaultCauseView) SetParam(param string)
SetParam sets the cause param name to the view.
func (*DefaultCauseView) SetValue ¶
func (cv *DefaultCauseView) SetValue(value string)
SetValue sets the cause param value to the view.
type DefaultErrView ¶
type DefaultErrView struct { Error struct { Code string `json:"code"` Message string `json:"message"` } `json:"error"` RequestID string `json:"request_id,omitempty"` Causes []CauseView `json:"causes,omitempty"` }
DefaultErrView is a default implementation of ErrView.
Example of the default format:
{ "error":{ "code": "022-044", "message": "User is not allowed to see the application." }, "request_id": "c3057227-feb5-4b64-b0fd-dfb83714c7df", "causes": [ { "param": "role", "value": "guest", "message": "Invalid role." } ] }
func (*DefaultErrView) SetCauses ¶
func (ev *DefaultErrView) SetCauses(causes []CauseView)
SetCauses sets error causes to the view.
func (*DefaultErrView) SetCode ¶
func (ev *DefaultErrView) SetCode(code string)
SetCode sets the error code to the view.
func (*DefaultErrView) SetMessage ¶
func (ev *DefaultErrView) SetMessage(message string)
SetMessage sets the error message to the view.
func (*DefaultErrView) SetRequestID ¶
func (ev *DefaultErrView) SetRequestID(requestID string)
SetRequestID sets the request ID to the view.
type Err ¶
type Err interface { error // HTTPStatusCode returns status code that a HTTP handler should return. HTTPStatusCode() int // Code returns unique error code. Code() string // Message returns user friendly error message. Message() string // RequestID returns ID of the HTTP request. RequestID() string // Causes returns error's causes and details. Causes() []Cause // SetRequestID sets ID of the HTTP request. SetRequestID(requestID string) // AddCause adds cause or detail to the error. AddCause(param, value, message string) }
Err encapsulates an error returned by an HTTP service.
type ErrView ¶
type ErrView interface { SetCode(code string) SetMessage(message string) SetRequestID(requestID string) SetCauses(causes []CauseView) }
ErrView allows to define the error format used in the body of the HTTP response. The default ErrView implementation is DefaultErrView.
type GetCauseView ¶
type GetCauseView func() CauseView
GetCauseView is a function to create an empty instance of CauseView.
type GetErrView ¶
type GetErrView func() ErrView
GetErrView is a function to create an empty instance of ErrView.
type Presenter ¶
type Presenter struct {
// contains filtered or unexported fields
}
Presenter helps to handle errors and return an HTTP Response.
func CreatePresenter ¶
func CreatePresenter(config PresenterConfig) Presenter
CreatePresenter creates a new Presenter using the given configuration. If some configuration parameters are not specified, the default values are used.
func (Presenter) ErrToJSON ¶
ErrToJSON marshalls the error to JSON using ErrView and CauseView formats.
func (Presenter) WriteJSONResponse ¶
func (p Presenter) WriteJSONResponse(err Err, w http.ResponseWriter) error
WriteJSONResponse writes the HTTP error to ResponseWriter using ErrView and CauseView formats.
type PresenterConfig ¶
type PresenterConfig struct { // GetErrView is a function to create an empty instance of ErrView. // To use custom error format, implement ErrView and pass a function that creates an empty instance of that imlementation. // The default value is GetDefaultErrView. GetErrView GetErrView // GetCauseView is a function to create an empty instance of CauseView. // To use custom cause format, implement CauseView and pass a function that creates an empty instance of that imlementation. // The default value is GetDefaultCauseView. GetCauseView GetCauseView }
PresenterConfig is used to pass configuration options to Presenter's constructor.