-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new AbortReason type, use old RunStatus type only for k6 cloud code
RunStatus is very specific to the k6 cloud and doesn't map perfectly to how k6 OSS sees the causes of prematurely stopped tests. So, this commit restricts the usage of RunStatus only to the cloudapi/ package and to the `k6 cloud` command handling in `cmd/cloud.go`. It does that by introducing a new type, `errext.AbortReason`, and a way to attach this type to test run errors. This allows us a cleaner error propagation to the outputs, and every output can map these generic values to its own internal ones however it wants. It is not necessary for that mapping to be exactly 1:1 and, indeed, the `errext.AbortReason`:`cloudapi.RunStatus` mapping in cloudapi/ is not 1:1.
- Loading branch information
Showing
17 changed files
with
181 additions
and
79 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
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
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,54 @@ | ||
package errext | ||
|
||
import "errors" | ||
|
||
// AbortReason is used to signal to outputs what type of error caused the test | ||
// run to be stopped prematurely. | ||
type AbortReason uint8 | ||
|
||
// These are the various reasons why a test might have been aborted prematurely. | ||
const ( | ||
AbortedByUser AbortReason = iota + 1 | ||
AbortedByThreshold | ||
AbortedByThresholdsAfterTestEnd // TODO: rename? | ||
AbortedByScriptError | ||
AbortedByScriptAbort | ||
AbortedByTimeout | ||
) | ||
|
||
// HasAbortReason is a wrapper around an error with an attached abort reason. | ||
type HasAbortReason interface { | ||
error | ||
AbortReason() AbortReason | ||
} | ||
|
||
// WithAbortReasonIfNone can attach an abort reason to the given error, if it | ||
// doesn't have one already. It won't do anything if the error already had an | ||
// abort reason attached. Similarly, if there is no error (i.e. the given error | ||
// is nil), it also won't do anything and will return nil. | ||
func WithAbortReasonIfNone(err error, abortReason AbortReason) error { | ||
if err == nil { | ||
return nil // No error, do nothing | ||
} | ||
var arerr HasAbortReason | ||
if errors.As(err, &arerr) { | ||
// The given error already has an abort reason, do nothing | ||
return err | ||
} | ||
return withAbortReason{err, abortReason} | ||
} | ||
|
||
type withAbortReason struct { | ||
error | ||
abortReason AbortReason | ||
} | ||
|
||
func (ar withAbortReason) Unwrap() error { | ||
return ar.error | ||
} | ||
|
||
func (ar withAbortReason) AbortReason() AbortReason { | ||
return ar.abortReason | ||
} | ||
|
||
var _ HasAbortReason = withAbortReason{} |
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
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
Oops, something went wrong.