Skip to content

Commit c7e1bd3

Browse files
committed
godev/internal: add a content writer for http status
Added a function as a convenience for writing an http status code response without additional content. Change-Id: I1b9ee9d39d7ab1bb43248d0b800e5dacf2743606 Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/503760 Run-TryBot: Jamal Carvalho <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent 5206d28 commit c7e1bd3

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

godev/cmd/telemetrygodev/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func handleUpload(store storage.Store) content.HandlerFunc {
6868
}
6969
return content.Text(w, "ok", http.StatusOK)
7070
}
71-
return content.ErrorStatus(http.StatusMethodNotAllowed)
71+
return content.Status(w, http.StatusMethodNotAllowed)
7272
}
7373
}
7474

godev/internal/content/content.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,16 @@ func Text(w http.ResponseWriter, data any, code int) error {
213213
return nil
214214
}
215215

216-
// Error annotates an error with http status information.
217-
func Error(err error, code int) error {
218-
return &contentError{err, code}
216+
// Text renders an http status code as a text response.
217+
func Status(w http.ResponseWriter, code int) error {
218+
if code < http.StatusBadRequest {
219+
return Text(w, http.StatusText(code), code)
220+
}
221+
return Error(errors.New(http.StatusText(code)), code)
219222
}
220223

221-
// Error status returns a content error from a status code.
222-
func ErrorStatus(code int) error {
223-
err := errors.New(http.StatusText(code))
224+
// Error annotates an error with http status information.
225+
func Error(err error, code int) error {
224226
return &contentError{err, code}
225227
}
226228

godev/internal/content/content_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestServer_ServeHTTP(t *testing.T) {
2424
Handler("/data", handleTemplate(fsys)),
2525
Handler("/json", handleJSON()),
2626
Handler("/text", handleText()),
27+
Handler("/teapot", handleTeapot()),
2728
Handler("/error", handleError()),
2829
)
2930

@@ -57,6 +58,11 @@ func TestServer_ServeHTTP(t *testing.T) {
5758
"error.out",
5859
http.StatusBadRequest,
5960
},
61+
{
62+
"/teapot",
63+
"teapot.out",
64+
http.StatusTeapot,
65+
},
6066
{
6167
"/script.ts",
6268
"script.ts.out",
@@ -146,16 +152,25 @@ func handleTemplate(fsys fs.FS) HandlerFunc {
146152
return Template(w, fsys, "data.html", "Data from Handler", http.StatusOK)
147153
}
148154
}
155+
149156
func handleJSON() HandlerFunc {
150157
return func(w http.ResponseWriter, _ *http.Request) error {
151158
return JSON(w, struct{ Data string }{Data: "Data"}, http.StatusOK)
152159
}
153160
}
161+
154162
func handleText() HandlerFunc {
155163
return func(w http.ResponseWriter, _ *http.Request) error {
156164
return Text(w, "Hello, World!", http.StatusOK)
157165
}
158166
}
167+
168+
func handleTeapot() HandlerFunc {
169+
return func(w http.ResponseWriter, r *http.Request) error {
170+
return Status(w, http.StatusTeapot)
171+
}
172+
}
173+
159174
func handleError() HandlerFunc {
160175
return func(w http.ResponseWriter, r *http.Request) error {
161176
return Error(errors.New("Bad Request"), http.StatusBadRequest)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I'm a teapot

0 commit comments

Comments
 (0)