added updated code for core wiring

1. Removed tracing
2. Skipped Registration
This commit is contained in:
MohitKatare-protean
2025-03-25 21:06:34 +05:30
parent 519cca19af
commit ec558558c5
87 changed files with 9279 additions and 711 deletions

View File

@@ -3,141 +3,121 @@ package response
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"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 ErrorType = "INVALID_REQUEST"
// 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"`
}
type Message struct {
Ack struct {
Status string `json:"status,omitempty"`
} `json:"ack,omitempty"`
Error *Error `json:"error,omitempty"`
}
type BecknResponse struct {
Context map[string]interface{} `json:"context,omitempty"`
Message Message `json:"message,omitempty"`
}
type ClientFailureBecknResponse struct {
Context map[string]interface{} `json:"context,omitempty"`
Error *Error `json:"error,omitempty"`
}
var errorMap = map[ErrorType]Error{
SchemaValidationErrorType: {
Code: "400",
Message: "Schema validation failed",
},
InvalidRequestErrorType: {
Code: "401",
Message: "Invalid request format",
},
}
var DefaultError = Error{
Code: "500",
Message: "Internal server error",
}
func Nack(ctx context.Context, tp ErrorType, paths string, body []byte) ([]byte, error) {
var req BecknRequest
if err := json.Unmarshal(body, &req); err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err)
}
errorObj, ok := errorMap[tp]
if paths != "" {
errorObj.Paths = paths
}
var response BecknResponse
if !ok {
response = BecknResponse{
Context: req.Context,
Message: Message{
Ack: struct {
Status string `json:"status,omitempty"`
}{
Status: "NACK",
},
Error: &DefaultError,
},
}
} else {
response = BecknResponse{
Context: req.Context,
Message: Message{
Ack: struct {
Status string `json:"status,omitempty"`
}{
Status: "NACK",
},
Error: &errorObj,
},
}
}
return json.Marshal(response)
}
func Ack(ctx context.Context, body []byte) ([]byte, error) {
var req BecknRequest
if err := json.Unmarshal(body, &req); err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err)
}
response := BecknResponse{
Context: req.Context,
Message: Message{
Ack: struct {
Status string `json:"status,omitempty"`
}{
Status: "ACK",
// 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{
Status: model.StatusACK,
},
},
}
return json.Marshal(response)
}
func HandleClientFailure(ctx context.Context, tp ErrorType, body []byte) ([]byte, error) {
var req BecknRequest
if err := json.Unmarshal(body, &req); err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err)
// Marshal to JSON
data, err := json.Marshal(resp)
if err != nil {
http.Error(w, "failed to marshal response", http.StatusInternalServerError)
return
}
errorObj, ok := errorMap[tp]
var response ClientFailureBecknResponse
// Set headers and write response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
if !ok {
response = ClientFailureBecknResponse{
Context: req.Context,
Error: &DefaultError,
}
} else {
response = ClientFailureBecknResponse{
Context: req.Context,
Error: &errorObj,
}
// 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
resp := &model.Response{
Message: model.Message{
Ack: model.Ack{
Status: model.StatusNACK,
},
Error: err,
},
}
return json.Marshal(response)
// Marshal the response to JSON
data, jsonErr := json.Marshal(resp)
if jsonErr != nil {
http.Error(w, "failed to marshal response", http.StatusInternalServerError)
return
}
// Set headers and write response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status) // Assuming NACK means a bad request
w.Write(data)
}
func internalServerError(ctx context.Context) *model.Error {
return &model.Error{
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
var badReqErr *model.BadReqErr
var notFoundErr *model.NotFoundErr
switch {
case errors.As(err, &schemaErr): // Custom application error
nack(w, schemaErr.BecknError(), http.StatusBadRequest)
return
case errors.As(err, &signErr):
nack(w, signErr.BecknError(), http.StatusUnauthorized)
return
case errors.As(err, &badReqErr):
nack(w, badReqErr.BecknError(), http.StatusBadRequest)
return
case errors.As(err, &notFoundErr):
nack(w, notFoundErr.BecknError(), http.StatusNotFound)
return
default:
nack(w, internalServerError(ctx), http.StatusInternalServerError)
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),
}
}

View File

