From 354223307fb71c669d429c7509fa2885f1b154bf Mon Sep 17 00:00:00 2001 From: mtrinh11 Date: Tue, 16 Nov 2021 12:23:48 -0800 Subject: [PATCH] add UnmarshalTypeError checker for more descriptive error msgs (#88) * add UnmarshalTypeError checker for more descriptive error msgs * just for testing * corrected field in error message * changelog * prep for ancla v0.3.3 release --- CHANGELOG.md | 6 +++++- transport.go | 4 ++++ transport_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6094f60..904c930 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) @@ -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 diff --git a/transport.go b/transport.go index a23a341..25ba556 100644 --- a/transport.go +++ b/transport.go @@ -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} } diff --git a/transport_test.go b/transport_test.go index f22b860..bceb5ea 100644 --- a/transport_test.go +++ b/transport_test.go @@ -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(), @@ -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{