@@ -11,6 +11,8 @@ import (
11
11
"strings"
12
12
13
13
"github.com/go-playground/validator/v10"
14
+
15
+ "github.com/coder/coder/codersdk"
14
16
)
15
17
16
18
var (
@@ -50,42 +52,16 @@ func init() {
50
52
}
51
53
}
52
54
53
- // Response represents a generic HTTP response.
54
- type Response struct {
55
- // Message is an actionable message that depicts actions the request took.
56
- // These messages should be fully formed sentences with proper punctuation.
57
- // Examples:
58
- // - "A user has been created."
59
- // - "Failed to create a user."
60
- Message string `json:"message"`
61
- // Detail is a debug message that provides further insight into why the
62
- // action failed. This information can be technical and a regular golang
63
- // err.Error() text.
64
- // - "database: too many open connections"
65
- // - "stat: too many open files"
66
- Detail string `json:"detail,omitempty"`
67
- // Validations are form field-specific friendly error messages. They will be
68
- // shown on a form field in the UI. These can also be used to add additional
69
- // context if there is a set of errors in the primary 'Message'.
70
- Validations []Error `json:"validations,omitempty"`
71
- }
72
-
73
- // Error represents a scoped error to a user input.
74
- type Error struct {
75
- Field string `json:"field" validate:"required"`
76
- Detail string `json:"detail" validate:"required"`
77
- }
78
-
79
55
// ResourceNotFound is intentionally vague. All 404 responses should be identical
80
56
// to prevent leaking existence of resources.
81
57
func ResourceNotFound (rw http.ResponseWriter ) {
82
- Write (rw , http .StatusNotFound , Response {
58
+ Write (rw , http .StatusNotFound , codersdk. Response {
83
59
Message : "Resource not found or you do not have access to this resource" ,
84
60
})
85
61
}
86
62
87
63
func Forbidden (rw http.ResponseWriter ) {
88
- Write (rw , http .StatusForbidden , Response {
64
+ Write (rw , http .StatusForbidden , codersdk. Response {
89
65
Message : "Forbidden." ,
90
66
})
91
67
}
@@ -114,7 +90,7 @@ func Write(rw http.ResponseWriter, status int, response interface{}) {
114
90
func Read (rw http.ResponseWriter , r * http.Request , value interface {}) bool {
115
91
err := json .NewDecoder (r .Body ).Decode (value )
116
92
if err != nil {
117
- Write (rw , http .StatusBadRequest , Response {
93
+ Write (rw , http .StatusBadRequest , codersdk. Response {
118
94
Message : "Request body must be valid JSON." ,
119
95
Detail : err .Error (),
120
96
})
@@ -123,21 +99,21 @@ func Read(rw http.ResponseWriter, r *http.Request, value interface{}) bool {
123
99
err = validate .Struct (value )
124
100
var validationErrors validator.ValidationErrors
125
101
if errors .As (err , & validationErrors ) {
126
- apiErrors := make ([]Error , 0 , len (validationErrors ))
102
+ apiErrors := make ([]codersdk. ValidationError , 0 , len (validationErrors ))
127
103
for _ , validationError := range validationErrors {
128
- apiErrors = append (apiErrors , Error {
104
+ apiErrors = append (apiErrors , codersdk. ValidationError {
129
105
Field : validationError .Field (),
130
106
Detail : fmt .Sprintf ("Validation failed for tag %q with value: \" %v\" " , validationError .Tag (), validationError .Value ()),
131
107
})
132
108
}
133
- Write (rw , http .StatusBadRequest , Response {
109
+ Write (rw , http .StatusBadRequest , codersdk. Response {
134
110
Message : "Validation failed." ,
135
111
Validations : apiErrors ,
136
112
})
137
113
return false
138
114
}
139
115
if err != nil {
140
- Write (rw , http .StatusInternalServerError , Response {
116
+ Write (rw , http .StatusInternalServerError , codersdk. Response {
141
117
Message : "Internal error validating request body payload." ,
142
118
Detail : err .Error (),
143
119
})
0 commit comments