Skip to content

Commit 7455acc

Browse files
committed
Proper 404's for objects
1 parent 83a9666 commit 7455acc

File tree

14 files changed

+149
-81
lines changed

14 files changed

+149
-81
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ dist
2323
backend/embed/acme.sh
2424
docker/dev/resolv.conf
2525
docker/dev/dnsrouter-config.json.tmp
26-
26+
thunder-tests

backend/internal/api/handler/certificate_authorities.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handler
22

33
import (
4+
"database/sql"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -43,11 +44,14 @@ func GetCertificateAuthority() func(http.ResponseWriter, *http.Request) {
4344
return
4445
}
4546

46-
cert, err := certificateauthority.GetByID(caID)
47-
if err != nil {
47+
item, err := certificateauthority.GetByID(caID)
48+
switch err {
49+
case sql.ErrNoRows:
50+
h.NotFound(w, r)
51+
case nil:
52+
h.ResultResponseJSON(w, r, http.StatusOK, item)
53+
default:
4854
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
49-
} else {
50-
h.ResultResponseJSON(w, r, http.StatusOK, cert)
5155
}
5256
}
5357
}
@@ -95,9 +99,10 @@ func UpdateCertificateAuthority() func(http.ResponseWriter, *http.Request) {
9599
}
96100

97101
ca, err := certificateauthority.GetByID(caID)
98-
if err != nil {
99-
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
100-
} else {
102+
switch err {
103+
case sql.ErrNoRows:
104+
h.NotFound(w, r)
105+
case nil:
101106
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
102107
err := json.Unmarshal(bodyBytes, &ca)
103108
if err != nil {
@@ -116,6 +121,8 @@ func UpdateCertificateAuthority() func(http.ResponseWriter, *http.Request) {
116121
}
117122

118123
h.ResultResponseJSON(w, r, http.StatusOK, ca)
124+
default:
125+
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
119126
}
120127
}
121128
}
@@ -131,11 +138,14 @@ func DeleteCertificateAuthority() func(http.ResponseWriter, *http.Request) {
131138
return
132139
}
133140

134-
cert, err := certificateauthority.GetByID(caID)
135-
if err != nil {
141+
item, err := certificateauthority.GetByID(caID)
142+
switch err {
143+
case sql.ErrNoRows:
144+
h.NotFound(w, r)
145+
case nil:
146+
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
147+
default:
136148
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
137-
} else {
138-
h.ResultResponseJSON(w, r, http.StatusOK, cert.Delete())
139149
}
140150
}
141151
}

