Skip to content

Commit 0a8bcd7

Browse files
api: output a more helpful error message when root is not found
1 parent 9bf6a83 commit 0a8bcd7

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ func Root(w http.ResponseWriter, r *http.Request) {
372372
// Load root certificate with the
373373
cert, err := mustAuthority(r.Context()).Root(sum)
374374
if err != nil {
375-
render.Error(w, r, errs.Wrapf(http.StatusNotFound, err, "%s was not found", r.RequestURI))
375+
render.Error(w, r, errs.NotFoundErr(err, errs.WithMessage("root with fingerprint %s was not found", sum)))
376376
return
377377
}
378378

api/api_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -835,20 +835,22 @@ func Test_Health(t *testing.T) {
835835
}
836836

837837
func Test_Root(t *testing.T) {
838+
const sha = "efc7d6b475a56fe587650bcdb999a4a308f815ba44db4bf0371ea68a786ccd36"
838839
tests := []struct {
839-
name string
840-
root *x509.Certificate
841-
err error
842-
statusCode int
840+
name string
841+
root *x509.Certificate
842+
err error
843+
expectedMsg string
844+
statusCode int
843845
}{
844-
{"ok", parseCertificate(rootPEM), nil, 200},
845-
{"fail", nil, fmt.Errorf("not found"), 404},
846+
{"ok", parseCertificate(rootPEM), nil, "", 200},
847+
{"fail", nil, fmt.Errorf("not found"), fmt.Sprintf("root with fingerprint %s was not found", sha), 404},
846848
}
847849

848850
// Request with chi context
849851
chiCtx := chi.NewRouteContext()
850-
chiCtx.URLParams.Add("sha", "efc7d6b475a56fe587650bcdb999a4a308f815ba44db4bf0371ea68a786ccd36")
851-
req := httptest.NewRequest("GET", "http://example.com/root/efc7d6b475a56fe587650bcdb999a4a308f815ba44db4bf0371ea68a786ccd36", http.NoBody)
852+
chiCtx.URLParams.Add("sha", sha)
853+
req := httptest.NewRequest("GET", "http://example.com/root/"+sha, http.NoBody)
852854
req = req.WithContext(context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx))
853855

854856
expected := []byte(`{"ca":"` + strings.ReplaceAll(rootPEM, "\n", `\n`) + `\n"}`)
@@ -866,13 +868,16 @@ func Test_Root(t *testing.T) {
866868

867869
body, err := io.ReadAll(res.Body)
868870
res.Body.Close()
871+
fmt.Println("body:", string(body))
869872
if err != nil {
870873
t.Errorf("caHandler.Root unexpected error = %v", err)
871874
}
872875
if tt.statusCode == 200 {
873876
if !bytes.Equal(bytes.TrimSpace(body), expected) {
874877
t.Errorf("caHandler.Root Body = %s, wants %s", body, expected)
875878
}
879+
} else {
880+
require.Contains(t, string(body), tt.expectedMsg)
876881
}
877882
})
878883
}

0 commit comments

Comments
 (0)