Skip to content

Commit 8ac62f7

Browse files
committed
feat(golang): return JSON response for Internal Server Errors
Part of #48
1 parent 3e3c037 commit 8ac62f7

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

examples/go/chi/mysql/routes.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import "database/sql"
44
import "encoding/json"
55
import "fmt"
6+
import "io"
67
import "net/http"
78
import "os"
89
import "github.com/go-chi/chi"
@@ -46,15 +47,15 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
4647
json.NewEncoder(w).Encode(&result)
4748
default:
4849
fmt.Fprintf(os.Stderr, "Get failed: %v\n", err)
49-
w.WriteHeader(http.StatusInternalServerError)
50+
internalServerError(w)
5051
}
5152
})
5253

5354
r.Get("/v1/collections/{collectionId}/categories/count", func(w http.ResponseWriter, r *http.Request) {
5455
stmt, err := db.PrepareNamed("SELECT COUNT(DISTINCT s.category_id) AS counter FROM collections_series cs JOIN series s ON s.id = cs.series_id WHERE cs.collection_id = :collectionId")
5556
if err != nil {
5657
fmt.Fprintf(os.Stderr, "PrepareNamed failed: %v\n", err)
57-
w.WriteHeader(http.StatusInternalServerError)
58+
internalServerError(w)
5859
return
5960
}
6061

@@ -71,15 +72,15 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
7172
json.NewEncoder(w).Encode(&result)
7273
default:
7374
fmt.Fprintf(os.Stderr, "Get failed: %v\n", err)
74-
w.WriteHeader(http.StatusInternalServerError)
75+
internalServerError(w)
7576
}
7677
})
7778

7879
r.Get("/v1/categories", func(w http.ResponseWriter, r *http.Request) {
7980
stmt, err := db.PrepareNamed("SELECT id , name , name_ru , slug FROM categories LIMIT :limit")
8081
if err != nil {
8182
fmt.Fprintf(os.Stderr, "PrepareNamed failed: %v\n", err)
82-
w.WriteHeader(http.StatusInternalServerError)
83+
internalServerError(w)
8384
return
8485
}
8586

@@ -96,7 +97,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
9697
json.NewEncoder(w).Encode(&result)
9798
default:
9899
fmt.Fprintf(os.Stderr, "Select failed: %v\n", err)
99-
w.WriteHeader(http.StatusInternalServerError)
100+
internalServerError(w)
100101
}
101102
})
102103

@@ -116,7 +117,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
116117
)
117118
if err != nil {
118119
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
119-
w.WriteHeader(http.StatusInternalServerError)
120+
internalServerError(w)
120121
return
121122
}
122123

@@ -127,7 +128,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
127128
stmt, err := db.PrepareNamed("SELECT id , name , name_ru , slug FROM categories WHERE id = :categoryId")
128129
if err != nil {
129130
fmt.Fprintf(os.Stderr, "PrepareNamed failed: %v\n", err)
130-
w.WriteHeader(http.StatusInternalServerError)
131+
internalServerError(w)
131132
return
132133
}
133134

@@ -144,7 +145,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
144145
json.NewEncoder(w).Encode(&result)
145146
default:
146147
fmt.Fprintf(os.Stderr, "Get failed: %v\n", err)
147-
w.WriteHeader(http.StatusInternalServerError)
148+
internalServerError(w)
148149
}
149150
})
150151

@@ -165,7 +166,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
165166
)
166167
if err != nil {
167168
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
168-
w.WriteHeader(http.StatusInternalServerError)
169+
internalServerError(w)
169170
return
170171
}
171172

@@ -182,11 +183,17 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
182183
)
183184
if err != nil {
184185
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
185-
w.WriteHeader(http.StatusInternalServerError)
186+
internalServerError(w)
186187
return
187188
}
188189

189190
w.WriteHeader(http.StatusNoContent)
190191
})
191192

192193
}
194+
195+
func internalServerError(w http.ResponseWriter) {
196+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
197+
w.WriteHeader(http.StatusInternalServerError)
198+
io.WriteString(w, `{"error":"Internal Server Error"}`)
199+
}

src/templates/routes.go.ejs

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import "database/sql"
44
import "encoding/json"
55
import "fmt"
6+
import "io"
67
import "net/http"
78
import "os"
89
import "github.com/go-chi/chi"
@@ -187,7 +188,7 @@ endpoints.forEach(function(endpoint) {
187188
stmt, err := db.PrepareNamed("<%- formatQuery(method.query) %>")
188189
if err != nil {
189190
fmt.Fprintf(os.Stderr, "PrepareNamed failed: %v\n", err)
190-
w.WriteHeader(http.StatusInternalServerError)
191+
internalServerError(w)
191192
return
192193
}
193194
@@ -208,7 +209,7 @@ endpoints.forEach(function(endpoint) {
208209
json.NewEncoder(w).Encode(&result)
209210
default:
210211
fmt.Fprintf(os.Stderr, "<%- queryFunction %> failed: %v\n", err)
211-
w.WriteHeader(http.StatusInternalServerError)
212+
internalServerError(w)
212213
}
213214
})
214215
<%
@@ -232,7 +233,7 @@ endpoints.forEach(function(endpoint) {
232233
)
233234
if err != nil {
234235
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
235-
w.WriteHeader(http.StatusInternalServerError)
236+
internalServerError(w)
236237
return
237238
}
238239
@@ -259,7 +260,7 @@ endpoints.forEach(function(endpoint) {
259260
)
260261
if err != nil {
261262
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
262-
w.WriteHeader(http.StatusInternalServerError)
263+
internalServerError(w)
263264
return
264265
}
265266
@@ -279,7 +280,7 @@ endpoints.forEach(function(endpoint) {
279280
)
280281
if err != nil {
281282
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
282-
w.WriteHeader(http.StatusInternalServerError)
283+
internalServerError(w)
283284
return
284285
}
285286
@@ -291,3 +292,11 @@ endpoints.forEach(function(endpoint) {
291292
})
292293
%>
293294
}
295+
296+
<%# IMPORTANT: WriteHeader() must be called after w.Header() -%>
297+
<%# w.Write() vs io.WriteString(): https://stackoverflow.com/questions/37863374/whats-the-difference-between-responsewriter-write-and-io-writestring -%>
298+
func internalServerError(w http.ResponseWriter) {
299+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
300+
w.WriteHeader(http.StatusInternalServerError)
301+
io.WriteString(w, `{"error":"Internal Server Error"}`)
302+
}

0 commit comments

Comments
 (0)