diff --git a/error_code.go b/error_code.go index f1ccefd..5ccddea 100644 --- a/error_code.go +++ b/error_code.go @@ -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 { @@ -196,7 +196,7 @@ func newJSONFormat(errCode ErrorCode, recur bool) JSONFormat { } } - op, data := OperationClientData(errCode) + op, data := operationClientData(errCode) msg := GetUserMsg(errCode) if msg == "" { diff --git a/error_code_test.go b/error_code_test.go index 6e001d1..045fde7 100644 --- a/error_code_test.go +++ b/error_code_test.go @@ -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) @@ -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) { @@ -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) } @@ -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 +} diff --git a/operation.go b/operation.go index 018d771..acedf93 100644 --- a/operation.go +++ b/operation.go @@ -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 } @@ -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}, } diff --git a/stack.go b/stack.go index 864f4c2..63e75be 100644 --- a/stack.go +++ b/stack.go @@ -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]. diff --git a/user.go b/user.go index e8cd154..aab5220 100644 --- a/user.go +++ b/user.go @@ -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 @@ -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} }