mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-25 10:07:43 +01:00
refactor: use lib/errors
to instead of scan/errs
Signed-off-by: He Weiwei <hweiwei@vmware.com>
This commit is contained in:
parent
9c06c79ff4
commit
1bf142c33b
@ -37,7 +37,6 @@ import (
|
||||
"github.com/goharbor/harbor/src/pkg/scan/all"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/dao/scan"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/dao/scanner"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/errs"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/report"
|
||||
v1 "github.com/goharbor/harbor/src/pkg/scan/rest/v1"
|
||||
"github.com/google/uuid"
|
||||
@ -167,12 +166,12 @@ func (bc *basicController) Scan(ctx context.Context, artifact *ar.Artifact, opti
|
||||
|
||||
// In case it does not exist
|
||||
if r == nil {
|
||||
return errs.WithCode(errs.PreconditionFailed, errs.Errorf("no available scanner for project: %d", artifact.ProjectID))
|
||||
return errors.PreconditionFailedError(nil).WithMessage("no available scanner for project: %d", artifact.ProjectID)
|
||||
}
|
||||
|
||||
// Check if it is disabled
|
||||
if r.Disabled {
|
||||
return errs.WithCode(errs.PreconditionFailed, errs.Errorf("scanner %s is disabled", r.Name))
|
||||
return errors.PreconditionFailedError(nil).WithMessage("scanner %s is disabled", r.Name)
|
||||
}
|
||||
|
||||
artifacts, scannable, err := bc.collectScanningArtifacts(ctx, r, artifact)
|
||||
|
@ -1,132 +0,0 @@
|
||||
// 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
|
||||
}
|
@ -1,118 +0,0 @@
|
||||
// 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
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user