From 3792404e16e102611b683a5cb73974c225646fb4 Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Tue, 13 Dec 2022 10:21:13 +0100 Subject: [PATCH] content type and status code issues --- server.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/server.go b/server.go index 251d310..7a071c5 100644 --- a/server.go +++ b/server.go @@ -179,7 +179,7 @@ func (s *Server) getDriverJourneys(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Println(err) - jsonResponse(w, err, http.StatusInternalServerError) + jsonError(w, err, http.StatusInternalServerError) return } @@ -219,7 +219,7 @@ func (s *Server) getPassengerJourneys(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Println(err) - jsonResponse(w, err, http.StatusInternalServerError) + jsonError(w, err, http.StatusInternalServerError) return } @@ -273,7 +273,7 @@ func (s *Server) getDriverRegularTrips(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Println(err) - jsonResponse(w, err, http.StatusInternalServerError) + jsonError(w, err, http.StatusInternalServerError) return } @@ -327,7 +327,7 @@ func (s *Server) getPassengerRegularTrips(w http.ResponseWriter, r *http.Request if err != nil { fmt.Println(err) - jsonResponse(w, err, http.StatusInternalServerError) + jsonError(w, err, http.StatusInternalServerError) return } @@ -335,12 +335,16 @@ func (s *Server) getPassengerRegularTrips(w http.ResponseWriter, r *http.Request } func jsonResponse(w http.ResponseWriter, response any, statuscode int) { - w.WriteHeader(http.StatusBadRequest) w.Header().Set("Content-Type", "application/json") + w.WriteHeader(statuscode) err := json.NewEncoder(w).Encode(response) fmt.Println(err) } -func badRequest(w http.ResponseWriter, err error) { - jsonResponse(w, map[string]any{"error": err.Error()}, http.StatusBadRequest) +func jsonError(w http.ResponseWriter, err error, statuscode int) { + jsonResponse(w, map[string]any{"error": err.Error()}, statuscode) +} + +func badRequest(w http.ResponseWriter, err error) { + jsonError(w, err, http.StatusBadRequest) }