Skip to content
Snippets Groups Projects
Commit d7a4bf73 authored by Jan Semmelink's avatar Jan Semmelink
Browse files

Renamed Error to CustomError and created Error() func to be similar to Wrap() without formatting

parent a42306d9
Branches
Tags
No related merge requests found
......@@ -7,34 +7,33 @@ import (
"strings"
)
//Error implements the following interfaces:
//CustomError implements the following interfaces:
// error
// github.com/pkg/errors: Cause
type Error struct {
type CustomError struct {
message string
caller Caller
//stack []CallerInfo
cause error
}
//implement interface error:
func (err Error) Error() string {
func (err CustomError) Error() string {
return err.Formatted(FormattingOptions{Causes: true})
}
//implement github.com/pkg/errors: Cause
func (err Error) Cause() error {
func (err CustomError) Cause() error {
return err.cause
}
func (err Error) Description() Description {
func (err CustomError) Description() Description {
info := err.caller.Info()
desc := &Description{
Message: err.message,
Source: &info,
}
if err.cause != nil {
causeWithStack, ok := err.cause.(*Error)
causeWithStack, ok := err.cause.(*CustomError)
if !ok {
//external cause without our stack
//if github.com/pkg/errors, we can still get caller reference
......@@ -48,7 +47,7 @@ func (err Error) Description() Description {
return *desc
}
func (err Error) Format(s fmt.State, v rune) {
func (err CustomError) Format(s fmt.State, v rune) {
s.Write([]byte(
err.Formatted(
FormattingOptions{
......@@ -66,7 +65,7 @@ type FormattingOptions struct {
Source bool
}
func (err Error) Formatted(opts FormattingOptions) string {
func (err CustomError) Formatted(opts FormattingOptions) string {
//start with this error
thisError := ""
if opts.Source {
......@@ -93,7 +92,7 @@ func (err Error) Formatted(opts FormattingOptions) string {
sep += " "
}
if causeWithStack, ok := err.cause.(*Error); ok {
if causeWithStack, ok := err.cause.(*CustomError); ok {
return thisError + sep + causeWithStack.Formatted(opts)
}
......
......@@ -265,13 +265,13 @@ func show(t *testing.T, title string, err error) {
t.Logf("------------------------------------------------------------------------------------")
t.Logf("err.Error(): %s", err.Error())
t.Logf("Printf(%%s): %s", err)
if _, ok := err.(*uf_errors.Error); ok { //compact only supported on our own lib:
if _, ok := err.(*uf_errors.CustomError); ok { //compact only supported on our own lib:
t.Logf("Printf(%%c): %c", err)
t.Logf("Printf(%%+c): %+c", err)
}
t.Logf("Printf(%%v): %v", err)
t.Logf("Printf(%%+v): %+v", err)
if e, ok := err.(*uf_errors.Error); ok { //description only supported on our own lib:
if e, ok := err.(*uf_errors.CustomError); ok { //description only supported on our own lib:
desc := e.Description()
jsonDesc, _ := json.Marshal(desc)
t.Logf("JSON: %s", string(jsonDesc))
......
......@@ -13,30 +13,27 @@ type ErrorWithCause interface {
}
func New(message string) error {
err := &Error{
err := &CustomError{
message: message,
caller: GetCaller(2),
//stack: Stack(3),
cause: nil,
}
return err
}
func NError(message string) error {
err := &Error{
func Error(message string) error {
err := &CustomError{
message: message,
caller: GetCaller(2),
//stack: Stack(3),
cause: nil,
}
return err
}
func Errorf(format string, args ...interface{}) error {
err := &Error{
err := &CustomError{
message: fmt.Sprintf(format, args...),
caller: GetCaller(2),
//stack: Stack(3),
cause: nil,
}
return err
......@@ -47,7 +44,7 @@ func Wrapf(err error, format string, args ...interface{}) error {
return nil
}
wrappedErr := &Error{
wrappedErr := &CustomError{
message: fmt.Sprintf(format, args...),
caller: GetCaller(2),
cause: err,
......@@ -61,7 +58,7 @@ func Wrap(err error, msg string) error {
return nil
}
wrappedErr := &Error{
wrappedErr := &CustomError{
message: msg,
caller: GetCaller(2),
cause: err,
......
......@@ -14,7 +14,7 @@ func TestErrorFormatting(t *testing.T) {
e1 := errors.Errorf("you have problem in your SQL near xxx")
e2 := errors.Wrapf(e1, "query failed")
e3 := errors.Wrapf(e2, "failed to find account")
e4 := errors.Wrapf(e3, "login failed").(*errors.Error)
e4 := errors.Wrapf(e3, "login failed").(*errors.CustomError)
//we can log the error in different ways:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment