content type and status code issues
This commit is contained in:
parent
8bb8d90a88
commit
3792404e16
18
server.go
18
server.go
|
@ -179,7 +179,7 @@ func (s *Server) getDriverJourneys(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
jsonResponse(w, err, http.StatusInternalServerError)
|
jsonError(w, err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,7 +219,7 @@ func (s *Server) getPassengerJourneys(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
jsonResponse(w, err, http.StatusInternalServerError)
|
jsonError(w, err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -273,7 +273,7 @@ func (s *Server) getDriverRegularTrips(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
jsonResponse(w, err, http.StatusInternalServerError)
|
jsonError(w, err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -327,7 +327,7 @@ func (s *Server) getPassengerRegularTrips(w http.ResponseWriter, r *http.Request
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
jsonResponse(w, err, http.StatusInternalServerError)
|
jsonError(w, err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -335,12 +335,16 @@ func (s *Server) getPassengerRegularTrips(w http.ResponseWriter, r *http.Request
|
||||||
}
|
}
|
||||||
|
|
||||||
func jsonResponse(w http.ResponseWriter, response any, statuscode int) {
|
func jsonResponse(w http.ResponseWriter, response any, statuscode int) {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(statuscode)
|
||||||
err := json.NewEncoder(w).Encode(response)
|
err := json.NewEncoder(w).Encode(response)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func badRequest(w http.ResponseWriter, err error) {
|
func jsonError(w http.ResponseWriter, err error, statuscode int) {
|
||||||
jsonResponse(w, map[string]any{"error": err.Error()}, http.StatusBadRequest)
|
jsonResponse(w, map[string]any{"error": err.Error()}, statuscode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func badRequest(w http.ResponseWriter, err error) {
|
||||||
|
jsonError(w, err, http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue