mirror of
https://github.com/goharbor/harbor
synced 2024-09-21 08:22:19 +00:00
update internal common error (#10994)
1, New support construct with string or err 2, Add Wrap/Errorf method Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
parent
1df879052b
commit
9e4fdc571a
|
@ -32,6 +32,12 @@ func (e *Error) WithCode(code string) *Error {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithCause ...
|
||||||
|
func (e *Error) WithCause(err error) *Error {
|
||||||
|
e.Cause = err
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
// Unwrap ...
|
// Unwrap ...
|
||||||
func (e *Error) Unwrap() error { return e.Cause }
|
func (e *Error) Unwrap() error { return e.Cause }
|
||||||
|
|
||||||
|
@ -101,18 +107,52 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// New ...
|
// New ...
|
||||||
func New(err error) *Error {
|
func New(in interface{}) *Error {
|
||||||
e := &Error{}
|
var err error
|
||||||
if err != nil {
|
switch in := in.(type) {
|
||||||
e.Cause = err
|
case error:
|
||||||
e.Message = err.Error()
|
err = in
|
||||||
if ee, ok := err.(*Error); ok {
|
case *Error:
|
||||||
e.Cause = ee
|
err = in.Cause
|
||||||
}
|
default:
|
||||||
|
err = fmt.Errorf("%v", in)
|
||||||
|
}
|
||||||
|
return &Error{
|
||||||
|
Cause: err,
|
||||||
|
Message: err.Error(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap ...
|
||||||
|
func Wrap(err error, message string) *Error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
e := &Error{
|
||||||
|
Cause: err,
|
||||||
|
Message: message,
|
||||||
}
|
}
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrapf ...
|
||||||
|
func Wrapf(err error, format string, args ...interface{}) *Error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
e := &Error{
|
||||||
|
Cause: err,
|
||||||
|
}
|
||||||
|
return e.WithMessage(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorf ...
|
||||||
|
func Errorf(format string, args ...interface{}) *Error {
|
||||||
|
return &Error{
|
||||||
|
Message: fmt.Sprintf(format, args...),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NotFoundError is error for the case of object not found
|
// NotFoundError is error for the case of object not found
|
||||||
func NotFoundError(err error) *Error {
|
func NotFoundError(err error) *Error {
|
||||||
return New(err).WithCode(NotFoundCode).WithMessage("resource not found")
|
return New(err).WithCode(NotFoundCode).WithMessage("resource not found")
|
||||||
|
@ -153,6 +193,18 @@ func UnknownError(err error) *Error {
|
||||||
return New(err).WithCode(GeneralCode).WithMessage("unknown")
|
return New(err).WithCode(GeneralCode).WithMessage("unknown")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cause gets the root error
|
||||||
|
func Cause(err error) error {
|
||||||
|
for err != nil {
|
||||||
|
cause, ok := err.(*Error)
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err = cause.Cause
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// IsErr checks whether the err chain contains error matches the code
|
// IsErr checks whether the err chain contains error matches the code
|
||||||
func IsErr(err error, code string) bool {
|
func IsErr(err error, code string) bool {
|
||||||
var e *Error
|
var e *Error
|
||||||
|
|
Loading…
Reference in New Issue
Block a user