Merge branch 'beckn-onix-v1.0-develop' of https://github.com/beckn/beckn-onix into feature/core
This commit is contained in:
@@ -6,31 +6,40 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/log"
|
||||
"strings"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/model"
|
||||
)
|
||||
|
||||
// ErrorType represents different types of errors in the Beckn protocol.
|
||||
type ErrorType string
|
||||
|
||||
const (
|
||||
// SchemaValidationErrorType represents an error due to schema validation failure.
|
||||
SchemaValidationErrorType ErrorType = "SCHEMA_VALIDATION_ERROR"
|
||||
|
||||
// InvalidRequestErrorType represents an error due to an invalid request.
|
||||
InvalidRequestErrorType ErrorType = "INVALID_REQUEST"
|
||||
)
|
||||
|
||||
// BecknRequest represents a generic Beckn request with an optional context.
|
||||
type BecknRequest struct {
|
||||
Context map[string]interface{} `json:"context,omitempty"`
|
||||
type Error struct {
|
||||
Code string `json:"code,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Paths string `json:"paths,omitempty"`
|
||||
}
|
||||
|
||||
// SchemaValidationErr represents a collection of schema validation failures.
|
||||
type SchemaValidationErr struct {
|
||||
Errors []Error
|
||||
}
|
||||
|
||||
// Error implements the error interface for SchemaValidationErr.
|
||||
func (e *SchemaValidationErr) Error() string {
|
||||
var errorMessages []string
|
||||
for _, err := range e.Errors {
|
||||
errorMessages = append(errorMessages, fmt.Sprintf("%s: %s", err.Paths, err.Message))
|
||||
}
|
||||
return strings.Join(errorMessages, "; ")
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Ack struct {
|
||||
Status string `json:"status,omitempty"`
|
||||
} `json:"ack,omitempty"`
|
||||
Error *Error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// SendAck sends an acknowledgment (ACK) response indicating a successful request processing.
|
||||
func SendAck(w http.ResponseWriter) {
|
||||
// Create the response object
|
||||
resp := &model.Response{
|
||||
Message: model.Message{
|
||||
Ack: model.Ack{
|
||||
@@ -39,25 +48,18 @@ func SendAck(w http.ResponseWriter) {
|
||||
},
|
||||
}
|
||||
|
||||
// Marshal to JSON
|
||||
data, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to marshal response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data, _ := json.Marshal(resp) //should not fail here
|
||||
|
||||
// Set headers and write response
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if _, err := w.Write(data); err != nil {
|
||||
log.Error(context.Background(), err, "failed to write ack response")
|
||||
_, err := w.Write(data)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to write response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// nack sends a negative acknowledgment (NACK) response with an error message.
|
||||
func nack(w http.ResponseWriter, err *model.Error, status int) {
|
||||
// Create the NACK response object
|
||||
func nack(w http.ResponseWriter, err *model.Error, status int, ctx context.Context) {
|
||||
resp := &model.Response{
|
||||
Message: model.Message{
|
||||
Ack: model.Ack{
|
||||
@@ -66,30 +68,25 @@ func nack(w http.ResponseWriter, err *model.Error, status int) {
|
||||
Error: err,
|
||||
},
|
||||
}
|
||||
data, _ := json.Marshal(resp) //should not fail here
|
||||
|
||||
// Marshal the response to JSON
|
||||
data, jsonErr := json.Marshal(resp)
|
||||
if jsonErr != nil {
|
||||
http.Error(w, "failed to marshal response", http.StatusInternalServerError)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_, er := w.Write(data)
|
||||
if er != nil {
|
||||
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
|
||||
}
|
||||
|
||||
// Set headers and write response
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status) // Assuming NACK means a bad request
|
||||
if _, err := w.Write(data); err != nil {
|
||||
log.Error(context.Background(), err, "failed to write nack response")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func internalServerError(ctx context.Context) *model.Error {
|
||||
return &model.Error{
|
||||
Code: http.StatusText(http.StatusInternalServerError),
|
||||
Message: fmt.Sprintf("Internal server error, MessageID: %s", ctx.Value(model.MsgIDKey)),
|
||||
}
|
||||
}
|
||||
|
||||
// SendNack sends a negative acknowledgment (NACK) response with an error message.
|
||||
func SendNack(ctx context.Context, w http.ResponseWriter, err error) {
|
||||
var schemaErr *model.SchemaValidationErr
|
||||
var signErr *model.SignValidationErr
|
||||
@@ -97,34 +94,20 @@ func SendNack(ctx context.Context, w http.ResponseWriter, err error) {
|
||||
var notFoundErr *model.NotFoundErr
|
||||
|
||||
switch {
|
||||
case errors.As(err, &schemaErr): // Custom application error
|
||||
nack(w, schemaErr.BecknError(), http.StatusBadRequest)
|
||||
case errors.As(err, &schemaErr):
|
||||
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, ¬FoundErr):
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// BecknError generates a standardized Beckn error response.
|
||||
func BecknError(ctx context.Context, err error, status int) *model.Error {
|
||||
msg := err.Error()
|
||||
msgID := ctx.Value(model.MsgIDKey)
|
||||
if status == http.StatusInternalServerError {
|
||||
|
||||
msg = "Internal server error"
|
||||
}
|
||||
return &model.Error{
|
||||
Message: fmt.Sprintf("%s. MessageID: %s.", msg, msgID),
|
||||
Code: strconv.Itoa(status),
|
||||
}
|
||||
}
|
||||
|
||||
256
pkg/response/response_test.go
Normal file
256
pkg/response/response_test.go
Normal file
@@ -0,0 +1,256 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/model"
|
||||
)
|
||||
|
||||
type errorResponseWriter struct{}
|
||||
|
||||
// TODO: Optimize the cases by removing these
|
||||
func (e *errorResponseWriter) Write([]byte) (int, error) {
|
||||
return 0, errors.New("write error")
|
||||
}
|
||||
func (e *errorResponseWriter) WriteHeader(statusCode int) {}
|
||||
|
||||
func (e *errorResponseWriter) Header() http.Header {
|
||||
return http.Header{}
|
||||
}
|
||||
|
||||
func TestSendAck(t *testing.T) {
|
||||
_, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err) // For tests
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
SendAck(rr)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("wanted status code %d, got %d", http.StatusOK, rr.Code)
|
||||
}
|
||||
|
||||
expected := `{"message":{"ack":{"status":"ACK"}}}`
|
||||
if rr.Body.String() != expected {
|
||||
t.Errorf("err.Error() = %s, want %s",
|
||||
rr.Body.String(), expected)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendNack(t *testing.T) {
|
||||
ctx := context.WithValue(context.Background(), model.MsgIDKey, "123456")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
expected string
|
||||
status int
|
||||
}{
|
||||
{
|
||||
name: "SchemaValidationErr",
|
||||
err: &model.SchemaValidationErr{
|
||||
Errors: []model.Error{
|
||||
{Paths: "/path1", Message: "Error 1"},
|
||||
{Paths: "/path2", Message: "Error 2"},
|
||||
},
|
||||
},
|
||||
status: http.StatusBadRequest,
|
||||
expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Bad Request","paths":"/path1;/path2","message":"Error 1; Error 2"}}}`,
|
||||
},
|
||||
{
|
||||
name: "SignValidationErr",
|
||||
err: model.NewSignValidationErr(errors.New("signature invalid")),
|
||||
status: http.StatusUnauthorized,
|
||||
expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Unauthorized","message":"Signature Validation Error: signature invalid"}}}`,
|
||||
},
|
||||
{
|
||||
name: "BadReqErr",
|
||||
err: model.NewBadReqErr(errors.New("bad request error")),
|
||||
status: http.StatusBadRequest,
|
||||
expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Bad Request","message":"BAD Request: bad request error"}}}`,
|
||||
},
|
||||
{
|
||||
name: "NotFoundErr",
|
||||
err: model.NewNotFoundErr(errors.New("endpoint not found")),
|
||||
status: http.StatusNotFound,
|
||||
expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Not Found","message":"Endpoint not found: endpoint not found"}}}`,
|
||||
},
|
||||
{
|
||||
name: "InternalServerError",
|
||||
err: errors.New("unexpected error"),
|
||||
status: http.StatusInternalServerError,
|
||||
expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Internal Server Error","message":"Internal server error, MessageID: 123456"}}}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err) // For tests
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
SendNack(ctx, rr, tt.err)
|
||||
|
||||
if rr.Code != tt.status {
|
||||
t.Errorf("wanted status code %d, got %d", tt.status, rr.Code)
|
||||
}
|
||||
|
||||
var actual map[string]interface{}
|
||||
err = json.Unmarshal(rr.Body.Bytes(), &actual)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
var expected map[string]interface{}
|
||||
err = json.Unmarshal([]byte(tt.expected), &expected)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal expected response: %v", err)
|
||||
}
|
||||
|
||||
if !compareJSON(expected, actual) {
|
||||
t.Errorf("err.Error() = %s, want %s",
|
||||
actual, expected)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaValidationErr_Error(t *testing.T) {
|
||||
// Create sample validation errors
|
||||
validationErrors := []Error{
|
||||
{Paths: "name", Message: "Name is required"},
|
||||
{Paths: "email", Message: "Invalid email format"},
|
||||
}
|
||||
err := SchemaValidationErr{Errors: validationErrors}
|
||||
expected := "name: Name is required; email: Invalid email format"
|
||||
if err.Error() != expected {
|
||||
t.Errorf("err.Error() = %s, want %s",
|
||||
err.Error(), expected)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func compareJSON(expected, actual map[string]interface{}) bool {
|
||||
expectedBytes, _ := json.Marshal(expected)
|
||||
actualBytes, _ := json.Marshal(actual)
|
||||
return bytes.Equal(expectedBytes, actualBytes)
|
||||
}
|
||||
|
||||
func TestSendAck_WriteError(t *testing.T) {
|
||||
w := &errorResponseWriter{}
|
||||
SendAck(w)
|
||||
}
|
||||
|
||||
// Mock struct to force JSON marshalling error
|
||||
type badMessage struct{}
|
||||
|
||||
func (b *badMessage) MarshalJSON() ([]byte, error) {
|
||||
return nil, errors.New("marshal error")
|
||||
}
|
||||
|
||||
func TestNack_1(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err *model.Error
|
||||
status int
|
||||
expected string
|
||||
useBadJSON bool
|
||||
useBadWrite bool
|
||||
}{
|
||||
{
|
||||
name: "Schema Validation Error",
|
||||
err: &model.Error{
|
||||
Code: "BAD_REQUEST",
|
||||
Paths: "/test/path",
|
||||
Message: "Invalid schema",
|
||||
},
|
||||
status: http.StatusBadRequest,
|
||||
expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"BAD_REQUEST","paths":"/test/path","message":"Invalid schema"}}}`,
|
||||
},
|
||||
{
|
||||
name: "Internal Server Error",
|
||||
err: &model.Error{
|
||||
Code: "INTERNAL_SERVER_ERROR",
|
||||
Message: "Something went wrong",
|
||||
},
|
||||
status: http.StatusInternalServerError,
|
||||
expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"INTERNAL_SERVER_ERROR","message":"Something went wrong"}}}`,
|
||||
},
|
||||
{
|
||||
name: "JSON Marshal Error",
|
||||
err: nil, // This will be overridden to cause marshaling error
|
||||
status: http.StatusInternalServerError,
|
||||
expected: `Internal server error, MessageID: 12345`,
|
||||
useBadJSON: true,
|
||||
},
|
||||
{
|
||||
name: "Write Error",
|
||||
err: &model.Error{
|
||||
Code: "WRITE_ERROR",
|
||||
Message: "Failed to write response",
|
||||
},
|
||||
status: http.StatusInternalServerError,
|
||||
expected: `Internal server error, MessageID: 12345`,
|
||||
useBadWrite: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.WithValue(req.Context(), model.MsgIDKey, "12345")
|
||||
|
||||
var w http.ResponseWriter
|
||||
if tt.useBadWrite {
|
||||
w = &errorResponseWriter{} // Simulate write error
|
||||
} else {
|
||||
w = httptest.NewRecorder()
|
||||
}
|
||||
|
||||
// TODO: Fix this approach , should not be used like this.
|
||||
if tt.useBadJSON {
|
||||
data, _ := json.Marshal(&badMessage{})
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(tt.status)
|
||||
_, err := w.Write(data)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to write response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
nack(w, tt.err, tt.status, ctx)
|
||||
if !tt.useBadWrite {
|
||||
recorder, ok := w.(*httptest.ResponseRecorder)
|
||||
if !ok {
|
||||
t.Fatal("Failed to cast response recorder")
|
||||
}
|
||||
|
||||
if recorder.Code != tt.status {
|
||||
t.Errorf("wanted status code %d, got %d", tt.status, recorder.Code)
|
||||
}
|
||||
|
||||
body := recorder.Body.String()
|
||||
if body != tt.expected {
|
||||
t.Errorf("err.Error() = %s, want %s",
|
||||
body, tt.expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user