From 5de7463275c2c08b332af7d4168468daa5ab66ae Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Tue, 7 Jul 2020 16:31:16 -0700 Subject: [PATCH] Fix source formatting (#13) * fix source formatting --- README.md | 2 +- benchmarks_test.go | 7 +++++++ chain.go | 24 +++++++++++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c9b2d17..030783b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ Package errors ============ -![Project status](https://img.shields.io/badge/version-5.1.0-green.svg) +![Project status](https://img.shields.io/badge/version-5.1.1-green.svg) [![Build Status](https://travis-ci.org/go-playground/errors.svg?branch=master)](https://travis-ci.org/go-playground/errors) [![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/errors)](https://goreportcard.com/report/github.com/go-playground/errors) [![GoDoc](https://godoc.org/github.com/go-playground/errors?status.svg)](https://pkg.go.dev/github.com/go-playground/errors/v5) diff --git a/benchmarks_test.go b/benchmarks_test.go index 0127c34..338c159 100644 --- a/benchmarks_test.go +++ b/benchmarks_test.go @@ -10,6 +10,13 @@ func BenchmarkErrorNew(b *testing.B) { } } +func BenchmarkErrorWrap(b *testing.B) { + err := New("base error") + for i := 0; i < b.N; i++ { + _ = Wrap(err, "wrapped error") + } +} + func BenchmarkErrorParallelNew(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { diff --git a/chain.go b/chain.go index 20723f4..cf9f15e 100644 --- a/chain.go +++ b/chain.go @@ -76,19 +76,37 @@ type Link struct { // formatError prints a single Links error func (l *Link) formatError(b []byte) []byte { + var funcName string + b = append(b, "source="...) idx := strings.LastIndexByte(l.Source.Frame.Function, '.') if idx == -1 { b = append(b, l.Source.File()...) } else { - b = append(b, l.Source.Frame.Function[:idx]...) + funcName = l.Source.Frame.Function[idx+1:] + remaining := l.Source.Frame.Function[:idx] + + idx = strings.LastIndexByte(remaining, '/') + if idx > -1 { + b = append(b, l.Source.Frame.Function[:idx+1]...) + remaining = l.Source.Frame.Function[idx+1:] + } + + idx = strings.IndexByte(remaining, '.') + if idx == -1 { + b = append(b, remaining...) + } else { + b = append(b, remaining[:idx]...) + } b = append(b, '/') b = append(b, l.Source.File()...) } b = append(b, ':') b = strconv.AppendInt(b, int64(l.Source.Line()), 10) - b = append(b, ':') - b = append(b, l.Source.Frame.Function[idx+1:]...) + if funcName != "" { + b = append(b, ':') + b = append(b, funcName...) + } b = append(b, ' ') b = append(b, "error="...)