fix: changes in document added

This commit is contained in:
mayur.popli
2025-03-17 17:52:31 +05:30
parent 08ba115e7a
commit 7f47bd60be
2 changed files with 14 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ type BecknRequest struct {
type Error struct { type Error struct {
Code string `json:"code,omitempty"` Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
Paths string `json:"paths,omitempty"`
} }
type Message struct { type Message struct {
@@ -55,13 +56,17 @@ var DefaultError = Error{
Message: "Internal server error", Message: "Internal server error",
} }
func Nack(ctx context.Context, tp ErrorType, body []byte) ([]byte, error) { func Nack(ctx context.Context, tp ErrorType, paths string, body []byte) ([]byte, error) {
var req BecknRequest var req BecknRequest
if err := json.Unmarshal(body, &req); err != nil { if err := json.Unmarshal(body, &req); err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err) return nil, fmt.Errorf("failed to parse request: %w", err)
} }
errorObj, ok := errorMap[tp] errorObj, ok := errorMap[tp]
if paths != "" {
errorObj.Paths = paths
}
var response BecknResponse var response BecknResponse
if !ok { if !ok {

View File

@@ -18,6 +18,7 @@ func TestNack(t *testing.T) {
wantErrCode string wantErrCode string
wantErrMsg string wantErrMsg string
wantErr bool wantErr bool
path string
}{ }{
{ {
name: "Schema validation error", name: "Schema validation error",
@@ -27,6 +28,7 @@ func TestNack(t *testing.T) {
wantErrCode: "400", wantErrCode: "400",
wantErrMsg: "Schema validation failed", wantErrMsg: "Schema validation failed",
wantErr: false, wantErr: false,
path: "test",
}, },
{ {
name: "Invalid request error", name: "Invalid request error",
@@ -36,6 +38,7 @@ func TestNack(t *testing.T) {
wantErrCode: "401", wantErrCode: "401",
wantErrMsg: "Invalid request format", wantErrMsg: "Invalid request format",
wantErr: false, wantErr: false,
path: "test",
}, },
{ {
name: "Unknown error type", name: "Unknown error type",
@@ -45,6 +48,7 @@ func TestNack(t *testing.T) {
wantErrCode: "500", wantErrCode: "500",
wantErrMsg: "Internal server error", wantErrMsg: "Internal server error",
wantErr: false, wantErr: false,
path: "test",
}, },
{ {
name: "Empty request body", name: "Empty request body",
@@ -54,12 +58,14 @@ func TestNack(t *testing.T) {
wantErrCode: "400", wantErrCode: "400",
wantErrMsg: "Schema validation failed", wantErrMsg: "Schema validation failed",
wantErr: false, wantErr: false,
path: "test",
}, },
{ {
name: "Invalid JSON", name: "Invalid JSON",
errorType: SchemaValidationErrorType, errorType: SchemaValidationErrorType,
requestBody: `{invalid json}`, requestBody: `{invalid json}`,
wantErr: true, wantErr: true,
path: "test",
}, },
{ {
name: "Complex nested context", name: "Complex nested context",
@@ -69,12 +75,13 @@ func TestNack(t *testing.T) {
wantErrCode: "400", wantErrCode: "400",
wantErrMsg: "Schema validation failed", wantErrMsg: "Schema validation failed",
wantErr: false, wantErr: false,
path: "test",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
resp, err := Nack(ctx, tt.errorType, []byte(tt.requestBody)) resp, err := Nack(ctx, tt.errorType, tt.path, []byte(tt.requestBody))
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("Nack() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("Nack() error = %v, wantErr %v", err, tt.wantErr)