fix: resolved comments

This commit is contained in:
mayur.popli
2025-03-26 09:39:37 +05:30
parent 543f100dfe
commit f13157b6b9
6 changed files with 39 additions and 51 deletions

View File

@@ -12,11 +12,6 @@ import (
type ErrorType string
const (
SchemaValidationErrorType ErrorType = "SCHEMA_VALIDATION_ERROR"
InvalidRequestErrorType ErrorType = "INVALID_REQUEST"
)
type errorResponseWriter struct{}
func (e *errorResponseWriter) Header() http.Header {
@@ -53,7 +48,7 @@ func SendAck(w http.ResponseWriter) {
}
}
func nack(w http.ResponseWriter, err *model.Error, status int) {
func nack(w http.ResponseWriter, err *model.Error, status int, ctx context.Context) {
resp := &model.Response{
Message: model.Message{
Ack: model.Ack{
@@ -64,14 +59,17 @@ func nack(w http.ResponseWriter, err *model.Error, status int) {
}
data, jsonErr := json.Marshal(resp)
if jsonErr != nil {
http.Error(w, "failed to marshal response", http.StatusInternalServerError)
fmt.Printf("Error marshaling response: %v, MessageID: %s", jsonErr, ctx.Value(model.MsgIDKey))
http.Error(w, fmt.Sprintf("Internal server error, MessageID: %s", ctx.Value(model.MsgIDKey)), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, er := w.Write(data)
if er != nil {
http.Error(w, "failed to write response", http.StatusInternalServerError)
fmt.Printf("Error writing response: %v, MessageID: %s", er, ctx.Value(model.MsgIDKey))
http.Error(w, fmt.Sprintf("Internal server error, MessageID: %s", ctx.Value(model.MsgIDKey)), http.StatusInternalServerError)
return
}
}
@@ -91,19 +89,19 @@ func SendNack(ctx context.Context, w http.ResponseWriter, err error) {
switch {
case errors.As(err, &schemaErr):
nack(w, schemaErr.BecknError(), http.StatusBadRequest)
nack(w, schemaErr.BecknError(), http.StatusBadRequest, ctx)
return
case errors.As(err, &signErr):
nack(w, signErr.BecknError(), http.StatusUnauthorized)
nack(w, signErr.BecknError(), http.StatusUnauthorized, ctx)
return
case errors.As(err, &badReqErr):
nack(w, badReqErr.BecknError(), http.StatusBadRequest)
nack(w, badReqErr.BecknError(), http.StatusBadRequest, ctx)
return
case errors.As(err, &notFoundErr):
nack(w, notFoundErr.BecknError(), http.StatusNotFound)
nack(w, notFoundErr.BecknError(), http.StatusNotFound, ctx)
return
default:
nack(w, internalServerError(ctx), http.StatusInternalServerError)
nack(w, internalServerError(ctx), http.StatusInternalServerError, ctx)
return
}
}

View File

@@ -61,13 +61,14 @@ func TestNack(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := http.NewRequest("GET", "/", nil)
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err) // For tests
}
rr := httptest.NewRecorder()
ctx := context.WithValue(req.Context(), model.MsgIDKey, "12345")
nack(rr, tt.err, tt.status)
nack(rr, tt.err, tt.status, ctx)
if rr.Code != tt.status {
t.Errorf("expected status code %d, got %d", tt.status, rr.Code)
@@ -77,6 +78,7 @@ func TestNack(t *testing.T) {
if body != tt.expected {
t.Errorf("expected body %s, got %s", tt.expected, body)
}
})
}
}
@@ -166,9 +168,7 @@ func compareJSON(expected, actual map[string]interface{}) bool {
return bytes.Equal(expectedBytes, actualBytes)
}
func TestSendAck_WriteError(t *testing.T) {
w := &errorResponseWriter{}
SendAck(w)
// No need to assert, just ensure it doesn't panic
}