Skip to content

Commit

Permalink
fix: use better field name and preserve error response
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbrm committed Sep 12, 2024
1 parent a105c94 commit 397e0a4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
14 changes: 4 additions & 10 deletions services/skus/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,12 +967,9 @@ func handleVerifyCredV2(svc *Service, valid *validator.Validate) handlers.AppHan
}

if err := validateVerifyCredRequestV2(valid, req); err != nil {
verrs, ok := collectValidationErrors(err)
if !ok {
return handlers.ValidationError("request", map[string]interface{}{"request-body": err.Error()})
}
lg.Warn().Err(err).Msg("failed to validate request")

return handlers.ValidationError("request", verrs)
return handlers.WrapError(err, "Error in request body", http.StatusBadRequest)
}

aerr := svc.verifyCredential(ctx, req, w)
Expand Down Expand Up @@ -1005,12 +1002,9 @@ func handleVerifyCredV1(svc *Service, valid *validator.Validate) handlers.AppHan
}

if err := valid.StructCtx(ctx, req); err != nil {
verrs, ok := collectValidationErrors(err)
if !ok {
return handlers.ValidationError("request", map[string]interface{}{"request-body": err.Error()})
}
lg.Warn().Err(err).Msg("failed to validate request")

return handlers.ValidationError("request", verrs)
return handlers.WrapError(err, "Error in request validation", http.StatusBadRequest)
}

aerr := svc.verifyCredential(ctx, req, w)
Expand Down
12 changes: 6 additions & 6 deletions services/skus/controllers_noint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ func TestHandleReceiptErr(t *testing.T) {

func TestParseVerifyCredRequestV2(t *testing.T) {
type tcExpected struct {
val *model.VerifyCredentialRequestV2
errFn must.ErrorAssertionFunc
val *model.VerifyCredentialRequestV2
mustErr must.ErrorAssertionFunc
}

type testCase struct {
Expand All @@ -241,7 +241,7 @@ func TestParseVerifyCredRequestV2(t *testing.T) {
name: "error_malformed_payload",
given: []byte(`nonsense`),
exp: tcExpected{
errFn: func(tt must.TestingT, err error, i ...interface{}) {
mustErr: func(tt must.TestingT, err error, i ...interface{}) {
must.Equal(tt, true, err != nil)
},
},
Expand All @@ -251,7 +251,7 @@ func TestParseVerifyCredRequestV2(t *testing.T) {
name: "error_malformed_credential",
given: []byte(`{"sku":"sku","merchantId":"merchantId"}`),
exp: tcExpected{
errFn: func(tt must.TestingT, err error, i ...interface{}) {
mustErr: func(tt must.TestingT, err error, i ...interface{}) {
must.Equal(tt, true, err != nil)
},
},
Expand All @@ -270,7 +270,7 @@ func TestParseVerifyCredRequestV2(t *testing.T) {
Presentation: "TmF0dXJlIGFiaG9ycyBhIHZhY3V1bS4K",
},
},
errFn: func(tt must.TestingT, err error, i ...interface{}) {
mustErr: func(tt must.TestingT, err error, i ...interface{}) {
must.Equal(tt, true, err == nil)
},
},
Expand All @@ -282,7 +282,7 @@ func TestParseVerifyCredRequestV2(t *testing.T) {

t.Run(tc.name, func(t *testing.T) {
actual, err := parseVerifyCredRequestV2(tc.given)
tc.exp.errFn(t, err)
tc.exp.mustErr(t, err)

should.Equal(t, tc.exp.val, actual)
})
Expand Down

0 comments on commit 397e0a4

Please sign in to comment.