fix: resolved comments
This commit is contained in:
@@ -1,27 +0,0 @@
|
|||||||
mode: set
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:15.32,17.2 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:23.46,25.31 2 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:25.31,27.3 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:28.2,28.42 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:31.51,32.24 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:32.24,37.3 1 0
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:38.2,40.31 3 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:40.31,41.22 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:41.22,43.4 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:44.3,44.43 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:47.2,51.3 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:58.72,60.2 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:62.55,64.2 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:66.49,71.2 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:77.41,79.2 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:81.56,83.2 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:85.41,90.2 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:96.45,98.2 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:100.60,102.2 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/error.go:104.43,109.2 1 1
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/model.go:63.71,65.45 2 0
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/model.go:65.45,67.3 1 0
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/model.go:69.2,70.23 2 0
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/model.go:70.23,72.3 1 0
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/model.go:73.2,74.12 2 0
|
|
||||||
github.com/beckn/beckn-onix/pkg/model/model.go:93.61,95.2 1 0
|
|
||||||
@@ -6,20 +6,24 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Error represents an error response.
|
||||||
type Error struct {
|
type Error struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Paths string `json:"paths,omitempty"`
|
Paths string `json:"paths,omitempty"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error implements the error interface for the Error struct.
|
||||||
func (e *Error) Error() string {
|
func (e *Error) Error() string {
|
||||||
return fmt.Sprintf("Error: Code=%s, Path=%s, Message=%s", e.Code, e.Paths, e.Message)
|
return fmt.Sprintf("Error: Code=%s, Path=%s, Message=%s", e.Code, e.Paths, e.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SchemaValidationErr represents a collection of schema validation failures.
|
||||||
type SchemaValidationErr struct {
|
type SchemaValidationErr struct {
|
||||||
Errors []Error
|
Errors []Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error implements the error interface for SchemaValidationErr.
|
||||||
func (e *SchemaValidationErr) Error() string {
|
func (e *SchemaValidationErr) Error() string {
|
||||||
var errorMessages []string
|
var errorMessages []string
|
||||||
for _, err := range e.Errors {
|
for _, err := range e.Errors {
|
||||||
@@ -35,6 +39,8 @@ func (e *SchemaValidationErr) BecknError() *Error {
|
|||||||
Message: "Schema validation error.",
|
Message: "Schema validation error.",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Collect all error paths and messages
|
||||||
var paths []string
|
var paths []string
|
||||||
var messages []string
|
var messages []string
|
||||||
for _, err := range e.Errors {
|
for _, err := range e.Errors {
|
||||||
@@ -51,6 +57,7 @@ func (e *SchemaValidationErr) BecknError() *Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SignalidationErr represents a collection of schema validation failures.
|
||||||
type SignValidationErr struct {
|
type SignValidationErr struct {
|
||||||
error
|
error
|
||||||
}
|
}
|
||||||
@@ -70,6 +77,7 @@ func (e *SignValidationErr) BecknError() *Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SignalidationErr represents a collection of schema validation failures.
|
||||||
type BadReqErr struct {
|
type BadReqErr struct {
|
||||||
error
|
error
|
||||||
}
|
}
|
||||||
@@ -89,6 +97,7 @@ func (e *BadReqErr) BecknError() *Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SignalidationErr represents a collection of schema validation failures.
|
||||||
type NotFoundErr struct {
|
type NotFoundErr struct {
|
||||||
error
|
error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,8 +19,10 @@ func TestError_Error(t *testing.T) {
|
|||||||
actual := err.Error()
|
actual := err.Error()
|
||||||
|
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Errorf("expected %s, got %s", expected, actual)
|
t.Errorf("err.Error() = %s, want %s",
|
||||||
|
actual, expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSchemaValidationErr_Error(t *testing.T) {
|
func TestSchemaValidationErr_Error(t *testing.T) {
|
||||||
@@ -35,7 +37,8 @@ func TestSchemaValidationErr_Error(t *testing.T) {
|
|||||||
actual := schemaErr.Error()
|
actual := schemaErr.Error()
|
||||||
|
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Errorf("expected %s, got %s", expected, actual)
|
t.Errorf("err.Error() = %s, want %s",
|
||||||
|
actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +52,8 @@ func TestSchemaValidationErr_BecknError(t *testing.T) {
|
|||||||
beErr := schemaErr.BecknError()
|
beErr := schemaErr.BecknError()
|
||||||
expected := "Bad Request"
|
expected := "Bad Request"
|
||||||
if beErr.Code != expected {
|
if beErr.Code != expected {
|
||||||
t.Errorf("expected %s, got %s", expected, beErr.Code)
|
t.Errorf("err.Error() = %s, want %s",
|
||||||
|
beErr.Code, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,15 +63,18 @@ func TestSignValidationErr_BecknError(t *testing.T) {
|
|||||||
|
|
||||||
expectedMsg := "Signature Validation Error: signature failed"
|
expectedMsg := "Signature Validation Error: signature failed"
|
||||||
if beErr.Message != expectedMsg {
|
if beErr.Message != expectedMsg {
|
||||||
t.Errorf("expected %s, got %s", expectedMsg, beErr.Message)
|
t.Errorf("err.Error() = %s, want %s",
|
||||||
|
beErr.Message, expectedMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewSignValidationErrf(t *testing.T) {
|
func TestNewSignValidationErrf(t *testing.T) {
|
||||||
signErr := NewSignValidationErrf("error %s", "signature failed")
|
signErr := NewSignValidationErrf("error %s", "signature failed")
|
||||||
expected := "error signature failed"
|
expected := "error signature failed"
|
||||||
if signErr.Error() != expected {
|
if signErr.Error() != expected {
|
||||||
t.Errorf("expected %s, got %s", expected, signErr.Error())
|
t.Errorf("err.Error() = %s, want %s",
|
||||||
|
signErr.Error(), expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +83,8 @@ func TestNewSignValidationErr(t *testing.T) {
|
|||||||
signErr := NewSignValidationErr(err)
|
signErr := NewSignValidationErr(err)
|
||||||
|
|
||||||
if signErr.Error() != err.Error() {
|
if signErr.Error() != err.Error() {
|
||||||
t.Errorf("expected %s, got %s", err.Error(), signErr.Error())
|
t.Errorf("err.Error() = %s, want %s", err.Error(),
|
||||||
|
signErr.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ const (
|
|||||||
|
|
||||||
type contextKey string
|
type contextKey string
|
||||||
|
|
||||||
var MsgIDKey = contextKey("message_id")
|
const MsgIDKey = contextKey("message_id")
|
||||||
|
|
||||||
type Role string
|
type Role string
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,6 @@ import (
|
|||||||
|
|
||||||
type ErrorType string
|
type ErrorType string
|
||||||
|
|
||||||
const (
|
|
||||||
SchemaValidationErrorType ErrorType = "SCHEMA_VALIDATION_ERROR"
|
|
||||||
InvalidRequestErrorType ErrorType = "INVALID_REQUEST"
|
|
||||||
)
|
|
||||||
|
|
||||||
type errorResponseWriter struct{}
|
type errorResponseWriter struct{}
|
||||||
|
|
||||||
func (e *errorResponseWriter) Header() http.Header {
|
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{
|
resp := &model.Response{
|
||||||
Message: model.Message{
|
Message: model.Message{
|
||||||
Ack: model.Ack{
|
Ack: model.Ack{
|
||||||
@@ -64,14 +59,17 @@ func nack(w http.ResponseWriter, err *model.Error, status int) {
|
|||||||
}
|
}
|
||||||
data, jsonErr := json.Marshal(resp)
|
data, jsonErr := json.Marshal(resp)
|
||||||
if jsonErr != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
_, er := w.Write(data)
|
_, er := w.Write(data)
|
||||||
if er != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,19 +89,19 @@ func SendNack(ctx context.Context, w http.ResponseWriter, err error) {
|
|||||||
|
|
||||||
switch {
|
switch {
|
||||||
case errors.As(err, &schemaErr):
|
case errors.As(err, &schemaErr):
|
||||||
nack(w, schemaErr.BecknError(), http.StatusBadRequest)
|
nack(w, schemaErr.BecknError(), http.StatusBadRequest, ctx)
|
||||||
return
|
return
|
||||||
case errors.As(err, &signErr):
|
case errors.As(err, &signErr):
|
||||||
nack(w, signErr.BecknError(), http.StatusUnauthorized)
|
nack(w, signErr.BecknError(), http.StatusUnauthorized, ctx)
|
||||||
return
|
return
|
||||||
case errors.As(err, &badReqErr):
|
case errors.As(err, &badReqErr):
|
||||||
nack(w, badReqErr.BecknError(), http.StatusBadRequest)
|
nack(w, badReqErr.BecknError(), http.StatusBadRequest, ctx)
|
||||||
return
|
return
|
||||||
case errors.As(err, ¬FoundErr):
|
case errors.As(err, ¬FoundErr):
|
||||||
nack(w, notFoundErr.BecknError(), http.StatusNotFound)
|
nack(w, notFoundErr.BecknError(), http.StatusNotFound, ctx)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
nack(w, internalServerError(ctx), http.StatusInternalServerError)
|
nack(w, internalServerError(ctx), http.StatusInternalServerError, ctx)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,13 +61,14 @@ func TestNack(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
_, err := http.NewRequest("GET", "/", nil)
|
req, err := http.NewRequest("GET", "/", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err) // For tests
|
t.Fatal(err) // For tests
|
||||||
}
|
}
|
||||||
rr := httptest.NewRecorder()
|
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 {
|
if rr.Code != tt.status {
|
||||||
t.Errorf("expected status code %d, got %d", tt.status, rr.Code)
|
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 {
|
if body != tt.expected {
|
||||||
t.Errorf("expected body %s, got %s", tt.expected, body)
|
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)
|
return bytes.Equal(expectedBytes, actualBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestSendAck_WriteError(t *testing.T) {
|
func TestSendAck_WriteError(t *testing.T) {
|
||||||
w := &errorResponseWriter{}
|
w := &errorResponseWriter{}
|
||||||
SendAck(w)
|
SendAck(w)
|
||||||
// No need to assert, just ensure it doesn't panic
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user