Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/jbowes/vice

go 1.12

require golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
require (
github.com/pkg/errors v0.9.1
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
21 changes: 5 additions & 16 deletions skip/error.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package skip

import (
"fmt"

"golang.org/x/xerrors"
)
import "fmt"

// New returns an error that formats as the given text, and implements the
// behaviour described by the given Vice. The argument skip is the number of
Expand Down Expand Up @@ -97,7 +93,7 @@ func Sealf(err error, v Vice, skip uint, format string, a ...interface{}) error
}

func sealed(serr *sealError, v Vice, skip uint) error {
serr.frame = xerrors.Caller(int(2 + skip))
serr.frame = caller(int(2 + skip))
switch v {
case Timeout:
return &timeoutSeal{*serr}
Expand Down Expand Up @@ -129,7 +125,7 @@ func sealed(serr *sealError, v Vice, skip uint) error {
}

func wrapped(werr *wrapError, v Vice, skip uint) error {
werr.frame = xerrors.Caller(int(2 + skip))
werr.frame = caller(int(2 + skip))
switch v {
case Timeout:
return &timeoutWrap{*werr}
Expand Down Expand Up @@ -163,17 +159,10 @@ func wrapped(werr *wrapError, v Vice, skip uint) error {
type sealError struct {
msg string
err error
frame xerrors.Frame
frame frame
}

func (e *sealError) Error() string { return fmt.Sprint(e) }
func (e *sealError) Format(s fmt.State, v rune) { xerrors.FormatError(e, s, v) }

func (e *sealError) FormatError(p xerrors.Printer) (next error) {
p.Print(e.msg)
e.frame.Format(p)
return e.err
}
func (e *sealError) Error() string { return fmt.Sprint(e) }

type wrapError struct {
sealError
Expand Down
26 changes: 26 additions & 0 deletions skip/frame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// frame reimplements stack frame handling in a way that is compatible with
// multiple error ecosystems.

package skip

import "runtime"

type frame [3]uintptr

func caller(skip int) frame {
var f frame
runtime.Callers(skip+1, f[:])
return f
}

func (f frame) detail() (string, string, int) {
frs := runtime.CallersFrames(f[:])

if _, ok := frs.Next(); ok {
if fr, ok := frs.Next(); ok {
return fr.Function, fr.File, fr.Line
}
}

return "", "", 0
}
34 changes: 34 additions & 0 deletions skip/xerrors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file holds xerrors specific logic. xerrors was meant to be the
// behaviour that would land in the standard library in go 1.13. Not all
// of it did, but this still remains the default for vice. Still, it is kept
// in its own file to make it easier to remove or put behind a build tag.

package skip

import (
"fmt"

"golang.org/x/xerrors"
)

func (e *sealError) Format(s fmt.State, v rune) { xerrors.FormatError(e, s, v) }

func (e *sealError) FormatError(p xerrors.Printer) (next error) {
p.Print(e.msg)
e.frame.Format(p)
return e.err
}

func (f frame) Format(p xerrors.Printer) {
if !p.Detail() {
return
}

fn, file, line := f.detail()
if fn != "" {
p.Printf("%s\n ", fn)
}
if file != "" {
p.Printf("%s:%d\n", file, line)
}
}