Skip to content

Commit

Permalink
client: Unify errors interface to get details
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
  • Loading branch information
smallhive committed May 17, 2023
1 parent d1e2de3 commit 4ef4ffe
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 31 deletions.
75 changes: 73 additions & 2 deletions client/status/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ var (
ErrNodeUnderMaintenance NodeUnderMaintenance
)

// APIErrorDetails gives unified method to get details and determine what happened from certain error.
type APIErrorDetails interface {
Code() status.Code
Message() string
IterateDetails(f func(*status.Detail) bool)
}

// ServerInternal describes failure statuses related to internal server errors.
// Instances provide [Status], [StatusV2] and error interfaces.
//
Expand All @@ -51,6 +58,20 @@ func (x ServerInternal) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x ServerInternal) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x ServerInternal) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// implements local interface defined in FromStatusV2 func.
func (x *ServerInternal) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand All @@ -76,7 +97,7 @@ func (x *ServerInternal) SetMessage(msg string) {

// Message returns message describing internal server error.
//
// Message should be used for debug purposes only. By default, it is empty.
// Implements [APIErrorDetails].
func (x ServerInternal) Message() string {
return x.v2.Message()
}
Expand Down Expand Up @@ -109,6 +130,27 @@ func (x WrongMagicNumber) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x WrongMagicNumber) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x WrongMagicNumber) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// Message returns message describing internal server error.
//
// Implements [APIErrorDetails].
func (x WrongMagicNumber) Message() string {
return x.v2.Message()
}

// implements local interface defined in FromStatusV2 func.
func (x *WrongMagicNumber) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand Down Expand Up @@ -194,6 +236,20 @@ func (x SignatureVerification) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x SignatureVerification) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x SignatureVerification) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// implements local interface defined in FromStatusV2 func.
func (x *SignatureVerification) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand Down Expand Up @@ -228,6 +284,7 @@ func (x *SignatureVerification) SetMessage(v string) {
// Message should be used for debug purposes only.
//
// See also SetMessage.
// Implements [APIErrorDetails].
func (x SignatureVerification) Message() string {
return x.v2.Message()
}
Expand Down Expand Up @@ -263,6 +320,20 @@ func (x NodeUnderMaintenance) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x NodeUnderMaintenance) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x NodeUnderMaintenance) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

func (x *NodeUnderMaintenance) fromStatusV2(st *status.Status) {
x.v2 = *st
}
Expand Down Expand Up @@ -292,9 +363,9 @@ func (x *NodeUnderMaintenance) SetMessage(v string) {
}

// Message returns status message. Zero status returns empty message.
// Message should be used for debug purposes only.
//
// See also SetMessage.
// Implements [APIErrorDetails].
func (x NodeUnderMaintenance) Message() string {
return x.v2.Message()
}
42 changes: 42 additions & 0 deletions client/status/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ func (x ContainerNotFound) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x ContainerNotFound) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x ContainerNotFound) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// Message returns message describing internal server error.
//
// Implements [APIErrorDetails].
func (x ContainerNotFound) Message() string {
return x.v2.Message()
}

// implements local interface defined in FromStatusV2 func.
func (x *ContainerNotFound) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand Down Expand Up @@ -94,6 +115,27 @@ func (x EACLNotFound) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x EACLNotFound) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x EACLNotFound) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// Message returns message describing internal server error.
//
// Implements [APIErrorDetails].
func (x EACLNotFound) Message() string {
return x.v2.Message()
}

// implements local interface defined in FromStatusV2 func.
func (x *EACLNotFound) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand Down
126 changes: 126 additions & 0 deletions client/status/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ func (x ObjectLocked) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x ObjectLocked) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x ObjectLocked) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// Message returns message describing internal server error.
//
// Implements [APIErrorDetails].
func (x ObjectLocked) Message() string {
return x.v2.Message()
}

// implements local interface defined in FromStatusV2 func.
func (x *ObjectLocked) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand Down Expand Up @@ -105,6 +126,27 @@ func (x LockNonRegularObject) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x LockNonRegularObject) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x LockNonRegularObject) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// Message returns message describing internal server error.
//
// Implements [APIErrorDetails].
func (x LockNonRegularObject) Message() string {
return x.v2.Message()
}

// implements local interface defined in FromStatusV2 func.
func (x *LockNonRegularObject) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand Down Expand Up @@ -152,6 +194,27 @@ func (x ObjectAccessDenied) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x ObjectAccessDenied) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x ObjectAccessDenied) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// Message returns message describing internal server error.
//
// Implements [APIErrorDetails].
func (x ObjectAccessDenied) Message() string {
return x.v2.Message()
}

// implements local interface defined in FromStatusV2 func.
func (x *ObjectAccessDenied) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand Down Expand Up @@ -210,6 +273,27 @@ func (x ObjectNotFound) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x ObjectNotFound) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x ObjectNotFound) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// Message returns message describing internal server error.
//
// Implements [APIErrorDetails].
func (x ObjectNotFound) Message() string {
return x.v2.Message()
}

// implements local interface defined in FromStatusV2 func.
func (x *ObjectNotFound) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand Down Expand Up @@ -257,6 +341,27 @@ func (x ObjectAlreadyRemoved) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x ObjectAlreadyRemoved) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x ObjectAlreadyRemoved) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// Message returns message describing internal server error.
//
// Implements [APIErrorDetails].
func (x ObjectAlreadyRemoved) Message() string {
return x.v2.Message()
}

// implements local interface defined in FromStatusV2 func.
func (x *ObjectAlreadyRemoved) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand Down Expand Up @@ -305,6 +410,27 @@ func (x ObjectOutOfRange) Is(target error) bool {
}
}

// Code returns code of the error.
//
// Implements [APIErrorDetails].
func (x ObjectOutOfRange) Code() status.Code {
return x.v2.Code()
}

// IterateDetails allows to process each detail in the error.
//
// Implements [APIErrorDetails].
func (x ObjectOutOfRange) IterateDetails(f func(*status.Detail) bool) {
x.v2.IterateDetails(f)
}

// Message returns message describing internal server error.
//
// Implements [APIErrorDetails].
func (x ObjectOutOfRange) Message() string {
return x.v2.Message()
}

// implements local interface defined in FromStatusV2 func.
func (x *ObjectOutOfRange) fromStatusV2(st *status.Status) {
x.v2 = *st
Expand Down
Loading

0 comments on commit 4ef4ffe

Please sign in to comment.