Skip to content

Commit

Permalink
add UnmarshalTypeError checker for more descriptive error msgs (#88)
Browse files Browse the repository at this point in the history
* add UnmarshalTypeError checker for more descriptive error msgs

* just for testing

* corrected field in error message

* changelog

* prep for ancla v0.3.3 release
  • Loading branch information
mtrinh11 authored Nov 16, 2021
1 parent a2c1aca commit 3542233
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [v0.3.3]
- Removed legacy logic condition for inserting a webhook. [#85](https://github.com/xmidt-org/ancla/pull/85)
- Changed webhook address handling. [#84](https://github.com/xmidt-org/ancla/pull/84)
- Changed webhook unmarshaling error to be more descriptive. [#88](https://github.com/xmidt-org/ancla/pull/88)

## [v0.3.2]
- Added PartnerID and WebhookValidation to Config. [#83](https://github.com/xmidt-org/ancla/pull/83)
Expand Down Expand Up @@ -75,7 +78,8 @@ internalWebhooks. [#80](https://github.com/xmidt-org/ancla/pull/80)
## [v0.1.0]
- Initial release

[Unreleased]: https://github.com/xmidt-org/ancla/compare/v0.3.2...HEAD
[Unreleased]: https://github.com/xmidt-org/ancla/compare/v0.3.3...HEAD
[v0.3.3]: https://github.com/xmidt-org/ancla/compare/0.3.2...v0.3.3
[v0.3.2]: https://github.com/xmidt-org/ancla/compare/0.3.1...v0.3.2
[v0.3.1]: https://github.com/xmidt-org/ancla/compare/0.3.0...v0.3.1
[v0.3.0]: https://github.com/xmidt-org/ancla/compare/0.2.4...v0.3.0
Expand Down
4 changes: 4 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func addWebhookRequestDecoder(config transportConfig) kithttp.DecodeRequestFunc

err = json.Unmarshal(requestPayload, &webhook)
if err != nil {
var e *json.UnmarshalTypeError
if errors.As(err, &e) {
return nil, &erraux.Error{Err: fmt.Errorf("%w: %v must be of type %v", errFailedWebhookUnmarshal, e.Field, e.Type), Code: http.StatusBadRequest}
}
return nil, &erraux.Error{Err: fmt.Errorf("%w: %v", errFailedWebhookUnmarshal, err), Code: http.StatusBadRequest}
}

Expand Down
27 changes: 27 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ func TestAddWebhookRequestDecoder(t *testing.T) {
ExpectedStatusCode: 400,
Auth: "jwt",
},
{
Description: "Failed to JSON Unmarshal",
InputPayload: addWebhookDecoderUnmarshalingErrorInput(),
ExpectedErr: errFailedWebhookUnmarshal,
Validator: Validators{},
ExpectedStatusCode: 400,
Auth: "jwt",
},
{
Description: "Webhook validation Failure",
InputPayload: addWebhookDecoderInput(),
Expand Down Expand Up @@ -355,6 +363,25 @@ func addWebhookDecoderInput() string {
`
}

func addWebhookDecoderUnmarshalingErrorInput() string {
return `
{
"config": {
"url": "http://deliver-here-0.example.net",
"content_type": "application/json",
"secret": "superSecretXYZ"
},
"events": ["online"],
"matcher": {
"device_id": ["mac:aabbccddee.*"]
},
"failure_url": "http://contact-here-when-fails.example.net",
"duration": "hehe",
"until": "2021-01-02T15:04:10Z"
}
`
}

func addWebhookDecoderOutput(withPIDs bool) *addWebhookRequest {
if withPIDs {
return &addWebhookRequest{
Expand Down

0 comments on commit 3542233

Please sign in to comment.