Skip to content

Commit

Permalink
Merge pull request #4 from RyougiNevermore/master
Browse files Browse the repository at this point in the history
update depth
  • Loading branch information
RyougiNevermore committed Aug 5, 2020
2 parents 177737e + 99f31ca commit 625c4a9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Errors interface {
```go
e1 := io.EOF
e2 := errors.With(e1, "error2")
e3 := errors.WithF(e2, "%s", "error3")
e3 := errors.Withf(e2, "%s", "error3")

if errors.Contains(e3, e2) {
// TODO ..
Expand Down
39 changes: 24 additions & 15 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func New(message string) error {
}
}

func ErrorF(format string, args ...interface{}) error {
func Errorf(format string, args ...interface{}) error {
return &errorUp{
msg: fmt.Sprintf(format, args...),
cause: nil,
Expand All @@ -23,6 +23,24 @@ func ErrorF(format string, args ...interface{}) error {
}
}

func NewWithDepth(depth int, skip int, message string) error {
return &errorUp{
msg: message,
cause: nil,
pcs: callersByAssigned(depth, skip),
occurred: timeNow(),
}
}

func ErrorWithDepthf(depth int, skip int, format string, args ...interface{}) error {
return &errorUp{
msg: fmt.Sprintf(format, args...),
cause: nil,
pcs: callersByAssigned(depth, skip),
occurred: timeNow(),
}
}

func With(cause error, message string) error {
if cause == nil {
return nil
Expand All @@ -35,7 +53,7 @@ func With(cause error, message string) error {
}
}

func WithF(cause error, format string, args ...interface{}) error {
func Withf(cause error, format string, args ...interface{}) error {
if cause == nil {
return nil
}
Expand All @@ -47,26 +65,26 @@ func WithF(cause error, format string, args ...interface{}) error {
}
}

func WithDepth(depth int, cause error, message string) error {
func WithDepth(depth int, skip int, cause error, message string) error {
if cause == nil {
return nil
}
return &errorUp{
msg: message,
cause: cause,
pcs: callersByAssigned(depth, 3),
pcs: callersByAssigned(depth, skip),
occurred: timeNow(),
}
}

func WithDepthF(depth int, cause error, format string, args ...interface{}) error {
func WithDepthf(depth int, skip int, cause error, format string, args ...interface{}) error {
if cause == nil {
return nil
}
return &errorUp{
msg: fmt.Sprintf(format, args...),
cause: cause,
pcs: callersByAssigned(depth, 3),
pcs: callersByAssigned(depth, skip),
occurred: timeNow(),
}
}
Expand All @@ -80,15 +98,6 @@ func Wrap(e error) error {
}
}

func NewByAssigned(depth int, skip int, message string) error {
return &errorUp{
msg: message,
cause: nil,
pcs: callersByAssigned(depth, skip),
occurred: timeNow(),
}
}

type errorUp struct {
msg string
cause error
Expand Down
18 changes: 16 additions & 2 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,24 @@ import (

func TestErrorF(t *testing.T) {
e1 := errors.New("error 1")
e2 := errors.WithF(e1, "error %d", 2)
e2 := errors.Withf(e1, "error %d", 2)
fmt.Println(fmt.Sprintf("%+v", e2))

e3 := errors.WithDepth(2, e2, "error 3")
e3 := errors.WithDepth(2, 3, e2, "error 3")
fmt.Println(fmt.Sprintf("%-v", e3))

}

func wrap(msg string) error {
return errors.NewWithDepth(1, 4, msg)
}

func Test_Wrap(t *testing.T) {

e1 := wrap("1")
e2 := errors.With(e1, "error 2")
e3 := errors.With(e2, "error 3")

fmt.Println(fmt.Sprintf("%+v", e3))

}
37 changes: 19 additions & 18 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"runtime"
"time"
)

type Format func(s fmt.State, verb rune, e Errors)
Expand All @@ -20,11 +21,11 @@ func DefaultFormatFn(s fmt.State, verb rune, e Errors) {
_, _ = io.WriteString(s, "unknown")
} else {
file, line := fn.FileLine(pc)
home, filename := fileName(file)
_, filename := fileName(file)
if i == 0 {
_, _ = fmt.Fprintf(s, "\t[T] %s\n\t[F] %s\n\t[H] %s\n\t[F] %s:%d \n", e.OccurTime().String(), fn.Name(), home, filename, line)
_, _ = fmt.Fprintf(s, "\t[T] %s\n\t[F] %s\n\t[F] %s:%d \n", e.OccurTime().Format(time.RFC3339), fn.Name(), filename, line)
} else {
_, _ = fmt.Fprintf(s, "\t[F] %s\n\t[H] %s\n\t[F] %s:%d \n", fn.Name(), home, filename, line)
_, _ = fmt.Fprintf(s, "\t[F] %s\n\t[F] %s:%d \n", fn.Name(), filename, line)
}
}
}
Expand All @@ -38,18 +39,18 @@ func DefaultFormatFn(s fmt.State, verb rune, e Errors) {
}
case s.Flag('-'):
_, _ = io.WriteString(s, "{")
_, _ = fmt.Fprintf(s, `"msg":"%s", "occurTime":"%s", "stack":[`, e.Error(), e.OccurTime())
_, _ = fmt.Fprintf(s, `"msg":"%s", "occurTime":"%s", "stack":[`, e.Error(), e.OccurTime().Format(time.RFC3339))
for i, pc := range e.PCS() {
if i > 0 {
_, _ = io.WriteString(s, ",")
}
fn := runtime.FuncForPC(pc)
if fn == nil {
_, _ = fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, "unknown", "unknown", "unknown", 0)
_, _ = fmt.Fprintf(s, `{"fn":"%s", "file":"%s", "line":%d}`, "unknown", "unknown", 0)
} else {
file, line := fn.FileLine(pc)
home, filename := fileName(file)
_, _ = fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, fn.Name(), home, filename, line)
_, filename := fileName(file)
_, _ = fmt.Fprintf(s, `{"fn":"%s", "file":"%s", "line":%d}`, fn.Name(), filename, line)
}
}
_, _ = io.WriteString(s, "]")
Expand All @@ -75,35 +76,35 @@ func JsonFormatFn(s fmt.State, verb rune, e Errors) {
case 'v':
switch {
case s.Flag('+'):
io.WriteString(s, "{")
fmt.Fprintf(s, `"msg":"%s", "occurTime":"%s", "stack":[`, e.Error(), e.OccurTime())
_, _ = io.WriteString(s, "{")
_, _ = fmt.Fprintf(s, `"msg":"%s", "occurTime":"%s", "stack":[`, e.Error(), e.OccurTime().Format(time.RFC3339))
for i, pc := range e.PCS() {
if i > 0 {
io.WriteString(s, ",")
_, _ = io.WriteString(s, ",")
}
fn := runtime.FuncForPC(pc)
if fn == nil {
fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, "unknown", "unknown", "unknown", 0)
_, _ = fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, "unknown", "unknown", "unknown", 0)
} else {
file, line := fn.FileLine(pc)
home, filename := fileName(file)
fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, fn.Name(), home, filename, line)
_, _ = fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, fn.Name(), home, filename, line)
}
}
io.WriteString(s, "]")
_, _ = io.WriteString(s, "]")
if e.Cause() != nil {
io.WriteString(s, ",")
_, _ = io.WriteString(s, ",")
hasCause, ok := e.Cause().(Errors)
if !ok {
fmt.Fprintf(s, `"cause":{"msg":"%s"}`, e.Cause().Error())
_, _ = fmt.Fprintf(s, `"cause":{"msg":"%s"}`, e.Cause().Error())
} else {
io.WriteString(s, `"cause":`)
_, _ = io.WriteString(s, `"cause":`)
hasCause.Format(s, verb)
}
}
io.WriteString(s, "}")
_, _ = io.WriteString(s, "}")
default:
fmt.Fprintf(s, "%s", e.Error())
_, _ = fmt.Fprintf(s, "%s", e.Error())
}
}
}
2 changes: 1 addition & 1 deletion stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func fileName(src string) (goPath string, file string) {
}

func timeNow() time.Time {
return time.Now().In(_cfg.loc)
return time.Now()
}

func callers() []uintptr {
Expand Down

0 comments on commit 625c4a9

Please sign in to comment.