forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for a WithMessage modifier on packit.Fail
For example, a call to `packit.Fail.WithMessage("my error")` will result in an error that triggers the correct fail exit code for the buildpack lifecycle, and also included the given message. This change is backwards compatible and so should not impact any buildpacks currently just using `packit.Fail`.
- Loading branch information
1 parent
ccfe769
commit 9a35314
Showing
7 changed files
with
74 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package packit | ||
|
||
import "github.com/paketo-buildpacks/packit/internal" | ||
|
||
// Fail is a sentinal value that can be used to indicate a failure to detect | ||
// during the detect phase. Fail implements the Error interface and should be | ||
// returned as the error value in the DetectFunc signature. Fail also supports | ||
// a modifier function, WithMessage, that allows the caller to set a custom | ||
// failure message. The WithMessage function supports a fmt.Printf-like format | ||
// string and variadic arguments to build a message, eg: | ||
// packit.Fail.WithMessage("failed: %w", err). | ||
var Fail = internal.Fail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package internal | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var Fail = failError{error: errors.New("failed")} | ||
|
||
type failError struct { | ||
error | ||
} | ||
|
||
func (f failError) WithMessage(format string, v ...interface{}) failError { | ||
return failError{error: fmt.Errorf(format, v...)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package internal_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/paketo-buildpacks/packit/internal" | ||
"github.com/sclevine/spec" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func testFail(t *testing.T, context spec.G, it spec.S) { | ||
var ( | ||
Expect = NewWithT(t).Expect | ||
) | ||
|
||
it("acts as an error", func() { | ||
fail := internal.Fail | ||
Expect(fail).To(MatchError("failed")) | ||
}) | ||
|
||
context("when given a message", func() { | ||
it("acts as an error with that message", func() { | ||
fail := internal.Fail.WithMessage("this is a %s", "failure message") | ||
Expect(fail).To(MatchError("this is a failure message")) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters