Skip to content

Commit

Permalink
add stackerr for stack traces
Browse files Browse the repository at this point in the history
  • Loading branch information
cszczepaniak committed Jun 4, 2023
1 parent 1baf2ee commit a143b36
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/stackerr/stackerr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package stackerr

import (
"fmt"
"runtime/debug"
)

type StackErr struct {
cause error
stack []byte
}

func NewStackErr(cause error) error {
return StackErr{
cause: cause,
stack: debug.Stack(),
}
}

func NewStackErrf(cause error, f string, args ...any) error {
msg := fmt.Sprintf(f, args...)
cause = fmt.Errorf(msg+": %w", cause)
return NewStackErr(cause)
}

func (se StackErr) Error() string {
return se.cause.Error()
}

func (se StackErr) Unwrap() error {
return se.cause
}

func (se StackErr) Stack() []byte {
return se.stack
}

0 comments on commit a143b36

Please sign in to comment.