Skip to content

Commit

Permalink
Return a typed error when no dependencies are found during resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
robdimsdale committed Nov 14, 2022
1 parent 86b7e87 commit 1820200
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 21 additions & 7 deletions postal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
Expand Down
4 changes: 3 additions & 1 deletion postal/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]")))
})
})
Expand Down

0 comments on commit 1820200

Please sign in to comment.