Skip to content
This repository was archived by the owner on Mar 8, 2023. It is now read-only.

Commit 0368782

Browse files
committed
DRY error reponses
1 parent ae270ee commit 0368782

File tree

1 file changed

+24
-41
lines changed

1 file changed

+24
-41
lines changed

pkg/restproxy/rest_proxy.go

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,15 @@ func (p *restProxy) handleSecret(w http.ResponseWriter, r *http.Request) {
6666
path := r.URL.Path
6767
err := api.ValidateSecretPath(path)
6868
if err != nil {
69-
w.WriteHeader(http.StatusBadRequest)
70-
io.WriteString(w, err.Error())
69+
writeError(w, err, http.StatusBadRequest)
7170
return
7271
}
7372

7473
switch r.Method {
7574
case "GET":
7675
secret, err := p.client.Secrets().Versions().GetWithData(path)
7776
if err != nil {
78-
var errCode int
79-
80-
if err, ok := err.(errio.PublicStatusError); ok {
81-
errCode = err.StatusCode
82-
}
83-
84-
if errCode == 0 {
85-
errCode = http.StatusInternalServerError
86-
}
87-
88-
w.WriteHeader(errCode)
89-
io.WriteString(w, err.Error())
77+
writeError(w, err, 0)
9078
return
9179
}
9280

@@ -95,51 +83,29 @@ func (p *restProxy) handleSecret(w http.ResponseWriter, r *http.Request) {
9583
case "POST":
9684
secret, err := ioutil.ReadAll(r.Body)
9785
if err != nil {
98-
w.WriteHeader(http.StatusInternalServerError)
99-
io.WriteString(w, err.Error())
86+
writeError(w, err, http.StatusInternalServerError)
10087
return
10188
}
10289

10390
_, err = p.client.Secrets().Write(path, secret)
10491
if err != nil {
105-
var errCode int
106-
107-
if err, ok := err.(errio.PublicStatusError); ok {
108-
errCode = err.StatusCode
109-
}
110-
92+
statusCode := 0
11193
switch err {
11294
case secrethub.ErrCannotWriteToVersion,
11395
secrethub.ErrEmptySecret,
11496
secrethub.ErrSecretTooBig:
115-
errCode = http.StatusBadRequest
97+
statusCode = http.StatusBadRequest
11698
}
11799

118-
if errCode == 0 {
119-
errCode = http.StatusInternalServerError
120-
}
121-
122-
w.WriteHeader(errCode)
123-
io.WriteString(w, err.Error())
100+
writeError(w, err, statusCode)
124101
return
125102
}
126103

127104
w.WriteHeader(http.StatusCreated)
128105
case "DELETE":
129106
err := p.client.Secrets().Delete(path)
130107
if err != nil {
131-
var errCode int
132-
133-
if err, ok := err.(errio.PublicStatusError); ok {
134-
errCode = err.StatusCode
135-
}
136-
137-
if errCode == 0 {
138-
errCode = http.StatusInternalServerError
139-
}
140-
141-
w.WriteHeader(errCode)
142-
io.WriteString(w, err.Error())
108+
writeError(w, err, 0)
143109
return
144110
}
145111

@@ -149,3 +115,20 @@ func (p *restProxy) handleSecret(w http.ResponseWriter, r *http.Request) {
149115
w.WriteHeader(http.StatusMethodNotAllowed)
150116
}
151117
}
118+
119+
// writeError writes an error message and HTTP status code to the ResponseWriter.
120+
// The HTTP status code is derrived from the error, unless overriden by the statusCode argument.
121+
func writeError(w http.ResponseWriter, err error, statusCode int) {
122+
if statusCode == 0 {
123+
if err, ok := err.(errio.PublicStatusError); ok {
124+
statusCode = err.StatusCode
125+
}
126+
127+
if statusCode == 0 {
128+
statusCode = http.StatusInternalServerError
129+
}
130+
}
131+
132+
w.WriteHeader(statusCode)
133+
io.WriteString(w, err.Error())
134+
}

0 commit comments

Comments
 (0)