Skip to content

Commit

Permalink
standardize not found error
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Apr 18, 2024
1 parent ffbee9a commit d211910
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
10 changes: 9 additions & 1 deletion internal/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func GetCertificateRequest(env *Environment) http.HandlerFunc {
id := r.PathValue("id")
cert, err := env.DB.Retrieve(id)
if err != nil {
if err.Error() == "sql: no rows in result set" {
if err.Error() == "csr id not found" {
logError(err.Error(), http.StatusBadRequest, w)
return
}
Expand Down Expand Up @@ -127,6 +127,14 @@ func PostCertificate(env *Environment) http.HandlerFunc {
id := r.PathValue("id")
insertId, err := env.DB.Update(id, string(cert))
if err != nil {
if err.Error() == "csr id not found" {
logError(err.Error(), http.StatusBadRequest, w)
return
}
if err.Error() == "certificate does not match CSR" {
logError(err.Error(), http.StatusBadRequest, w)
return
}
logError(err.Error(), http.StatusInternalServerError, w)
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func TestGoCertRouter(t *testing.T) {
method: "GET",
path: "/api/v1/certificate_requests/1",
data: "",
response: "error: sql: no rows in result set",
response: "error: csr id not found",
status: http.StatusBadRequest,
},
{
Expand Down
8 changes: 4 additions & 4 deletions internal/certdb/certdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func (db *CertificateRequestsRepository) Retrieve(id string) (CertificateRequest
var newCSR CertificateRequest
row := db.conn.QueryRow(fmt.Sprintf(queryGetCSR, db.table), id)
if err := row.Scan(&newCSR.ID, &newCSR.CSR, &newCSR.Certificate); err != nil {
if err.Error() == "sql: no rows in result set" {
return newCSR, errors.New("csr id not found")
}
return newCSR, err
}
return newCSR, nil
Expand Down Expand Up @@ -86,7 +89,7 @@ func (db *CertificateRequestsRepository) Update(id string, cert string) (int64,
}
csr, err := db.Retrieve(id)
if err != nil {
return 0, nil
return 0, err
}
if err := CertificateMatchesCSR(cert, csr.CSR); err != nil {
return 0, err
Expand All @@ -99,9 +102,6 @@ func (db *CertificateRequestsRepository) Update(id string, cert string) (int64,
if err != nil {
return 0, err
}
if insertId == 0 {
return 0, errors.New("column not found")
}
return insertId, nil
}

Expand Down

0 comments on commit d211910

Please sign in to comment.