diff options
Diffstat (limited to 'api/dto/response.go')
| -rw-r--r-- | api/dto/response.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/api/dto/response.go b/api/dto/response.go new file mode 100644 index 0000000..2c13218 --- /dev/null +++ b/api/dto/response.go @@ -0,0 +1,38 @@ +package dto + +import "fmt" + +type Response interface { + Error() string + Status() int +} + +type OkResponse struct { + Code int `json:"code"` + Data interface{} `json:"data,omitempty"` +} + +var _ Response = (*OkResponse)(nil) + +type ErrorResponse struct { + Code int `json:"code"` + Message string `json:"message"` +} + +var _ Response = (*ErrorResponse)(nil) + +func (r *OkResponse) Status() int { + return r.Code +} + +func (r *OkResponse) Error() string { + return fmt.Sprintf("HTTP status %d", r.Code) +} + +func (r *ErrorResponse) Status() int { + return r.Code +} + +func (r *ErrorResponse) Error() string { + return fmt.Sprintf("HTTP status %d: %s", r.Code, r.Message) +} |
