Skip to content

Commit 30fe4d1

Browse files
committed
Add standardization to HTTP response
1 parent 7894fd2 commit 30fe4d1

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

internal/server/httpServer.go

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package server
22

33
import (
44
"context"
5-
"encoding/json"
65
"errors"
7-
"fmt"
86
"log"
97
"net/http"
108
"server/internal/middleware"
@@ -28,17 +26,6 @@ type HandlerMux struct {
2826
rateLimiter func(http.ResponseWriter, *http.Request, http.Handler)
2927
}
3028

31-
type HTTPResponse struct {
32-
Data interface{} `json:"data"`
33-
}
34-
35-
type HTTPErrorResponse struct {
36-
Error interface{} `json:"error"`
37-
}
38-
39-
func errorResponse(response string) string {
40-
return fmt.Sprintf("{\"error\": %q}", response)
41-
}
4229

4330
func (cim *HandlerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4431
// Convert the path to lowercase before passing to the underlying mux.
@@ -91,43 +78,25 @@ func (s *HTTPServer) Shutdown() error {
9178
}
9279

9380
func (s *HTTPServer) HealthCheck(w http.ResponseWriter, request *http.Request) {
94-
util.JSONResponse(w, http.StatusOK, map[string]string{"message": "Server is running"})
81+
util.HttpResponseJSON(w, http.StatusOK, map[string]string{"message": "Server is running"})
9582
}
9683

9784
func (s *HTTPServer) CliHandler(w http.ResponseWriter, r *http.Request) {
9885
diceCmd, err := util.ParseHTTPRequest(r)
9986
if err != nil {
100-
http.Error(w, "Error parsing HTTP request", http.StatusBadRequest)
87+
util.HttpResponseException(w,http.StatusBadRequest,"Error parsing HTTP request");
10188
return
10289
}
10390

10491
resp, err := s.DiceClient.ExecuteCommand(diceCmd)
10592
if err != nil {
106-
http.Error(w, errorResponse(err.Error()), http.StatusBadRequest)
107-
return
108-
}
109-
110-
if _, ok := resp.(string); !ok {
111-
log.Println("Error marshaling response", "error", err)
112-
http.Error(w, errorResponse("Internal Server Error"), http.StatusInternalServerError)
93+
util.HttpResponseException(w,http.StatusBadRequest,err);
11394
return
11495
}
11596

116-
httpResponse := HTTPResponse{Data: resp.(string)}
117-
responseJSON, err := json.Marshal(httpResponse)
118-
if err != nil {
119-
log.Println("Error marshaling response", "error", err)
120-
http.Error(w, errorResponse("Internal Server Error"), http.StatusInternalServerError)
121-
return
122-
}
123-
124-
_, err = w.Write(responseJSON)
125-
if err != nil {
126-
http.Error(w, errorResponse("Internal Server Error"), http.StatusInternalServerError)
127-
return
128-
}
97+
util.HttpResponseJSON(w, http.StatusOK, resp)
12998
}
13099

131100
func (s *HTTPServer) SearchHandler(w http.ResponseWriter, request *http.Request) {
132-
util.JSONResponse(w, http.StatusOK, map[string]string{"message": "Search results"})
101+
util.HttpResponseJSON(w,http.StatusOK,map[string]string{"message": "Search results"});
133102
}

pkg/util/helpers.go

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"os"
10+
"runtime/debug"
911
"server/internal/cmds"
1012
"strings"
1113
)
@@ -31,6 +33,19 @@ const (
3133
JSONIngest string = "JSON.INGEST"
3234
)
3335

36+
type HttpResponse struct {
37+
Data interface{} `json:"data"`
38+
Error *ErrorDetails `json:"error"`
39+
HasError bool `json:"hasError"`
40+
HasData bool `json:"hasData"`
41+
StackTrace *string `json:"stackTrace,omitempty"`
42+
}
43+
44+
type ErrorDetails struct {
45+
Message *string `json:"message"`
46+
StackTrace *string `json:"stackTrace,omitempty"`
47+
}
48+
3449
func ParseHTTPRequest(r *http.Request) (*cmds.CommandRequest, error) {
3550
command := strings.TrimPrefix(r.URL.Path, "/cli/")
3651
if command == "" {
@@ -154,10 +169,45 @@ func ParseHTTPRequest(r *http.Request) (*cmds.CommandRequest, error) {
154169
}, nil
155170
}
156171

157-
func JSONResponse(w http.ResponseWriter, status int, data interface{}) {
158-
w.Header().Set("Content-Type", "application/json")
159-
w.WriteHeader(status)
160-
if err := json.NewEncoder(w).Encode(data); err != nil {
161-
http.Error(w, err.Error(), http.StatusInternalServerError)
172+
func generateHttpResponse(w http.ResponseWriter, statusCode int, data interface{}, err *string) {
173+
response := HttpResponse{
174+
HasData: data != nil,
175+
HasError: err != nil,
176+
Data: data,
177+
}
178+
179+
if err != nil {
180+
errorDetails := &ErrorDetails{
181+
Message: err,
182+
}
183+
if os.Getenv("ENV") == "development" {
184+
stackTrace := string(debug.Stack())
185+
errorDetails.StackTrace = &stackTrace
186+
}
187+
response.Error = errorDetails
162188
}
189+
190+
w.Header().Set("Content-Type", "application/json")
191+
w.WriteHeader(statusCode)
192+
193+
if encodeErr := json.NewEncoder(w).Encode(response); encodeErr != nil {
194+
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
195+
}
163196
}
197+
198+
func HttpResponseJSON(w http.ResponseWriter,statusCode int, data interface{}) {
199+
generateHttpResponse(w, http.StatusOK, data, nil)
200+
}
201+
202+
func HttpResponseException(w http.ResponseWriter, statusCode int, err interface{}) {
203+
var errorStr string
204+
switch e := err.(type) {
205+
case error:
206+
errorStr = e.Error()
207+
case string:
208+
errorStr = e
209+
default:
210+
errorStr = "Unknown error type"
211+
}
212+
generateHttpResponse(w, statusCode, nil, &errorStr)
213+
}

0 commit comments

Comments
 (0)