forked from goharbor/harbor
-
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.
Merge pull request goharbor#9673 from steven-zou/fix/issue_#9668_stat…
…us_conflicts return more clear error message for scan related API
- Loading branch information
Showing
5 changed files
with
294 additions
and
7 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,132 @@ | ||
// Copyright Project Harbor 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 errs | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
// Common error code | ||
Common uint16 = 10000 | ||
// Conflict error code | ||
Conflict uint16 = 10409 | ||
// PreconditionFailed error code | ||
PreconditionFailed uint16 = 10412 | ||
) | ||
|
||
// codeTexts behaviors as a hash map to look for the text for the given error code. | ||
func codeTexts(code uint16) string { | ||
switch code { | ||
case Common: | ||
return "common" | ||
case Conflict: | ||
return "conflict" | ||
case PreconditionFailed: | ||
return "Precondition failed" | ||
default: | ||
return "unknown" | ||
} | ||
} | ||
|
||
// Error with code | ||
type Error struct { | ||
// Code of error | ||
Code uint16 `json:"code"` | ||
// Code represented by meaningful text | ||
TextCode string `json:"text_code"` | ||
// Message of error | ||
Message string `json:"message"` | ||
// Cause for error | ||
Cause error `json:"cause"` | ||
} | ||
|
||
// Error message | ||
func (e *Error) Error() string { | ||
emsg := fmt.Sprintf("error: %d(%s) : %s", e.Code, e.TextCode, e.Message) | ||
if e.Cause != nil { | ||
emsg = fmt.Sprintf("%s : cause: %s", emsg, e.Cause.Error()) | ||
} | ||
|
||
return emsg | ||
} | ||
|
||
// String outputs the error with well-formatted string. | ||
func (e *Error) String() string { | ||
bytes, err := json.Marshal(e) | ||
if err != nil { | ||
// Fallback to normal string | ||
return e.Error() | ||
} | ||
|
||
return string(bytes) | ||
} | ||
|
||
// New common error. | ||
func New(message string) error { | ||
return &Error{ | ||
Code: Common, | ||
TextCode: codeTexts(Common), | ||
Message: message, | ||
} | ||
} | ||
|
||
// Wrap error with message. | ||
func Wrap(err error, message string) error { | ||
return &Error{ | ||
Code: Common, | ||
TextCode: codeTexts(Common), | ||
Message: message, | ||
Cause: err, | ||
} | ||
} | ||
|
||
// Errorf new a message with the specified format and arguments | ||
func Errorf(format string, args ...interface{}) error { | ||
return &Error{ | ||
Code: Common, | ||
TextCode: codeTexts(Common), | ||
Message: fmt.Sprintf(format, args...), | ||
} | ||
} | ||
|
||
// WithCode sets specified code for the error | ||
func WithCode(code uint16, err error) error { | ||
if err == nil { | ||
return err | ||
} | ||
|
||
e, ok := err.(*Error) | ||
if !ok { | ||
return err | ||
} | ||
|
||
e.Code = code | ||
e.TextCode = codeTexts(code) | ||
|
||
return e | ||
} | ||
|
||
// AsError checks if the given error has the given code | ||
func AsError(err error, code uint16) bool { | ||
if err == nil { | ||
return false | ||
} | ||
|
||
e, ok := err.(*Error) | ||
|
||
return ok && e.Code == code | ||
} |
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,118 @@ | ||
// Copyright Project Harbor 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 errs | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
// ErrorSuite is a test suite for testing Error. | ||
type ErrorSuite struct { | ||
suite.Suite | ||
} | ||
|
||
// TestError is the entry point of ErrorSuite. | ||
func TestError(t *testing.T) { | ||
suite.Run(t, &ErrorSuite{}) | ||
} | ||
|
||
// TestErrorNew ... | ||
func (suite *ErrorSuite) TestErrorNew() { | ||
err := New("error-testing") | ||
require.Error(suite.T(), err) | ||
|
||
suite.Equal(true, AsError(err, Common)) | ||
suite.Condition(func() (success bool) { | ||
return -1 != strings.Index(err.Error(), "error-testing") | ||
}) | ||
suite.Condition(func() (success bool) { | ||
success = strings.Contains(err.Error(), codeTexts(Common)) | ||
return | ||
}) | ||
} | ||
|
||
// TestErrorWrap ... | ||
func (suite *ErrorSuite) TestErrorWrap() { | ||
err := errors.New("error-stack") | ||
e := Wrap(err, "wrap-message") | ||
require.Error(suite.T(), e) | ||
|
||
suite.Equal(true, AsError(e, Common)) | ||
suite.Condition(func() (success bool) { | ||
success = -1 != strings.Index(e.Error(), "error-stack") | ||
return | ||
}) | ||
suite.Condition(func() (success bool) { | ||
success = -1 != strings.Index(e.Error(), "wrap-message") | ||
return | ||
}) | ||
suite.Condition(func() (success bool) { | ||
success = strings.Contains(e.Error(), codeTexts(Common)) | ||
return | ||
}) | ||
} | ||
|
||
// TestErrorErrorf ... | ||
func (suite *ErrorSuite) TestErrorErrorf() { | ||
err := Errorf("a=%d", 1000) | ||
require.Error(suite.T(), err) | ||
|
||
suite.Equal(true, AsError(err, Common)) | ||
suite.Condition(func() (success bool) { | ||
success = strings.Contains(err.Error(), "a=1000") | ||
return | ||
}) | ||
suite.Condition(func() (success bool) { | ||
success = strings.Contains(err.Error(), codeTexts(Common)) | ||
return | ||
}) | ||
} | ||
|
||
// TestErrorString ... | ||
func (suite *ErrorSuite) TestErrorString() { | ||
err := New("well-formatted-error") | ||
require.Error(suite.T(), err) | ||
|
||
str := err.(*Error).String() | ||
require.Condition(suite.T(), func() (success bool) { | ||
success = len(str) > 0 | ||
return | ||
}) | ||
|
||
e := &Error{} | ||
er := json.Unmarshal([]byte(str), e) | ||
suite.NoError(er) | ||
suite.Equal(e.Message, "well-formatted-error") | ||
} | ||
|
||
// TestErrorWithCode ... | ||
func (suite *ErrorSuite) TestErrorWithCode() { | ||
err := New("error-with-code") | ||
require.Error(suite.T(), err) | ||
|
||
err = WithCode(Conflict, err) | ||
require.Error(suite.T(), err) | ||
suite.Equal(true, AsError(err, Conflict)) | ||
suite.Condition(func() (success bool) { | ||
success = strings.Contains(err.Error(), codeTexts(Conflict)) | ||
return | ||
}) | ||
} |
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