Skip to content

Commit

Permalink
(rs#470) Make Function Wrap Public
Browse files Browse the repository at this point in the history
Signed-off-by: Bhargav Ravuri <vaguecoder0to.n@gmail.com>
  • Loading branch information
vaguecoder committed Sep 12, 2022
1 parent c083e5b commit ae0407d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion internal/errors/errors.go → pkgerrors/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package errors
package pkgerrors

// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
Expand Down
16 changes: 9 additions & 7 deletions internal/errors/errors_test.go → pkgerrors/errors_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package errors_test
// +build !binary_log

package pkgerrors_test

import (
"errors"
"fmt"
"io"
"testing"

internalErrors "github.com/rs/zerolog/internal/errors"
"github.com/rs/zerolog/pkgerrors"
)

func TestNew(t *testing.T) {
Expand All @@ -16,20 +18,20 @@ func TestNew(t *testing.T) {
}{
{"", fmt.Errorf("")},
{"foo", fmt.Errorf("foo")},
{"foo", internalErrors.New("foo")},
{"foo", pkgerrors.New("foo")},
{"string with format specifiers: %v", errors.New("string with format specifiers: %v")},
}

for _, tt := range tests {
got := internalErrors.New(tt.err)
got := pkgerrors.New(tt.err)
if got.Error() != tt.want.Error() {
t.Errorf("New.Error(): got: %q, want %q", got, tt.want)
}
}
}

func TestWrapNil(t *testing.T) {
got := internalErrors.Wrap(nil, "no error")
got := pkgerrors.Wrap(nil, "no error")
if got != nil {
t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got)
}
Expand All @@ -42,13 +44,13 @@ func TestWrap(t *testing.T) {
want string
}{
{io.EOF, "read error", "read error: EOF"},
{internalErrors.Wrap(io.EOF, "read error"), "client error", "client error: read error: EOF"},
{pkgerrors.Wrap(io.EOF, "read error"), "client error", "client error: read error: EOF"},
}

for _, tt := range tests {
var got string
if tt.err != nil {
got = internalErrors.Wrap(tt.err, tt.message).Error()
got = pkgerrors.Wrap(tt.err, tt.message).Error()
}
if got != tt.want {
t.Errorf("Wrap(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
Expand Down
2 changes: 1 addition & 1 deletion internal/errors/stack.go → pkgerrors/stack.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package errors
package pkgerrors

import (
"fmt"
Expand Down
46 changes: 24 additions & 22 deletions internal/errors/stack_test.go → pkgerrors/stack_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package errors_test
// +build !binary_log

package pkgerrors_test

import (
"errors"
Expand All @@ -8,29 +10,29 @@ import (
"runtime"
"testing"

internalErrors "github.com/rs/zerolog/internal/errors"
"github.com/rs/zerolog/pkgerrors"
)

func Test_StackTrace(t *testing.T) {
tests := []struct {
name string
s internalErrors.Stack
want internalErrors.StackTrace
s pkgerrors.Stack
want pkgerrors.StackTrace
}{
{
name: "Nil-Stack",
s: internalErrors.Stack(nil),
want: internalErrors.StackTrace([]internalErrors.Frame{}),
s: pkgerrors.Stack(nil),
want: pkgerrors.StackTrace([]pkgerrors.Frame{}),
},
{
name: "Empty-Stack",
s: internalErrors.Stack([]uintptr{}),
want: internalErrors.StackTrace([]internalErrors.Frame{}),
s: pkgerrors.Stack([]uintptr{}),
want: pkgerrors.StackTrace([]pkgerrors.Frame{}),
},
{
name: "Success",
s: internalErrors.Stack([]uintptr{1, 2, 3}),
want: internalErrors.StackTrace([]internalErrors.Frame{1, 2, 3}),
s: pkgerrors.Stack([]uintptr{1, 2, 3}),
want: pkgerrors.StackTrace([]pkgerrors.Frame{1, 2, 3}),
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -97,20 +99,20 @@ func TestFrame_Format(t *testing.T) {

tests := []struct {
name string
f internalErrors.Frame
f pkgerrors.Frame
args args
}{
{
name: "Source-File-With-Flag-False",
f: internalErrors.Frame(1),
f: pkgerrors.Frame(1),
args: args{
s: &state{},
verb: verbSourceFile,
},
},
{
name: "Source-File-Invalid-Frame-Number",
f: internalErrors.Frame(0),
f: pkgerrors.Frame(0),
args: args{
s: &state{
flag: true,
Expand All @@ -130,7 +132,7 @@ func TestFrame_Format(t *testing.T) {
},
{
name: "Source-File-Known-File-Name-and-Path",
f: internalErrors.Frame(pc[0]),
f: pkgerrors.Frame(pc[0]),
args: args{
s: &state{
flag: true,
Expand All @@ -154,7 +156,7 @@ func TestFrame_Format(t *testing.T) {
},
{
name: "Source-File-Error-At-Write",
f: internalErrors.Frame(pc[0]),
f: pkgerrors.Frame(pc[0]),
args: args{
s: &state{
flag: true,
Expand All @@ -178,7 +180,7 @@ func TestFrame_Format(t *testing.T) {
},
{
name: "Source-Line",
f: internalErrors.Frame(pc[0]),
f: pkgerrors.Frame(pc[0]),
args: args{
s: &state{
inputOutput: map[string]writeResult{
Expand All @@ -193,7 +195,7 @@ func TestFrame_Format(t *testing.T) {
},
{
name: "Source-Line-Error-At-Write",
f: internalErrors.Frame(pc[0]),
f: pkgerrors.Frame(pc[0]),
args: args{
s: &state{
inputOutput: map[string]writeResult{
Expand All @@ -208,7 +210,7 @@ func TestFrame_Format(t *testing.T) {
},
{
name: "Source-Line-Invalid-Frame-Number",
f: internalErrors.Frame(1),
f: pkgerrors.Frame(1),
args: args{
s: &state{
inputOutput: map[string]writeResult{
Expand All @@ -223,7 +225,7 @@ func TestFrame_Format(t *testing.T) {
},
{
name: "Func-Name",
f: internalErrors.Frame(pc[0]),
f: pkgerrors.Frame(pc[0]),
args: args{
s: &state{
inputOutput: map[string]writeResult{
Expand All @@ -238,7 +240,7 @@ func TestFrame_Format(t *testing.T) {
},
{
name: "Func-Name-Error-At-Write",
f: internalErrors.Frame(pc[0]),
f: pkgerrors.Frame(pc[0]),
args: args{
s: &state{
inputOutput: map[string]writeResult{
Expand All @@ -253,7 +255,7 @@ func TestFrame_Format(t *testing.T) {
},
{
name: "Source-File-and-Line",
f: internalErrors.Frame(pc[0]),
f: pkgerrors.Frame(pc[0]),
args: args{
s: &state{
flag: true,
Expand Down Expand Up @@ -285,7 +287,7 @@ func TestFrame_Format(t *testing.T) {
},
{
name: "Source-File-and-Line-Error-At-Write",
f: internalErrors.Frame(pc[0]),
f: pkgerrors.Frame(pc[0]),
args: args{
s: &state{
flag: true,
Expand Down
10 changes: 3 additions & 7 deletions pkgerrors/stacktrace.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package pkgerrors

import (
"github.com/rs/zerolog/internal/errors"
)

var (
StackSourceFileName = "source"
StackSourceLineName = "line"
Expand Down Expand Up @@ -35,17 +31,17 @@ func (s *state) Flag(c int) bool {
return false
}

func frameField(f errors.Frame, s *state, c rune) string {
func frameField(f Frame, s *state, c rune) string {
f.Format(s, c)
return string(s.b)
}

// MarshalStack implements pkg/errors stack trace marshaling.
// MarshalStack implements errors stack trace marshaling.
//
// zerolog.ErrorStackMarshaler = MarshalStack
func MarshalStack(err error) interface{} {
type stackTracer interface {
StackTrace() errors.StackTrace
StackTrace() StackTrace
}
sterr, ok := err.(stackTracer)
if !ok {
Expand Down
16 changes: 8 additions & 8 deletions pkgerrors/stacktrace_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// +build !binary_log

package pkgerrors
package pkgerrors_test

import (
"bytes"
"regexp"
"testing"

"github.com/rs/zerolog"
"github.com/rs/zerolog/internal/errors"
"github.com/rs/zerolog/pkgerrors"
)

func TestLogStack(t *testing.T) {
zerolog.ErrorStackMarshaler = MarshalStack
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack

out := &bytes.Buffer{}
log := zerolog.New(out)

err := errors.Wrap(errors.New("error message"), "from error")
err := pkgerrors.Wrap(pkgerrors.New("error message"), "from error")
log.Log().Stack().Err(err).Msg("")

got := out.String()
Expand All @@ -28,12 +28,12 @@ func TestLogStack(t *testing.T) {
}

func TestLogStackFromContext(t *testing.T) {
zerolog.ErrorStackMarshaler = MarshalStack
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack

out := &bytes.Buffer{}
log := zerolog.New(out).With().Stack().Logger() // calling Stack() on log context instead of event

err := errors.Wrap(errors.New("error message"), "from error")
err := pkgerrors.Wrap(pkgerrors.New("error message"), "from error")
log.Log().Err(err).Msg("") // not explicitly calling Stack()

got := out.String()
Expand All @@ -44,10 +44,10 @@ func TestLogStackFromContext(t *testing.T) {
}

func BenchmarkLogStack(b *testing.B) {
zerolog.ErrorStackMarshaler = MarshalStack
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
out := &bytes.Buffer{}
log := zerolog.New(out)
err := errors.Wrap(errors.New("error message"), "from error")
err := pkgerrors.Wrap(pkgerrors.New("error message"), "from error")
b.ReportAllocs()

for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit ae0407d

Please sign in to comment.