Skip to content

Commit

Permalink
better error messages for posting csr + test
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Apr 18, 2024
1 parent a1fc40e commit bbaa5c2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
7 changes: 5 additions & 2 deletions internal/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ func PostCertificateRequest(env *Environment) http.HandlerFunc {
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
logError("given csr already recorded", http.StatusBadRequest, w)
return
} else {
logError(err.Error(), http.StatusInternalServerError, w)
}
if strings.Contains(err.Error(), "csr validation failed") {
logError(err.Error(), http.StatusBadRequest, w)
return
}
logError(err.Error(), http.StatusInternalServerError, w)
return
}
w.WriteHeader(http.StatusCreated)
if _, err := w.Write([]byte(strconv.FormatInt(id, 10))); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ func TestGoCertRouter(t *testing.T) {
response: "null",
status: http.StatusOK,
},
{
desc: "post csr1 fail",
method: "POST",
path: "/api/v1/certificate_requests",
data: "this is very clearly not a csr",
response: "error: csr validation failed: PEM Certificate Request string not found or malformed",
status: http.StatusBadRequest,
},
{
desc: "post csr1 success",
method: "POST",
Expand Down
2 changes: 1 addition & 1 deletion internal/certdb/certdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (db *CertificateRequestsRepository) Retrieve(id string) (CertificateRequest
// The given CSR must be valid and unique
func (db *CertificateRequestsRepository) Create(csr string) (int64, error) {
if err := ValidateCertificateRequest(csr); err != nil {
return 0, err
return 0, errors.New("csr validation failed: " + err.Error())
}
result, err := db.conn.Exec(fmt.Sprintf(queryCreateCSR, db.table), csr)
if err != nil {
Expand Down

0 comments on commit bbaa5c2

Please sign in to comment.