feat: return structured errors

This commit is contained in:
etwas 2025-04-15 21:43:41 +02:00
parent 58f0a3ede6
commit 50727b2923
Signed by: etwas
SSH key fingerprint: SHA256:bHhIeAdn/2k9jmOs6+u6ox98VYmoHUN3HfnpV2w8Ws0

37
main.go
View file

@ -29,6 +29,11 @@ func convErrorHandler(str string, err error) string {
return str
}
type RequestError struct {
Code string `json:"code"`
Message string `json:"messaage"`
}
func run() error {
// load environment file
err := godotenv.Load()
@ -76,10 +81,12 @@ func (h *stopInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
ctx := context.Background()
w.Header().Set("Content-Type", "application/json")
conn, err := pool.Acquire(ctx)
if err != nil {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
return
}
defer conn.Release()
@ -92,7 +99,7 @@ func (h *stopInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if searchQuery == "" {
w.WriteHeader(400)
_, _ = w.Write([]byte("'query' is required"))
_ = enc.Encode(RequestError{Code: "0000", Message: "'query' is required"})
return
}
@ -105,17 +112,16 @@ func (h *stopInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
stations, err := queries.GetStationsByName(ctx, queryPgText)
if err != nil {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = enc.Encode(stations)
if err != nil {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
_ = enc.Encode(RequestError{Code: "0002", Message: "Could not write response: " + err.Error()})
return
}
@ -128,10 +134,12 @@ func (h *stopIdInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
ctx := context.Background()
w.Header().Set("Content-Type", "application/json")
conn, err := pool.Acquire(ctx)
if err != nil {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
return
}
defer conn.Release()
@ -143,7 +151,7 @@ func (h *stopIdInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var stopId = r.PathValue("id")
if stopId == "" {
w.WriteHeader(400)
_, _ = w.Write([]byte("'id' is required"))
_ = enc.Encode(RequestError{Code: "0000", Message: "'id' is required"})
return
}
@ -151,18 +159,17 @@ func (h *stopIdInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
stations, err := queries.GetStationById(ctx, stopId)
if err != nil {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
return
}
// Return
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = enc.Encode(stations)
if err != nil {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
_ = enc.Encode(RequestError{Code: "0002", Message: "Could not write response: " + err.Error()})
return
}
@ -176,11 +183,13 @@ func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
enc := json.NewEncoder(w)
ctx := context.Background()
w.Header().Set("Content-Type", "application/json")
// Acquire DB Connection
conn, err := pool.Acquire(ctx)
if err != nil {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
_ = enc.Encode(RequestError{Code: "0000", Message: "'id' is required"})
return
}
defer conn.Release()
@ -191,7 +200,7 @@ func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
var stopId = r.PathValue("id")
if stopId == "" {
w.WriteHeader(400)
_, _ = w.Write([]byte("'id' is required"))
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
return
}
stopIdPg := pgtype.Text{String: stopId, Valid: true}
@ -242,7 +251,7 @@ func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
if err != nil {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
return
}
@ -253,7 +262,7 @@ func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
if err != nil {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
_ = enc.Encode(RequestError{Code: "0002", Message: "Could not write response: " + err.Error()})
return
}