forked from tektoncd/pipeline
-
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.
Label
user error
for failure PipelineRun Status
This commit labels the user errors for failed PipelineRun status. This aims to communicate explicitly with users of whether the run failed could be attributed to users' responsibility. /kind misc part of tektoncd#7276
- Loading branch information
Showing
13 changed files
with
320 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2023 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package errors | ||
|
||
const userErrorLabel = "[User error] " | ||
|
||
type UserError struct { | ||
Reason string | ||
Original error | ||
} | ||
|
||
var _ error = &UserError{} | ||
|
||
// Error returns the original error message. This implements the error.Error interface. | ||
func (e *UserError) Error() string { | ||
return e.Original.Error() | ||
} | ||
|
||
// Unwrap returns the original error without the Reason annotation. This is | ||
// intended to support usage of errors.Is and errors.As with Errors. | ||
func (e *UserError) Unwrap() error { | ||
return e.Original | ||
} | ||
|
||
// newUserError returns a UserError with the given reason and underlying | ||
// original error. | ||
func newUserError(reason string, err error) *UserError { | ||
return &UserError{ | ||
Reason: reason, | ||
Original: err, | ||
} | ||
} | ||
|
||
// WrapUserError wraps the original error with the user error label | ||
func WrapUserError(err error) error { | ||
return newUserError(userErrorLabel, err) | ||
} | ||
|
||
// LabelUserError labels the failure RunStatus message if any of its error messages has been | ||
// wrapped as an UserError. It indicates that the user is responsible for an error. | ||
// See github.com/tektoncd/pipeline/blob/main/docs/pipelineruns.md#marking-off-user-errors | ||
// for more details. | ||
func LabelUserError(messageFormat string, messageA []interface{}) string { | ||
for _, message := range messageA { | ||
if ue, ok := message.(*UserError); ok { | ||
return ue.Reason + messageFormat | ||
} | ||
} | ||
return messageFormat | ||
} |
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,98 @@ | ||
/* | ||
Copyright 2023 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package errors_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
pipelineErrors "github.com/tektoncd/pipeline/pkg/apis/pipeline/errors" | ||
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" | ||
) | ||
|
||
type TestError struct{} | ||
|
||
var _ error = &TestError{} | ||
|
||
func (*TestError) Error() string { | ||
return "test error" | ||
} | ||
|
||
func TestUserErrorUnwrap(t *testing.T) { | ||
originalError := &TestError{} | ||
userError := pipelineErrors.WrapUserError(originalError) | ||
|
||
if !errors.Is(userError, &TestError{}) { | ||
t.Errorf("user error expected to unwrap successfully") | ||
} | ||
} | ||
|
||
func TestResolutionErrorMessage(t *testing.T) { | ||
originalError := &TestError{} | ||
expectedErrorMessage := originalError.Error() | ||
|
||
userError := pipelineErrors.WrapUserError(originalError) | ||
|
||
if userError.Error() != expectedErrorMessage { | ||
t.Errorf("user error message expected to equal to %s, got: %s", expectedErrorMessage, userError.Error()) | ||
} | ||
} | ||
|
||
func TestLabelsUserError(t *testing.T) { | ||
const hasUserError = true | ||
|
||
makeMessages := func(hasUserError bool) []interface{} { | ||
msgs := []string{"foo error message", "bar error format"} | ||
original := errors.New("orignal error") | ||
|
||
messages := make([]interface{}, 0) | ||
for _, msg := range msgs { | ||
messages = append(messages, msg) | ||
} | ||
|
||
if hasUserError { | ||
messages = append(messages, pipelineErrors.WrapUserError(original)) | ||
} | ||
return messages | ||
} | ||
|
||
tcs := []struct { | ||
description string | ||
messageFormat string | ||
messages []interface{} | ||
expected string | ||
}{{ | ||
description: "error messags with user error", | ||
messageFormat: v1.PipelineRunReasonInvalidGraph.String(), | ||
messages: makeMessages(hasUserError), | ||
expected: "[User error] " + v1.PipelineRunReasonInvalidGraph.String(), | ||
}, { | ||
description: "error messags without user error", | ||
messages: makeMessages(!hasUserError), | ||
messageFormat: v1.PipelineRunReasonInvalidGraph.String(), | ||
expected: v1.PipelineRunReasonInvalidGraph.String(), | ||
}} | ||
for _, tc := range tcs { | ||
{ | ||
messageFormat := pipelineErrors.LabelUserError(tc.messageFormat, tc.messages) | ||
|
||
if messageFormat != tc.expected { | ||
t.Errorf("failure messageFormat expected: %s; but got %s", tc.expected, messageFormat) | ||
} | ||
} | ||
} | ||
} |
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.