feat: return structured errors
This commit is contained in:
parent
58f0a3ede6
commit
50727b2923
1 changed files with 23 additions and 14 deletions
37
main.go
37
main.go
|
|
@ -29,6 +29,11 @@ func convErrorHandler(str string, err error) string {
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RequestError struct {
|
||||||
|
Code string `json:"code"`
|
||||||
|
Message string `json:"messaage"`
|
||||||
|
}
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
// load environment file
|
// load environment file
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
|
|
@ -76,10 +81,12 @@ func (h *stopInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
conn, err := pool.Acquire(ctx)
|
conn, err := pool.Acquire(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
_, _ = w.Write([]byte(err.Error()))
|
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer conn.Release()
|
defer conn.Release()
|
||||||
|
|
@ -92,7 +99,7 @@ func (h *stopInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if searchQuery == "" {
|
if searchQuery == "" {
|
||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
_, _ = w.Write([]byte("'query' is required"))
|
_ = enc.Encode(RequestError{Code: "0000", Message: "'query' is required"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -105,17 +112,16 @@ func (h *stopInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
stations, err := queries.GetStationsByName(ctx, queryPgText)
|
stations, err := queries.GetStationsByName(ctx, queryPgText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
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)
|
w.WriteHeader(http.StatusOK)
|
||||||
err = enc.Encode(stations)
|
err = enc.Encode(stations)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
_, _ = w.Write([]byte(err.Error()))
|
_ = enc.Encode(RequestError{Code: "0002", Message: "Could not write response: " + err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -128,10 +134,12 @@ func (h *stopIdInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
conn, err := pool.Acquire(ctx)
|
conn, err := pool.Acquire(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
_, _ = w.Write([]byte(err.Error()))
|
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer conn.Release()
|
defer conn.Release()
|
||||||
|
|
@ -143,7 +151,7 @@ func (h *stopIdInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
var stopId = r.PathValue("id")
|
var stopId = r.PathValue("id")
|
||||||
if stopId == "" {
|
if stopId == "" {
|
||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
_, _ = w.Write([]byte("'id' is required"))
|
_ = enc.Encode(RequestError{Code: "0000", Message: "'id' is required"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -151,18 +159,17 @@ func (h *stopIdInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
stations, err := queries.GetStationById(ctx, stopId)
|
stations, err := queries.GetStationById(ctx, stopId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
_, _ = w.Write([]byte(err.Error()))
|
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return
|
// Return
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
err = enc.Encode(stations)
|
err = enc.Encode(stations)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
_, _ = w.Write([]byte(err.Error()))
|
_ = enc.Encode(RequestError{Code: "0002", Message: "Could not write response: " + err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -176,11 +183,13 @@ func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
// Acquire DB Connection
|
// Acquire DB Connection
|
||||||
conn, err := pool.Acquire(ctx)
|
conn, err := pool.Acquire(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
_, _ = w.Write([]byte(err.Error()))
|
_ = enc.Encode(RequestError{Code: "0000", Message: "'id' is required"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer conn.Release()
|
defer conn.Release()
|
||||||
|
|
@ -191,7 +200,7 @@ func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
|
||||||
var stopId = r.PathValue("id")
|
var stopId = r.PathValue("id")
|
||||||
if stopId == "" {
|
if stopId == "" {
|
||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
_, _ = w.Write([]byte("'id' is required"))
|
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
stopIdPg := pgtype.Text{String: stopId, Valid: true}
|
stopIdPg := pgtype.Text{String: stopId, Valid: true}
|
||||||
|
|
@ -242,7 +251,7 @@ func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
_, _ = w.Write([]byte(err.Error()))
|
_ = enc.Encode(RequestError{Code: "0001", Message: "DB Error: " + err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -253,7 +262,7 @@ func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
_, _ = w.Write([]byte(err.Error()))
|
_ = enc.Encode(RequestError{Code: "0002", Message: "Could not write response: " + err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue