From 18202009038b0df285ba0fb7d8b43abbf60d3ed0 Mon Sep 17 00:00:00 2001 From: Rob Dimsdale-Zucker Date: Thu, 10 Nov 2022 11:18:21 -0500 Subject: [PATCH] Return a typed error when no dependencies are found during resolution. --- postal/service.go | 28 +++++++++++++++++++++------- postal/service_test.go | 4 +++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/postal/service.go b/postal/service.go index 3292ee7d..07fa8fe3 100644 --- a/postal/service.go +++ b/postal/service.go @@ -37,6 +37,26 @@ type MappingResolver interface { FindDependencyMapping(checksum, platformDir string) (string, error) } +// ErrNoDeps is a typed error indicating that no dependencies were resolved during Service.Resolve() +// +// errors can be tested against this type with: errors.As() +type ErrNoDeps struct { + id string + version string + stack string + supportedVersions []string +} + +// Error implements the error.Error interface +func (e *ErrNoDeps) Error() string { + return fmt.Sprintf("failed to satisfy %q dependency version constraint %q: no compatible versions on %q stack. Supported versions are: [%s]", + e.id, + e.version, + e.stack, + strings.Join(e.supportedVersions, ", "), + ) +} + // Service provides a mechanism for resolving and installing dependencies given // a Transport. type Service struct { @@ -123,13 +143,7 @@ func (s Service) Resolve(path, id, version, stack string) (Dependency, error) { } if len(compatibleVersions) == 0 { - return Dependency{}, fmt.Errorf( - "failed to satisfy %q dependency version constraint %q: no compatible versions on %q stack. Supported versions are: [%s]", - id, - version, - stack, - strings.Join(supportedVersions, ", "), - ) + return Dependency{}, &ErrNoDeps{id, version, stack, supportedVersions} } stacksForVersion := map[string][]string{} diff --git a/postal/service_test.go b/postal/service_test.go index 868dccf5..f46af900 100644 --- a/postal/service_test.go +++ b/postal/service_test.go @@ -420,8 +420,10 @@ version = "1.2.3" }) context("when the entry version constraint cannot be satisfied", func() { - it("returns an error with all the supported versions listed", func() { + it("returns a typed error with all the supported versions listed", func() { + expectedErr := &postal.ErrNoDeps{} _, err := service.Resolve(path, "some-entry", "9.9.9", "some-stack") + Expect(errors.As(err, &expectedErr)).To(BeTrue()) Expect(err).To(MatchError(ContainSubstring("failed to satisfy \"some-entry\" dependency version constraint \"9.9.9\": no compatible versions on \"some-stack\" stack. Supported versions are: [1.2.3, 4.5.6]"))) }) })