forked from xlab/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
62 lines (57 loc) · 1.91 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package http
import (
"net/http"
"goa.design/goa"
)
type (
// ErrorResponse is the data structure encoded in HTTP responses that
// correspond to errors created by the generated code. This struct is
// mainly intended for clients to decode error responses.
ErrorResponse struct {
// Name is a name for that class of errors.
Name string `json:"name" xml:"name" form:"name"`
// ID is the unique error instance identifier.
ID string `json:"id" xml:"id" form:"id"`
// Message describes the specific error occurrence.
Message string `json:"message" xml:"message" form:"message"`
// Temporary indicates whether the error is temporary.
Temporary bool `json:"temporary" xml:"temporary" form:"temporary"`
// Timeout indicates whether the error is a timeout.
Timeout bool `json:"timeout" xml:"timeout" form:"timeout"`
// Fault indicates whether the error is a server-side fault.
Fault bool `json:"fault" xml:"fault" form:"fault"`
}
)
// NewErrorResponse creates a HTTP response from the given error.
func NewErrorResponse(err error) *ErrorResponse {
if gerr, ok := err.(*goa.ServiceError); ok {
return &ErrorResponse{
Name: gerr.Name,
ID: gerr.ID,
Message: gerr.Message,
Timeout: gerr.Timeout,
Temporary: gerr.Temporary,
Fault: gerr.Fault,
}
}
return NewErrorResponse(goa.Fault(err.Error()))
}
// StatusCode implements a heuristic that computes a HTTP response status code
// appropriate for the timeout, temporary and fault characteristics of the
// error. This method is used by the generated server code when the error is not
// described explicitly in the design.
func (resp *ErrorResponse) StatusCode() int {
if resp.Fault {
return http.StatusInternalServerError
}
if resp.Timeout {
if resp.Temporary {
return http.StatusGatewayTimeout
}
return http.StatusRequestTimeout
}
if resp.Temporary {
return http.StatusServiceUnavailable
}
return http.StatusBadRequest
}