Skip to content

Commit

Permalink
remove HasClientData interface facades
Browse files Browse the repository at this point in the history
This is unecessary as it is handled by wrapping
  • Loading branch information
Greg Weber committed Jun 25, 2024
1 parent 3a25a58 commit 6b77778
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 61 deletions.
41 changes: 12 additions & 29 deletions codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ func NewCodedError(err error, code Code) CodedError {
return CodedError{GetCode: code, Err: err}
}

var _ ErrorCode = (*CodedError)(nil) // assert implements interface
var _ HasClientData = (*CodedError)(nil) // assert implements interface
var _ unwrapError = (*CodedError)(nil) // assert implements interface
var _ ErrorCode = (*CodedError)(nil) // assert implements interface
var _ unwrapError = (*CodedError)(nil) // assert implements interface

func (e CodedError) Error() string {
return e.Err.Error()
Expand All @@ -116,17 +115,6 @@ func (e CodedError) Code() Code {
return e.GetCode
}

// GetClientData returns the underlying Err field.
func (e CodedError) GetClientData() interface{} {
if cd, ok := e.Err.(HasClientData); ok {
return cd.GetClientData()
}
if errCode, ok := e.Err.(ErrorCode); ok {
return ClientData(errCode)
}
return e.Err
}

// invalidInputErr gives the code InvalidInputCode.
type invalidInputErr struct{ CodedError }

Expand All @@ -137,9 +125,8 @@ func NewInvalidInputErr(err error) ErrorCode {
return invalidInputErr{NewCodedError(err, InvalidInputCode)}
}

var _ ErrorCode = (*invalidInputErr)(nil) // assert implements interface
var _ HasClientData = (*invalidInputErr)(nil) // assert implements interface
var _ unwrapError = (*invalidInputErr)(nil) // assert implements interface
var _ ErrorCode = (*invalidInputErr)(nil) // assert implements interface
var _ unwrapError = (*invalidInputErr)(nil) // assert implements interface

// badReqeustErr gives the code BadRequestErr.
type BadRequestErr struct{ CodedError }
Expand All @@ -165,9 +152,8 @@ func NewInternalErr(err error) InternalErr {
return InternalErr{internalStackCode(err)}
}

var _ ErrorCode = (*InternalErr)(nil) // assert implements interface
var _ HasClientData = (*InternalErr)(nil) // assert implements interface
var _ unwrapError = (*InternalErr)(nil) // assert implements interface
var _ ErrorCode = (*InternalErr)(nil) // assert implements interface
var _ unwrapError = (*InternalErr)(nil) // assert implements interface

// makeInternalStackCode builds a function for making an an internal error with a stack trace.
func makeInternalStackCode(defaultCode Code) func(error) StackCode {
Expand Down Expand Up @@ -225,9 +211,8 @@ func NewNotFoundErr(err error) NotFoundErr {
return NotFoundErr{NewCodedError(err, NotFoundCode)}
}

var _ ErrorCode = (*NotFoundErr)(nil) // assert implements interface
var _ HasClientData = (*NotFoundErr)(nil) // assert implements interface
var _ unwrapError = (*NotFoundErr)(nil) // assert implements interface
var _ ErrorCode = (*NotFoundErr)(nil) // assert implements interface
var _ unwrapError = (*NotFoundErr)(nil) // assert implements interface

// NotAuthenticatedErr gives the code NotAuthenticatedCode.
type NotAuthenticatedErr struct{ CodedError }
Expand All @@ -239,9 +224,8 @@ func NewNotAuthenticatedErr(err error) NotAuthenticatedErr {
return NotAuthenticatedErr{NewCodedError(err, NotAuthenticatedCode)}
}

var _ ErrorCode = (*NotAuthenticatedErr)(nil) // assert implements interface
var _ HasClientData = (*NotAuthenticatedErr)(nil) // assert implements interface
var _ unwrapError = (*NotAuthenticatedErr)(nil) // assert implements interface
var _ ErrorCode = (*NotAuthenticatedErr)(nil) // assert implements interface
var _ unwrapError = (*NotAuthenticatedErr)(nil) // assert implements interface

// ForbiddenErr gives the code ForbiddenCode.
type ForbiddenErr struct{ CodedError }
Expand All @@ -253,9 +237,8 @@ func NewForbiddenErr(err error) ForbiddenErr {
return ForbiddenErr{NewCodedError(err, ForbiddenCode)}
}

var _ ErrorCode = (*ForbiddenErr)(nil) // assert implements interface
var _ HasClientData = (*ForbiddenErr)(nil) // assert implements interface
var _ unwrapError = (*ForbiddenErr)(nil) // assert implements interface
var _ ErrorCode = (*ForbiddenErr)(nil) // assert implements interface
var _ unwrapError = (*ForbiddenErr)(nil) // assert implements interface

// UnprocessableErr gives the code UnprocessibleCode.
type UnprocessableErr struct{ CodedError }
Expand Down
6 changes: 3 additions & 3 deletions error_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func TestNewInvalidInputErr(t *testing.T) {
err = errcode.NewInvalidInputErr(errors.New("new error"))
AssertCodes(t, err, "input")
ErrorEquals(t, err, "new error")
ClientDataEquals(t, err, errors.New("new error"), "input")
ClientDataEquals(t, err, nil, "input")

err = errcode.NewInvalidInputErr(MinimalError{})
AssertCodes(t, err, "input.testcode")
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestStackTrace(t *testing.T) {
AssertCode(t, wrappedInternalErr, internalCodeStr)
AssertHTTPCode(t, wrappedInternalErr, 500)
ErrorEquals(t, err, "errors stack")
ClientDataEquals(t, wrappedInternalErr, err, internalCodeStr)
ClientDataEquals(t, wrappedInternalErr, nil, internalCodeStr)
// It should use the original stack trace, not the wrapped
AssertStackEquals(t, wrappedInternalErr, errcode.StackTrace(err))
}
Expand All @@ -301,7 +301,7 @@ func TestNewInternalErr(t *testing.T) {
AssertCode(t, err, internalCodeStr)
AssertHTTPCode(t, err, 500)
ErrorEquals(t, err, "new error")
ClientDataEquals(t, err, errors.New("new error"), "internal")
ClientDataEquals(t, err, nil, "internal")

err = errcode.NewInternalErr(MinimalError{})
AssertCode(t, err, internalCodeStr)
Expand Down
12 changes: 0 additions & 12 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func Combine(initial ErrorCode, others ...ErrorCode) MultiErrCode {
}

var _ ErrorCode = (*MultiErrCode)(nil) // assert implements interface
var _ HasClientData = (*MultiErrCode)(nil) // assert implements interface
var _ unwrapError = (*MultiErrCode)(nil) // assert implements interface
var _ errors.ErrorGroup = (*MultiErrCode)(nil) // assert implements interface
var _ fmt.Formatter = (*MultiErrCode)(nil) // assert implements interface
Expand Down Expand Up @@ -90,11 +89,6 @@ func (e MultiErrCode) Unwrap() error {
return e.ErrCode
}

// GetClientData fullfills the HasClientData inteface
func (e MultiErrCode) GetClientData() interface{} {
return ClientData(e.ErrCode)
}

// CodeChain resolves wrapped errors down to the first ErrorCode.
// An error that is an ErrorGroup with multiple codes will have its error codes combined to a MultiErrCode.
// If the given error is not an ErrorCode, a ContextChain will be returned with Top set to the given error.
Expand Down Expand Up @@ -164,13 +158,7 @@ func (err ChainContext) Unwrap() error {
return err.ErrCode
}

// GetClientData satisfies the HasClientData interface
func (err ChainContext) GetClientData() interface{} {
return ClientData(err.ErrCode)
}

var _ ErrorCode = (*ChainContext)(nil)
var _ HasClientData = (*ChainContext)(nil)
var _ unwrapError = (*ChainContext)(nil)

// Format implements the Formatter interface
Expand Down
12 changes: 3 additions & 9 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,9 @@ func (e OpErrCode) Code() Code {
return e.Err.Code()
}

// GetClientData returns the ClientData of the underlying Err.
func (e OpErrCode) GetClientData() interface{} {
return ClientData(e.Err)
}

var _ ErrorCode = (*OpErrCode)(nil) // assert implements interface
var _ HasClientData = (*OpErrCode)(nil) // assert implements interface
var _ HasOperation = (*OpErrCode)(nil) // assert implements interface
var _ unwrapError = (*OpErrCode)(nil) // assert implements interface
var _ ErrorCode = (*OpErrCode)(nil) // assert implements interface
var _ HasOperation = (*OpErrCode)(nil) // assert implements interface
var _ unwrapError = (*OpErrCode)(nil) // assert implements interface

// AddOp is constructed by Op. It allows method chaining with AddTo.
type AddOp func(ErrorCode) OpErrCode
Expand Down
10 changes: 2 additions & 8 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,5 @@ func (e StackCode) Code() Code {
return e.Err.Code()
}

// GetClientData returns the ClientData of the underlying Err.
func (e StackCode) GetClientData() interface{} {
return ClientData(e.Err)
}

var _ ErrorCode = (*StackCode)(nil) // assert implements interface
var _ HasClientData = (*StackCode)(nil) // assert implements interface
var _ unwrapError = (*StackCode)(nil) // assert implements interface
var _ ErrorCode = (*StackCode)(nil) // assert implements interface
var _ unwrapError = (*StackCode)(nil) // assert implements interface

0 comments on commit 6b77778

Please sign in to comment.