Skip to content

Commit b98f86a

Browse files
authored
scep: minor cleanup (smallstep#867)
* api, scep: removed scep.Error * scep/api: replaced nextHTTP with http.HandlerFunc * scep/api: renamed writeSCEPResponse to writeResponse * scep/api: renamed decodeSCEPRequest to decodeRequest * scep/api: renamed writeError to fail * scep/api: replaced pkg/errors with errors * scep/api: formatted imports * scep/api: do not export SCEPRequest & SCEPResponse * scep/api: do not export Handler * api: flush errors better
1 parent 0827344 commit b98f86a

File tree

3 files changed

+86
-108
lines changed

3 files changed

+86
-108
lines changed

Diff for: api/errors.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/smallstep/certificates/authority/admin"
1414
"github.com/smallstep/certificates/errs"
1515
"github.com/smallstep/certificates/logging"
16-
"github.com/smallstep/certificates/scep"
1716
)
1817

1918
// WriteError writes to w a JSON representation of the given error.
@@ -25,22 +24,9 @@ func WriteError(w http.ResponseWriter, err error) {
2524
case *admin.Error:
2625
admin.WriteError(w, k)
2726
return
28-
case *scep.Error:
29-
w.Header().Set("Content-Type", "text/plain")
30-
default:
31-
w.Header().Set("Content-Type", "application/json")
3227
}
3328

3429
cause := errors.Cause(err)
35-
if sc, ok := err.(errs.StatusCoder); ok {
36-
w.WriteHeader(sc.StatusCode())
37-
} else {
38-
if sc, ok := cause.(errs.StatusCoder); ok {
39-
w.WriteHeader(sc.StatusCode())
40-
} else {
41-
w.WriteHeader(http.StatusInternalServerError)
42-
}
43-
}
4430

4531
// Write errors in the response writer
4632
if rl, ok := w.(logging.ResponseLogger); ok {
@@ -60,6 +46,16 @@ func WriteError(w http.ResponseWriter, err error) {
6046
}
6147
}
6248

49+
code := http.StatusInternalServerError
50+
if sc, ok := err.(errs.StatusCoder); ok {
51+
code = sc.StatusCode()
52+
} else if sc, ok := cause.(errs.StatusCoder); ok {
53+
code = sc.StatusCode()
54+
}
55+
56+
w.Header().Set("Content-Type", "application/json")
57+
w.WriteHeader(code)
58+
6359
if err := json.NewEncoder(w).Encode(err); err != nil {
6460
log.Error(w, err)
6561
}

0 commit comments

Comments
 (0)