backend/internal/api/handler/certificates.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func GetCertificate() func(http.ResponseWriter, *http.Request) {
4949
item, err := certificate.GetByID(certificateID)
5050
switch err {
5151
case sql.ErrNoRows:
52-
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
52+
h.NotFound(w, r)
5353
case nil:
5454
// nolint: errcheck,gosec
5555
item.Expand(getExpandFromContext(r))
@@ -100,10 +100,10 @@ func UpdateCertificate() func(http.ResponseWriter, *http.Request) {
100100
}
101101

102102
certificateObject, err := certificate.GetByID(certificateID)
103-
if err != nil {
104-
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
105-
} else {
106-
103+
switch err {
104+
case sql.ErrNoRows:
105+
h.NotFound(w, r)
106+
case nil:
107107
// This is a special endpoint, as it needs to verify the schema payload
108108
// based on the certificate type, without being given a type in the payload.
109109
// The middleware would normally handle this.
@@ -133,6 +133,8 @@ func UpdateCertificate() func(http.ResponseWriter, *http.Request) {
133133
configureCertificate(certificateObject)
134134

135135
h.ResultResponseJSON(w, r, http.StatusOK, certificateObject)
136+
default:
137+
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
136138
}
137139
}
138140
}
@@ -151,7 +153,7 @@ func DeleteCertificate() func(http.ResponseWriter, *http.Request) {
151153
item, err := certificate.GetByID(certificateID)
152154
switch err {
153155
case sql.ErrNoRows:
154-
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
156+
h.NotFound(w, r)
155157
case nil:
156158
// Ensure that this upstream isn't in use by a host
157159
cnt := host.GetCertificateUseCount(certificateID)

backend/internal/api/handler/dns_providers.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handler
22

33
import (
4+
"database/sql"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -10,6 +11,7 @@ import (
1011
"npm/internal/api/middleware"
1112
"npm/internal/dnsproviders"
1213
"npm/internal/entity/dnsprovider"
14+
"npm/internal/errors"
1315
)
1416

1517
// GetDNSProviders will return a list of DNS Providers
@@ -43,10 +45,13 @@ func GetDNSProvider() func(http.ResponseWriter, *http.Request) {
4345
}
4446

4547
item, err := dnsprovider.GetByID(providerID)
46-
if err != nil {
47-
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
48-
} else {
48+
switch err {
49+
case sql.ErrNoRows:
50+
h.NotFound(w, r)
51+
case nil:
4952
h.ResultResponseJSON(w, r, http.StatusOK, item)
53+
default:
54+
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
5055
}
5156
}
5257
}
@@ -89,9 +94,10 @@ func UpdateDNSProvider() func(http.ResponseWriter, *http.Request) {
8994
}
9095

9196
item, err := dnsprovider.GetByID(providerID)
92-
if err != nil {
93-
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
94-
} else {
97+
switch err {
98+
case sql.ErrNoRows:
99+
h.NotFound(w, r)
100+
case nil:
95101
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
96102
err := json.Unmarshal(bodyBytes, &item)
97103
if err != nil {
@@ -105,6 +111,8 @@ func UpdateDNSProvider() func(http.ResponseWriter, *http.Request) {
105111
}
106112

107113
h.ResultResponseJSON(w, r, http.StatusOK, item)
114+
default:
115+
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
108116
}
109117
}
110118
}
@@ -121,10 +129,13 @@ func DeleteDNSProvider() func(http.ResponseWriter, *http.Request) {
121129
}
122130

123131
item, err := dnsprovider.GetByID(providerID)
124-
if err != nil {
125-
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
126-
} else {
132+
switch err {
133+
case sql.ErrNoRows:
134+
h.NotFound(w, r)
135+
case nil:
127136
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
137+
default:
138+
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
128139
}
129140
}
130141
}
@@ -149,10 +160,13 @@ func GetAcmeshProvider() func(http.ResponseWriter, *http.Request) {
149160
}
150161

151162
item, getErr := dnsproviders.Get(acmeshID)
152-
if err != nil {
153-
h.ResultErrorJSON(w, r, http.StatusBadRequest, getErr.Error(), nil)
154-
} else {
163+
switch getErr {
164+
case errors.ErrProviderNotFound:
165+
h.NotFound(w, r)
166+
case nil:
155167
h.ResultResponseJSON(w, r, http.StatusOK, item)
168+
default:
169+
h.ResultErrorJSON(w, r, http.StatusBadRequest, getErr.Error(), nil)
156170
}
157171
}
158172
}

backend/internal/api/handler/hosts.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func GetHost() func(http.ResponseWriter, *http.Request) {
4949
item, err := host.GetByID(hostID)
5050
switch err {
5151
case sql.ErrNoRows:
52-
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
52+
h.NotFound(w, r)
5353
case nil:
5454
// nolint: errcheck,gosec
5555
item.Expand(getExpandFromContext(r))
@@ -110,9 +110,10 @@ func UpdateHost() func(http.ResponseWriter, *http.Request) {
110110
}
111111

112112
hostObject, err := host.GetByID(hostID)
113-
if err != nil {
114-
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
115-
} else {
113+
switch err {
114+
case sql.ErrNoRows:
115+
h.NotFound(w, r)
116+
case nil:
116117
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
117118
err := json.Unmarshal(bodyBytes, &hostObject)
118119
if err != nil {
@@ -136,6 +137,8 @@ func UpdateHost() func(http.ResponseWriter, *http.Request) {
136137
configureHost(hostObject)
137138

138139
h.ResultResponseJSON(w, r, http.StatusOK, hostObject)
140+
default:
141+
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
139142
}
140143
}
141144
}
@@ -154,7 +157,7 @@ func DeleteHost() func(http.ResponseWriter, *http.Request) {
154157
item, err := host.GetByID(hostID)
155158
switch err {
156159
case sql.ErrNoRows:
157-
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
160+
h.NotFound(w, r)
158161
case nil:
159162
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
160163
configureHost(item)
@@ -179,7 +182,7 @@ func GetHostNginxConfig(format string) func(http.ResponseWriter, *http.Request)
179182
item, err := host.GetByID(hostID)
180183
switch err {
181184
case sql.ErrNoRows:
182-
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
185+
h.NotFound(w, r)
183186
case nil:
184187
// Get the config from disk
185188
content, nErr := nginx.GetHostConfigContent(item)

backend/internal/api/handler/nginx_templates.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func GetNginxTemplate() func(http.ResponseWriter, *http.Request) {
4545
item, err := nginxtemplate.GetByID(templateID)
4646
switch err {
4747
case sql.ErrNoRows:
48-
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
48+
h.NotFound(w, r)
4949
case nil:
5050
h.ResultResponseJSON(w, r, http.StatusOK, item)
5151
default:
@@ -94,9 +94,10 @@ func UpdateNginxTemplate() func(http.ResponseWriter, *http.Request) {
9494
// reconfigure, _ := getQueryVarBool(r, "reconfigure", false, false)
9595

9696
nginxTemplate, err := nginxtemplate.GetByID(templateID)
97-
if err != nil {
98-
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
99-
} else {
97+
switch err {
98+
case sql.ErrNoRows:
99+
h.NotFound(w, r)
100+
case nil:
100101
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
101102
err := json.Unmarshal(bodyBytes, &nginxTemplate)
102103
if err != nil {
@@ -110,6 +111,8 @@ func UpdateNginxTemplate() func(http.ResponseWriter, *http.Request) {
110111
}
111112

112113
h.ResultResponseJSON(w, r, http.StatusOK, nginxTemplate)
114+
default:
115+
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
113116
}
114117
}
115118
}
@@ -128,7 +131,7 @@ func DeleteNginxTemplate() func(http.ResponseWriter, *http.Request) {
128131
item, err := nginxtemplate.GetByID(templateID)
129132
switch err {
130133
case sql.ErrNoRows:
131-
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
134+
h.NotFound(w, r)
132135
case nil:
133136
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
134137
default:

backend/internal/api/handler/not_found.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ func NotFound() func(http.ResponseWriter, *http.Request) {
3434
if err == errIsDir {
3535
err = tryRead(assetsSub, "index.html", w)
3636
if err != nil {
37-
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
37+
h.NotFound(w, r)
3838
}
3939
} else if err == nil {
4040
return
4141
}
4242

43-
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
43+
h.NotFound(w, r)
4444
}
4545
}
4646

backend/internal/api/handler/settings.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handler
22

33
import (
4+
"database/sql"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -38,11 +39,14 @@ func GetSetting() func(http.ResponseWriter, *http.Request) {
3839
return func(w http.ResponseWriter, r *http.Request) {
3940
name := chi.URLParam(r, "name")
4041

41-
sett, err := setting.GetByName(name)
42-
if err != nil {
42+
item, err := setting.GetByName(name)
43+
switch err {
44+
case sql.ErrNoRows:
45+
h.NotFound(w, r)
46+
case nil:
47+
h.ResultResponseJSON(w, r, http.StatusOK, item)
48+
default:
4349
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
44-
} else {
45-
h.ResultResponseJSON(w, r, http.StatusOK, sett)
4650
}
4751
}
4852
}
@@ -76,10 +80,10 @@ func UpdateSetting() func(http.ResponseWriter, *http.Request) {
7680
settingName := chi.URLParam(r, "name")
7781

7882
setting, err := setting.GetByName(settingName)
79-
if err != nil {
80-
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
81-
} else {
82-
83+
switch err {
84+
case sql.ErrNoRows:
85+
h.NotFound(w, r)
86+
case nil:
8387
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
8488
err := json.Unmarshal(bodyBytes, &setting)
8589
if err != nil {
@@ -93,6 +97,8 @@ func UpdateSetting() func(http.ResponseWriter, *http.Request) {
9397
}
9498

9599
h.ResultResponseJSON(w, r, http.StatusOK, setting)
100+
default:
101+
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
96102
}
97103
}
98104
}

0 commit comments

Comments
 (0)