Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ExceptionHandlerMiddleware(next http.Handler) http.Handler {
body, err := io.ReadAll(r.Body)
if err != nil {
logrus.Error(err)
NotifyError("Exception Handler Middleware Error", "failed to read request body", err.Error(), "URI", r.RequestURI)
NotifyError("Exception Handler Middleware Error", "failed to read request body", err.Error())
w.WriteHeader(http.StatusTeapot)
return
}
Expand All @@ -57,7 +57,7 @@ func ExceptionHandlerMiddleware(next http.Handler) http.Handler {
// print stack trace as well
fmt.Println(stackTrace)
w.WriteHeader(http.StatusInternalServerError)
NotifyError("Exception Handler Middleware Recovery", "Check stack trace above", fmt.Sprintf("%v\n%s", err, stackTrace), "Request", string(body), "URI", r.RequestURI)
NotifyError("Exception Handler Middleware Recovery", "Check stack trace above", fmt.Sprintf("%v\n%s", err, stackTrace), "Request", string(body))
}
}()

Expand All @@ -66,29 +66,29 @@ func ExceptionHandlerMiddleware(next http.Handler) http.Handler {
cloneRequest.Body = io.NopCloser(bytes.NewReader(body))
next.ServeHTTP(rw, cloneRequest)

description := fmt.Sprintf("failed request with StatusCode: %d", rw.statusCode)
description := fmt.Sprintf("failed request with status code[%d] : url[%s] : origin[%s]", rw.statusCode, r.RequestURI, r.RemoteAddr)

body = func() []byte {
var request map[string]interface{}
if err := json.Unmarshal(body, &request); err != nil {
NotifyWarn("Exception Handler Middleware Recovery", "failed to un-marshal request body", fmt.Sprintf("%v : %v", request, err), "URI", r.RequestURI)
NotifyWarn("Exception Handler Middleware Recovery", "failed to un-marshal request body", fmt.Sprintf("%v : %v", request, err))
return body
}

request = deleteFields(request)
modifiedBody, err := json.Marshal(request)
if err != nil {
NotifyWarn("Exception Handler Middleware Recovery", "failed to marshal request body", fmt.Sprintf("%v : %v", request, err), "URI", r.RequestURI)
NotifyWarn("Exception Handler Middleware Recovery", "failed to marshal request body", fmt.Sprintf("%v : %v", request, err))
return body
}

return modifiedBody
}()

if rw.statusCode >= 500 {
NotifyError("Exception Handler Middleware Error", description, "", "url", r.RequestURI, "Response", string(rw.response), "Request", string(body), "URI", r.RequestURI)
NotifyError("Exception Handler Middleware Error", description, "", "Response", string(rw.response), "Request", string(body))
} else if rw.statusCode >= 400 {
NotifyWarn("Exception Handler Middleware Warn", description, fmt.Sprintf("Response: %s", string(rw.response)), "URI", r.RequestURI)
NotifyWarn("Exception Handler Middleware Error", description, "", "Response", string(rw.response), "Request", string(body))
}

logrus.Debug("Exception Handler Middleware Passed!!!")
Expand Down