Skip to content

Commit

Permalink
- Maintain, deprecate error static variables
Browse files Browse the repository at this point in the history
  • Loading branch information
nwithan8 committed Aug 13, 2024
1 parent ebe1006 commit 6dbe6ed
Show file tree
Hide file tree
Showing 28 changed files with 62 additions and 52 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
## Next Release

- Fix inheritance tree for error types, allowing end-user to properly cast and check for specific error types and sub-types
- Some static error variables with non-dynamic error messages (e.g. `easypost.EndOfPaginationError`) have been removed and replaced with proper structs. Users relying on these variables to access the underlying error message should now use the error message directly (e.g. `easypost.NoPagesLeftToRetrieve`)
- Affected error variables and migration to static error messages:
- `easypost.EndOfPaginationError` -> `easypost.NoPagesLeftToRetrieve`
- `easypost.MissingWebhookSignatureError` -> `easypost.MissingWebhookSignature`
- `easypost.MismatchWebhookSignatureError` -> `easypost.MismatchWebhookSignature`

## v4.5.1 (2024-08-09)

Expand Down
2 changes: 1 addition & 1 deletion address.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (c *Client) GetNextAddressPageWithContext(ctx context.Context, collection *
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextAddressPageWithPageSizeWithContext(ctx context.Context, collection *ListAddressResult, pageSize int) (out *ListAddressResult, err error) {
if len(collection.Addresses) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.Addresses[len(collection.Addresses)-1].ID
Expand Down
2 changes: 1 addition & 1 deletion claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (c *Client) GetNextClaimPageWithContext(ctx context.Context, collection *Li
// GetNextClaimPageWithPageSizeWithContext performs the same operation as GetNextClaimPageWithPageSize, but allows specifying a context that can interrupt the request.
func (c *Client) GetNextClaimPageWithPageSizeWithContext(ctx context.Context, collection *ListClaimsResult, pageSize int) (out *ListClaimsResult, err error) {
if len(collection.Claims) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.Claims[len(collection.Claims)-1].ID
Expand Down
51 changes: 33 additions & 18 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,26 @@ func (e *LocalError) Unwrap() error {
return &e.LibraryError
}

// EndOfPaginationError is raised when there are no more pages to retrieve.
type EndOfPaginationError struct {
// EndOfPaginationErrorType is raised when there are no more pages to retrieve.
// TODO: This type will be renamed to EndOfPaginationError in a future release to match the other error types once the EndOfPaginationError helper is removed.
type EndOfPaginationErrorType struct {
LocalError // subtype of LocalError
}

// Unwrap returns the underlying LocalError error.
func (e *EndOfPaginationError) Unwrap() error {
func (e *EndOfPaginationErrorType) Unwrap() error {
return &e.LocalError
}

// newEndOfPaginationError returns a new EndOfPaginationError object.
func newEndOfPaginationError() *EndOfPaginationError {
return &EndOfPaginationError{LocalError{LibraryError{Message: NoPagesLeftToRetrieve}}}
// newEndOfPaginationError returns a new EndOfPaginationErrorType object.
func newEndOfPaginationError() *EndOfPaginationErrorType {
return &EndOfPaginationErrorType{LocalError{LibraryError{Message: NoPagesLeftToRetrieve}}}
}

// EndOfPaginationError is a singleton instance of EndOfPaginationErrorType.
// Deprecated: This helper will be removed in a future release. For access to the underlying message, use easypost.NoPagesLeftToRetrieve instead.
var EndOfPaginationError = newEndOfPaginationError()

// FilteringError is raised when there is an issue while running a filtering operation.
type FilteringError struct {
LocalError // subtype of LocalError
Expand Down Expand Up @@ -160,36 +165,46 @@ func newMissingPropertyError(property string) *MissingPropertyError {
return &MissingPropertyError{LocalError{LibraryError{Message: message}}}
}

// MissingWebhookSignatureError is raised when a webhook does not contain a valid HMAC signature.
type MissingWebhookSignatureError struct {
// MissingWebhookSignatureErrorType is raised when a webhook does not contain a valid HMAC signature.
// TODO: This type will be renamed to MissingWebhookSignatureError in a future release to match the other error types once the MissingWebhookSignatureError helper is removed.
type MissingWebhookSignatureErrorType struct {
LocalError // subtype of LocalError
}

// Unwrap returns the underlying LocalError error.
func (e *MissingWebhookSignatureError) Unwrap() error {
func (e *MissingWebhookSignatureErrorType) Unwrap() error {
return &e.LocalError
}

// newMissingWebhookSignatureError returns a new MissingWebhookSignatureError object.
func newMissingWebhookSignatureError() *MissingWebhookSignatureError {
return &MissingWebhookSignatureError{LocalError{LibraryError{Message: MissingWebhookSignature}}}
// newMissingWebhookSignatureError returns a new MissingWebhookSignatureErrorType object.
func newMissingWebhookSignatureError() *MissingWebhookSignatureErrorType {
return &MissingWebhookSignatureErrorType{LocalError{LibraryError{Message: MissingWebhookSignature}}}
}

// MismatchWebhookSignatureError is raised when a webhook received did not originate from EasyPost or had a webhook secret mismatch.
type MismatchWebhookSignatureError struct {
// MissingWebhookSignatureError is raised when a webhook does not contain a valid HMAC signature.
// Deprecated: This helper will be removed in a future release. For access to the underlying message, use easypost.MissingWebhookSignature instead.
var MissingWebhookSignatureError = newMissingWebhookSignatureError()

// MismatchWebhookSignatureErrorType is raised when a webhook received did not originate from EasyPost or had a webhook secret mismatch.
// TODO: This type will be renamed to MismatchWebhookSignatureError in a future release to match the other error types once the MismatchWebhookSignatureError helper is removed.
type MismatchWebhookSignatureErrorType struct {
LocalError // subtype of LocalError
}

// Unwrap returns the underlying LocalError error.
func (e *MismatchWebhookSignatureError) Unwrap() error {
func (e *MismatchWebhookSignatureErrorType) Unwrap() error {
return &e.LocalError
}

// newMismatchWebhookSignatureError returns a new MismatchWebhookSignatureError object.
func newMismatchWebhookSignatureError() *MismatchWebhookSignatureError {
return &MismatchWebhookSignatureError{LocalError{LibraryError{Message: MismatchWebhookSignature}}}
// newMismatchWebhookSignatureError returns a new MismatchWebhookSignatureErrorType object.
func newMismatchWebhookSignatureError() *MismatchWebhookSignatureErrorType {
return &MismatchWebhookSignatureErrorType{LocalError{LibraryError{Message: MismatchWebhookSignature}}}
}

// MismatchWebhookSignatureError is raised when a webhook received did not originate from EasyPost or had a webhook secret mismatch.
// Deprecated: This helper will be removed in a future release. For access to the underlying message, use easypost.MismatchWebhookSignature instead.
var MismatchWebhookSignatureError = newMismatchWebhookSignatureError()

// ExternalApiError represents an error caused by an external API, such as a 3rd party HTTP API (not EasyPost).
type ExternalApiError struct {
LibraryError // subtype of LibraryError
Expand Down
2 changes: 1 addition & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (c *Client) GetNextEventPageWithContext(ctx context.Context, collection *Li
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextEventPageWithPageSizeWithContext(ctx context.Context, collection *ListEventsResult, pageSize int) (out *ListEventsResult, err error) {
if len(collection.Events) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.Events[len(collection.Events)-1].ID
Expand Down
2 changes: 1 addition & 1 deletion insurance.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (c *Client) GetNextInsurancePageWithContext(ctx context.Context, collection
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextInsurancePageWithPageSizeWithContext(ctx context.Context, collection *ListInsurancesResult, pageSize int) (out *ListInsurancesResult, err error) {
if len(collection.Insurances) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.Insurances[len(collection.Insurances)-1].ID
Expand Down
4 changes: 2 additions & 2 deletions list_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ListOptions struct {
// If pageSize is 0, it will use the default page size
func nextPageParameters(hasMore bool, lastID string, pageSize int) (out *ListOptions, err error) {
if !hasMore {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
out = &ListOptions{
Expand All @@ -29,7 +29,7 @@ func nextPageParameters(hasMore bool, lastID string, pageSize int) (out *ListOpt
// If pageSize is 0, it will use the default page size
func nextChildUserPageParameters(hasMore bool, lastID string, pageSize int) (out *ListOptions, err error) {
if !hasMore {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
out = &ListOptions{
Expand Down
2 changes: 1 addition & 1 deletion pickup.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (c *Client) GetNextPickupPageWithContext(ctx context.Context, collection *L
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextPickupPageWithPageSizeWithContext(ctx context.Context, collection *ListPickupResult, pageSize int) (out *ListPickupResult, err error) {
if len(collection.Pickups) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.Pickups[len(collection.Pickups)-1].ID
Expand Down
2 changes: 1 addition & 1 deletion referral_customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (c *Client) GetNextReferralCustomerPageWithContext(ctx context.Context, col
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextReferralCustomerPageWithPageSizeWithContext(ctx context.Context, collection *ListReferralCustomersResult, pageSize int) (out *ListReferralCustomersResult, err error) {
if len(collection.ReferralCustomers) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.ReferralCustomers[len(collection.ReferralCustomers)-1].ID
Expand Down
2 changes: 1 addition & 1 deletion refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *Client) GetNextRefundPageWithContext(ctx context.Context, collection *L
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextRefundPageWithPageSizeWithContext(ctx context.Context, collection *ListRefundResult, pageSize int) (out *ListRefundResult, err error) {
if len(collection.Refunds) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.Refunds[len(collection.Refunds)-1].ID
Expand Down
2 changes: 1 addition & 1 deletion report.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *Client) GetNextReportPageWithContext(ctx context.Context, collection *L
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextReportPageWithPageSizeWithContext(ctx context.Context, collection *ListReportsResult, pageSize int) (out *ListReportsResult, err error) {
if len(collection.Reports) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.Reports[len(collection.Reports)-1].ID
Expand Down
2 changes: 1 addition & 1 deletion scan_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c *Client) GetNextScanFormPageWithContext(ctx context.Context, collection
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextScanFormPageWithPageSizeWithContext(ctx context.Context, collection *ListScanFormsResult, pageSize int) (out *ListScanFormsResult, err error) {
if len(collection.ScanForms) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.ScanForms[len(collection.ScanForms)-1].ID
Expand Down
2 changes: 1 addition & 1 deletion shipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (c *Client) GetNextShipmentPageWithContext(ctx context.Context, collection
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextShipmentPageWithPageSizeWithContext(ctx context.Context, collection *ListShipmentsResult, pageSize int) (out *ListShipmentsResult, err error) {
if len(collection.Shipments) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.Shipments[len(collection.Shipments)-1].ID
Expand Down
2 changes: 1 addition & 1 deletion tests/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *ClientTests) TestAddressGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (c *ClientTests) TestClaimGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/insurance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *ClientTests) TestInsuranceGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/paginated_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *ClientTests) TestGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func (c *ClientTests) TestGetNextPageWithPageSize() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pickup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (c *ClientTests) TestPickupsGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
2 changes: 1 addition & 1 deletion tests/referral_customer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (c *ClientTests) TestReferralCustomersGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
2 changes: 1 addition & 1 deletion tests/refund_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *ClientTests) TestRefundsGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
2 changes: 1 addition & 1 deletion tests/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *ClientTests) TestReportsGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
2 changes: 1 addition & 1 deletion tests/scan_form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *ClientTests) TestScanFormsGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
2 changes: 1 addition & 1 deletion tests/shipment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (c *ClientTests) TestShipmentsGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (c *ClientTests) TestTrackersGetNextPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
2 changes: 1 addition & 1 deletion tests/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (c *ClientTests) TestUserGetNextChildUserPage() {
}
}()
if err != nil {
assert.Equal(err.Error(), easypost.NoPagesLeftToRetrieve)
assert.Equal(err.Error(), easypost.EndOfPaginationError.Message)
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (c *Client) GetNextTrackerPageWithContext(ctx context.Context, collection *
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextTrackerPageWithPageSizeWithContext(ctx context.Context, collection *ListTrackersResult, pageSize int) (out *ListTrackersResult, err error) {
if len(collection.Trackers) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.Trackers[len(collection.Trackers)-1].ID
Expand Down
2 changes: 1 addition & 1 deletion user.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (c *Client) GetNextChildUserPageWithContext(ctx context.Context, collection
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextChildUserPageWithPageSizeWithContext(ctx context.Context, collection *ListChildUsersResult, pageSize int) (out *ListChildUsersResult, err error) {
if len(collection.Children) == 0 {
err = newEndOfPaginationError()
err = EndOfPaginationError
return
}
lastID := collection.Children[len(collection.Children)-1].ID
Expand Down
4 changes: 2 additions & 2 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ func (c *Client) ValidateWebhookWithContext(ctx context.Context, eventBody []byt
return &webhookBody, nil
}
} else {
return nil, newMismatchWebhookSignatureError()
return nil, MismatchWebhookSignatureError
}
} else {
return nil, newMissingWebhookSignatureError()
return nil, MissingWebhookSignatureError
}
}

0 comments on commit 6dbe6ed

Please sign in to comment.