@@ -1,303 +0,0 @@
package response
import (
"context"
"encoding/json"
"reflect"
"testing"
)
func TestNack(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
errorType ErrorType
requestBody string
wantStatus string
wantErrCode string
wantErrMsg string
wantErr bool
path string
}{
{
name: "Schema validation error",
errorType: SchemaValidationErrorType,
requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`,
wantStatus: "NACK",
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
path: "test",
},
{
name: "Invalid request error",
errorType: InvalidRequestErrorType,
requestBody: `{"context": {"domain": "test-domain"}}`,
wantStatus: "NACK",
wantErrCode: "401",
wantErrMsg: "Invalid request format",
wantErr: false,
path: "test",
},
{
name: "Unknown error type",
errorType: "UNKNOWN_ERROR",
requestBody: `{"context": {"domain": "test-domain"}}`,
wantStatus: "NACK",
wantErrCode: "500",
wantErrMsg: "Internal server error",
wantErr: false,
path: "test",
},
{
name: "Empty request body",
errorType: SchemaValidationErrorType,
requestBody: `{}`,
wantStatus: "NACK",
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
path: "test",
},
{
name: "Invalid JSON",
errorType: SchemaValidationErrorType,
requestBody: `{invalid json}`,
wantErr: true,
path: "test",
},
{
name: "Complex nested context",
errorType: SchemaValidationErrorType,
requestBody: `{"context": {"domain": "test-domain", "nested": {"key1": "value1", "key2": 123}}}`,
wantStatus: "NACK",
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
path: "test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := Nack(ctx, tt.errorType, tt.path, []byte(tt.requestBody))
if (err != nil) != tt.wantErr {
t.Errorf("Nack() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err != nil {
return
}
var becknResp BecknResponse
if err := json.Unmarshal(resp, &becknResp); err != nil {
t.Errorf("Failed to unmarshal response: %v", err)
return
}
if becknResp.Message.Ack.Status != tt.wantStatus {
t.Errorf("Nack() status = %v, want %v", becknResp.Message.Ack.Status, tt.wantStatus)
}
if becknResp.Message.Error.Code != tt.wantErrCode {
t.Errorf("Nack() error code = %v, want %v", becknResp.Message.Error.Code, tt.wantErrCode)
}
if becknResp.Message.Error.Message != tt.wantErrMsg {
t.Errorf("Nack() error message = %v, want %v", becknResp.Message.Error.Message, tt.wantErrMsg)
}
var origReq BecknRequest
if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil {
if !compareContexts(becknResp.Context, origReq.Context) {
t.Errorf("Nack() context not preserved, got = %v, want %v", becknResp.Context, origReq.Context)
}
}
})
}
}
func TestAck(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
requestBody string
wantStatus string
wantErr bool
}{
{
name: "Valid request",
requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`,
wantStatus: "ACK",
wantErr: false,
},
{
name: "Empty context",
requestBody: `{"context": {}}`,
wantStatus: "ACK",
wantErr: false,
},
{
name: "Invalid JSON",
requestBody: `{invalid json}`,
wantErr: true,
},
{
name: "Complex nested context",
requestBody: `{"context": {"domain": "test-domain", "nested": {"key1": "value1", "key2": 123, "array": [1,2,3]}}}`,
wantStatus: "ACK",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := Ack(ctx, []byte(tt.requestBody))
if (err != nil) != tt.wantErr {
t.Errorf("Ack() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err != nil {
return
}
var becknResp BecknResponse
if err := json.Unmarshal(resp, &becknResp); err != nil {
t.Errorf("Failed to unmarshal response: %v", err)
return
}
if becknResp.Message.Ack.Status != tt.wantStatus {
t.Errorf("Ack() status = %v, want %v", becknResp.Message.Ack.Status, tt.wantStatus)
}
if becknResp.Message.Error != nil {
t.Errorf("Ack() should not have error, got %v", becknResp.Message.Error)
}
var origReq BecknRequest
if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil {
if !compareContexts(becknResp.Context, origReq.Context) {
t.Errorf("Ack() context not preserved, got = %v, want %v", becknResp.Context, origReq.Context)
}
}
})
}
}
func TestHandleClientFailure(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
errorType ErrorType
requestBody string
wantErrCode string
wantErrMsg string
wantErr bool
}{
{
name: "Schema validation error",
errorType: SchemaValidationErrorType,
requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`,
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
},
{
name: "Invalid request error",
errorType: InvalidRequestErrorType,
requestBody: `{"context": {"domain": "test-domain"}}`,
wantErrCode: "401",
wantErrMsg: "Invalid request format",
wantErr: false,
},
{
name: "Unknown error type",
errorType: "UNKNOWN_ERROR",
requestBody: `{"context": {"domain": "test-domain"}}`,
wantErrCode: "500",
wantErrMsg: "Internal server error",
wantErr: false,
},
{
name: "Invalid JSON",
errorType: SchemaValidationErrorType,
requestBody: `{invalid json}`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := HandleClientFailure(ctx, tt.errorType, []byte(tt.requestBody))
if (err != nil) != tt.wantErr {
t.Errorf("HandleClientFailure() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err != nil {
return
}
var failureResp ClientFailureBecknResponse
if err := json.Unmarshal(resp, &failureResp); err != nil {
t.Errorf("Failed to unmarshal response: %v", err)
return
}
if failureResp.Error.Code != tt.wantErrCode {
t.Errorf("HandleClientFailure() error code = %v, want %v", failureResp.Error.Code, tt.wantErrCode)
}
if failureResp.Error.Message != tt.wantErrMsg {
t.Errorf("HandleClientFailure() error message = %v, want %v", failureResp.Error.Message, tt.wantErrMsg)
}
var origReq BecknRequest
if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil {
if !compareContexts(failureResp.Context, origReq.Context) {
t.Errorf("HandleClientFailure() context not preserved, got = %v, want %v", failureResp.Context, origReq.Context)
}
}
})
}
}
func TestErrorMap(t *testing.T) {
expectedTypes := []ErrorType{
SchemaValidationErrorType,
InvalidRequestErrorType,
}
for _, tp := range expectedTypes {
if _, exists := errorMap[tp]; !exists {
t.Errorf("ErrorType %v not found in errorMap", tp)
}
}
if DefaultError.Code != "500" || DefaultError.Message != "Internal server error" {
t.Errorf("DefaultError not set correctly, got code=%v, message=%v", DefaultError.Code, DefaultError.Message)
}
}
func compareContexts(c1, c2 map[string]interface{}) bool {
if c1 == nil && c2 == nil {
return true
}
if c1 == nil && len(c2) == 0 || c2 == nil && len(c1) == 0 {
return true
}
return reflect.DeepEqual(c1, c2)
}