Skip to content

Commit

Permalink
don't export structs
Browse files Browse the repository at this point in the history
Also remove StackTrace helper.
This would be better place in the errors package
  • Loading branch information
gregwebs committed Jan 23, 2025
1 parent eb56127 commit 3f11fc6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
6 changes: 3 additions & 3 deletions error_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ type JSONFormat struct {
Others []JSONFormat `json:"others,omitempty"`
}

// OperationClientData gives the results of both the ClientData and Operation functions.
// operationClientData gives the results of both the ClientData and Operation functions.
// The Operation function is applied to the original ErrorCode.
// If that does not return an operation, it is applied to the result of ClientData.
// This function is used by NewJSONFormat to fill JSONFormat.
func OperationClientData(errCode ErrorCode) (string, interface{}) {
func operationClientData(errCode ErrorCode) (string, interface{}) {
op := Operation(errCode)
data := ClientData(errCode)
if op == "" && data != nil {
Expand All @@ -196,7 +196,7 @@ func newJSONFormat(errCode ErrorCode, recur bool) JSONFormat {
}
}

op, data := OperationClientData(errCode)
op, data := operationClientData(errCode)

msg := GetUserMsg(errCode)
if msg == "" {
Expand Down
20 changes: 16 additions & 4 deletions error_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestNewInvalidInputErr(t *testing.T) {
ErrorEquals(t, err, "error")
ClientDataEquals(t, wrappedInternalErr, nil, internalCodeStr, MinimalError{})
// It should use the original stack trace, not the wrapped
AssertStackEquals(t, wrappedInternalErr, errcode.StackTrace(internalErr))
AssertStackEquals(t, wrappedInternalErr, StackTrace(internalErr))

err = errcode.NewInternalErr(InternalChild{})
AssertCode(t, err, internalChildCodeStr)
Expand All @@ -238,7 +238,7 @@ func TestStackTrace(t *testing.T) {
ErrorEquals(t, err, "errors stack")
ClientDataEquals(t, wrappedInternalErr, nil, internalCodeStr)
// It should use the original stack trace, not the wrapped
AssertStackEquals(t, wrappedInternalErr, errcode.StackTrace(err))
AssertStackEquals(t, wrappedInternalErr, StackTrace(err))
}

func TestNewInternalErr(t *testing.T) {
Expand Down Expand Up @@ -480,7 +480,7 @@ func jsonEquals(t *testing.T, errPrefix string, expectedIn interface{}, gotIn in

func OpEquals(t *testing.T, code errcode.ErrorCode, op string) {
t.Helper()
opGot, _ := errcode.OperationClientData(code)
opGot := errcode.Operation(code)
if opGot != op {
t.Errorf("\nOp expected: %#v\n but got: %#v", op, opGot)
}
Expand Down Expand Up @@ -512,8 +512,20 @@ func AssertUserMsg(t *testing.T, v interface{}, msg string) {

func AssertStackEquals(t *testing.T, given errcode.ErrorCode, stExpected stackfmt.StackTrace) {
t.Helper()
stGiven := errcode.StackTrace(given)
stGiven := StackTrace(given)
if stGiven == nil || stExpected == nil || stGiven[0] != stExpected[0] {
t.Errorf("\nStack expected: %#v\n Stack but got: %#v", stExpected[0], stGiven[0])
}
}

// StackTrace retrieves the errors.StackTrace from the error if it is present.
// If there is not StackTrace it will return nil
//
// StackTrace looks to see if the error is a StackTracer or if an Unwrap of the error is a StackTracer.
// It will return the stack trace from the deepest error it can find.
func StackTrace(err error) stackfmt.StackTrace {
if tracer := errors.GetStackTracer(err); tracer != nil {
return tracer.StackTrace()
}
return nil
}
28 changes: 14 additions & 14 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package errcode
// GetOperation is defined, but generally the operation should be retrieved with Operation().
// Operation() will check if a HasOperation interface exists.
// As an alternative to defining this interface
// you can use an existing wrapper (OpErrCode via AddOp) or embedding (EmbedOp) that has already defined it.
// you can use an existing wrapper (AddOp) or embedding (EmbedOp) that has already defined it.
type HasOperation interface {
GetOperation() string
}
Expand Down Expand Up @@ -46,50 +46,50 @@ type OpCode interface {
HasOperation
}

// OpErrCode is an ErrorCode with an Operation field attached.
// opErrCode is an ErrorCode with an Operation field attached.
// This can be conveniently constructed with Op() and AddTo() to record the operation information for the error.
// However, it isn't required to be used, see the HasOperation documentation for alternatives.
type OpErrCode struct {
type opErrCode struct {
ErrorCode
EmbedOp
}

// Unwrap satisfies the errors package Unwrap function
func (e OpErrCode) Unwrap() error {
func (e opErrCode) Unwrap() error {
return e.ErrorCode
}

// Error prefixes the operation to the underlying Err Error.
func (e OpErrCode) Error() string {
func (e opErrCode) Error() string {
return e.Op + ": " + e.ErrorCode.Error()
}

var _ ErrorCode = (*OpErrCode)(nil) // assert implements interface
var _ HasOperation = (*OpErrCode)(nil) // assert implements interface
var _ OpCode = (*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 _ OpCode = (*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
type AddOp func(ErrorCode) OpCode

// AddTo adds the operation from Op to the ErrorCode
func (addOp AddOp) AddTo(err ErrorCode) OpErrCode {
func (addOp AddOp) AddTo(err ErrorCode) OpCode {
return addOp(err)
}

// Op adds an operation to an ErrorCode with AddTo.
// This converts the error to the type OpErrCode.
// This converts the error to the type OpCode.
//
// op := errcode.Op("path.move.x")
// if start < obstable && obstacle < end {
// return op.AddTo(PathBlocked{start, end, obstacle})
// }
func Op(operation string) AddOp {
return func(err ErrorCode) OpErrCode {
return func(err ErrorCode) OpCode {
if err == nil {
panic("Op errorcode is nil")
}
return OpErrCode{
return opErrCode{
ErrorCode: err,
EmbedOp: EmbedOp{Op: operation},
}
Expand Down
12 changes: 0 additions & 12 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ import (
"github.com/gregwebs/stackfmt"
)

// StackTrace retrieves the errors.StackTrace from the error if it is present.
// If there is not StackTrace it will return nil
//
// StackTrace looks to see if the error is a StackTracer or if an Unwrap of the error is a StackTracer.
// It will return the stack trace from the deepest error it can find.
func StackTrace(err error) stackfmt.StackTrace {
if tracer := errors.GetStackTracer(err); tracer != nil {
return tracer.StackTrace()
}
return nil
}

// StackCode is an [ErrorCode] with stack trace information attached.
// This may be used as a convenience to record the strack trace information for the error.
// Stack traces are provided by [NewInternalErr].
Expand Down
20 changes: 10 additions & 10 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,32 @@ func (e EmbedUserMsg) GetUserMsg() string {
return e.Msg
}

// UserMsgErrCode is an ErrorCode with a Msg field attached.
// userMsgErrCode is an ErrorCode with a Msg field attached.
// This can be conveniently constructed with NewUserMsg and AddTo or WithUserMsg
// see the HasUserMsg documentation for alternatives.
type UserMsgErrCode struct {
type userMsgErrCode struct {
ErrorCode
Msg string
}

// Unwrap satisfies the errors package Unwrap function
func (e UserMsgErrCode) Unwrap() error {
func (e userMsgErrCode) Unwrap() error {
return e.ErrorCode
}

// Error prefixes the user message to the underlying Err Error.
func (e UserMsgErrCode) Error() string {
func (e userMsgErrCode) Error() string {
return e.Msg + ": " + e.ErrorCode.Error()
}

// GetUserMsg satisfies the [HasUserMsg] interface.
func (e UserMsgErrCode) GetUserMsg() string {
func (e userMsgErrCode) GetUserMsg() string {
return e.Msg
}

var _ ErrorCode = (*UserMsgErrCode)(nil) // assert implements interface
var _ HasUserMsg = (*UserMsgErrCode)(nil) // assert implements interface
var _ unwrapError = (*UserMsgErrCode)(nil) // assert implements interface
var _ ErrorCode = (*userMsgErrCode)(nil) // assert implements interface
var _ HasUserMsg = (*userMsgErrCode)(nil) // assert implements interface
var _ unwrapError = (*userMsgErrCode)(nil) // assert implements interface

// AddUserMsg is constructed by UserMsg. It allows method chaining with AddTo.
type AddUserMsg func(ErrorCode) UserCode
Expand All @@ -94,11 +94,11 @@ func UserMsg(msg string) AddUserMsg {
}
}

// WithUserMsg creates a UserMsgErrCode
// WithUserMsg creates a UserCode
// Panics if msg is empty or err is nil.
func WithUserMsg(msg string, err ErrorCode) UserCode {
if err == nil {
return nil
}
return UserMsgErrCode{Msg: msg, ErrorCode: err}
return userMsgErrCode{Msg: msg, ErrorCode: err}
}

0 comments on commit 3f11fc6

Please sign in to comment.