Documentation ¶
Index ¶
- type APIError
- type APIResponse
- type ErrorCode
- type JSONWriter
- func (j *JSONWriter) BadRequest(w http.ResponseWriter, r *http.Request, apiError APIError)
- func (j *JSONWriter) DefaultError(w http.ResponseWriter, r *http.Request)
- func (j *JSONWriter) Error(w http.ResponseWriter, r *http.Request, apiError APIError, httpStatus int)
- func (j *JSONWriter) Forbidden(w http.ResponseWriter, r *http.Request, apiError APIError)
- func (j *JSONWriter) Internal(w http.ResponseWriter, r *http.Request, apiError APIError)
- func (j *JSONWriter) NotFound(w http.ResponseWriter, r *http.Request, apiError APIError)
- func (j *JSONWriter) Ok(w http.ResponseWriter, r *http.Request, data interface{})
- func (j *JSONWriter) Unauthorized(w http.ResponseWriter, r *http.Request, apiError APIError)
- func (j *JSONWriter) UnprocessableEntity(w http.ResponseWriter, r *http.Request, apiError APIError)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIResponse ¶
type APIResponse interface {
Data() interface{}
}
APIResponse is to be implemented for sending a response to the client
type ErrorCode ¶
type ErrorCode int
const ( // DefaultErrorCode - in case other codes are irrelevant DefaultErrorCode ErrorCode = 500 // EmptyRequestBody - when the request body is empty EmptyRequestBody ErrorCode = 1000 // InvalidJSON - when the json data in request in invalid InvalidJSON ErrorCode = 1001 // InvalidJSONField - when a json field in request in invalid InvalidJSONField ErrorCode = 1002 // UnKnownJSONField - when json request body contains unnecessary field for the request UnKnownJSONField ErrorCode = 1003 // RequestSizeExceeds - when request body's size is large RequestSizeExceeds ErrorCode = 1004 // UnknownParseError - when the parse error is none of the above categories UnknownParseError ErrorCode = 1005 // ValidationFailed - when the parse error is none of the above categories ValidationFailed ErrorCode = 1006 )
type JSONWriter ¶
type JSONWriter struct {
// contains filtered or unexported fields
}
JSONWriter has functions for sending JSON response to client
func NewJSONWriter ¶
func NewJSONWriter(log *zap.Logger) *JSONWriter
NewJSONWriter creates a new instance of JSONWriter
func (*JSONWriter) BadRequest ¶
func (j *JSONWriter) BadRequest(w http.ResponseWriter, r *http.Request, apiError APIError)
BadRequest sends error to client with http status 400
func (*JSONWriter) DefaultError ¶
func (j *JSONWriter) DefaultError(w http.ResponseWriter, r *http.Request)
DefaultError sends unknown error to client with http status 500
func (*JSONWriter) Error ¶
func (j *JSONWriter) Error(w http.ResponseWriter, r *http.Request, apiError APIError, httpStatus int)
Error sends error to client with the given http status
func (*JSONWriter) Forbidden ¶
func (j *JSONWriter) Forbidden(w http.ResponseWriter, r *http.Request, apiError APIError)
Forbidden sends error to client with http status 403
func (*JSONWriter) Internal ¶
func (j *JSONWriter) Internal(w http.ResponseWriter, r *http.Request, apiError APIError)
Internal sends error to client with http status 500
func (*JSONWriter) NotFound ¶
func (j *JSONWriter) NotFound(w http.ResponseWriter, r *http.Request, apiError APIError)
NotFound sends error to client with http status 404
func (*JSONWriter) Ok ¶
func (j *JSONWriter) Ok(w http.ResponseWriter, r *http.Request, data interface{})
Ok sends the data to client with http status 200
func (*JSONWriter) Unauthorized ¶
func (j *JSONWriter) Unauthorized(w http.ResponseWriter, r *http.Request, apiError APIError)
Unauthorized sends error to client with http status 401
func (*JSONWriter) UnprocessableEntity ¶
func (j *JSONWriter) UnprocessableEntity(w http.ResponseWriter, r *http.Request, apiError APIError)
UnprocessableEntity sends error to client with http status 422