Skip to content

Commit

Permalink
feat(golang): return JSON response for Internal Server Errors
Browse files Browse the repository at this point in the history
Part of #48
  • Loading branch information
php-coder committed Mar 27, 2024
1 parent 3e3c037 commit 8ac62f7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
27 changes: 17 additions & 10 deletions examples/go/chi/mysql/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import "database/sql"
import "encoding/json"
import "fmt"
import "io"
import "net/http"
import "os"
import "github.com/go-chi/chi"
Expand Down Expand Up @@ -46,15 +47,15 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
json.NewEncoder(w).Encode(&result)
default:
fmt.Fprintf(os.Stderr, "Get failed: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
internalServerError(w)
}
})

r.Get("/v1/collections/{collectionId}/categories/count", func(w http.ResponseWriter, r *http.Request) {
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")
if err != nil {
fmt.Fprintf(os.Stderr, "PrepareNamed failed: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
internalServerError(w)
return
}

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

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

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

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

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

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

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

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

w.WriteHeader(http.StatusNoContent)
})

}

func internalServerError(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error":"Internal Server Error"}`)
}
19 changes: 14 additions & 5 deletions src/templates/routes.go.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import "database/sql"
import "encoding/json"
import "fmt"
import "io"
import "net/http"
import "os"
import "github.com/go-chi/chi"
Expand Down Expand Up @@ -187,7 +188,7 @@ endpoints.forEach(function(endpoint) {
stmt, err := db.PrepareNamed("<%- formatQuery(method.query) %>")
if err != nil {
fmt.Fprintf(os.Stderr, "PrepareNamed failed: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
internalServerError(w)
return
}
Expand All @@ -208,7 +209,7 @@ endpoints.forEach(function(endpoint) {
json.NewEncoder(w).Encode(&result)
default:
fmt.Fprintf(os.Stderr, "<%- queryFunction %> failed: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
internalServerError(w)
}
})
<%
Expand All @@ -232,7 +233,7 @@ endpoints.forEach(function(endpoint) {
)
if err != nil {
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
internalServerError(w)
return
}
Expand All @@ -259,7 +260,7 @@ endpoints.forEach(function(endpoint) {
)
if err != nil {
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
internalServerError(w)
return
}
Expand All @@ -279,7 +280,7 @@ endpoints.forEach(function(endpoint) {
)
if err != nil {
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
internalServerError(w)
return
}
Expand All @@ -291,3 +292,11 @@ endpoints.forEach(function(endpoint) {
})
%>
}

<%# IMPORTANT: WriteHeader() must be called after w.Header() -%>
<%# w.Write() vs io.WriteString(): https://stackoverflow.com/questions/37863374/whats-the-difference-between-responsewriter-write-and-io-writestring -%>
func internalServerError(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error":"Internal Server Error"}`)
}

0 comments on commit 8ac62f7

Please sign in to comment.