Skip to content
Merged
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
_*
coverage.txt
profile.out
profile.out
go.sum
vendor/
5 changes: 4 additions & 1 deletion .travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ exit_code=0
go get -u golang.org/x/lint/golint
[ "0" = "$?" ] || exit 1

go mod download && go mod tidy && go mod vendor
[ "0" = "$?" ] || exit 3

for dir in $(go list ./... | grep -v vendor); do
echo "golint $dir"
result=$(golint $dir)
Expand All @@ -20,7 +23,7 @@ done

rm -f coverage.txt
for dir in $(go list ./... | grep -v vendor); do
go test -race -timeout 20s -coverprofile=profile.out $dir
go test -race -mod=vendor -timeout 20s -coverprofile=profile.out $dir
exit_code=$?
if [ "0" != "$exit_code" ]; then
exit $exit_code
Expand Down
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ git:
quiet: true
submodules: false

install: true
env:
- GOMAXPROCS=4 GORACE=halt_on_error=1 GOFLAGS="-count=1" GOCACHE= GO111MODULE=on

branches:
only:
- master

language: go
go_import_path: github.com/bdlm/errors
go:
- 1.9.x
- 1.10.x
- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x
- tip

script:
Expand Down
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ All notable changes to this project are documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- **Major**: backwards incompatible package updates
- **Minor**: feature additions
- **Patch**: bug fixes, backward compatible model and function changes, etc.

# v2.0.0 - 2020-05-01
`v2.0.0` is the production release of the `v0.2.0` development branch.

#### Added
* `go.mod`
* `github.com/bdlm/std/v2/errors` interfaces

#### Changed
* licence changed from BSD to MIT
* replace interfaces with `github.com/bdlm/std/v2/errors` implementations
* simplified formatting and marshalling logic
* renamed `GetCaller(error) std_err.Caller` to `Caller(error) std_err.Caller`

#### Removed
* unused code

# v0.2.1 - 2019-07-18
#### Added
* Documentation and examples
Expand Down
42 changes: 17 additions & 25 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
BSD 3-Clause License
MIT License

Copyright (c) 2018, Bedlam Software
All rights reserved.
Copyright (c) 2018 Bedlam Software

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 23 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# errors

<p align="center">
<a href="https://github.com/bdlm/errors/blob/master/LICENSE"><img src="https://img.shields.io/github/license/bdlm/errors.svg" alt="BSD-3-Clause"></a>
<a href="https://github.com/mkenney/software-guides/blob/master/STABILITY-BADGES.md#beta"><img src="https://img.shields.io/badge/stability-beta-33bbff.svg" alt="Beta"></a>
<a href="https://travis-ci.org/bdlm/errors"><img src="https://travis-ci.org/bdlm/errors.svg?branch=master" alt="Build status"></a>
<a href="https://codecov.io/gh/bdlm/errors"><img src="https://img.shields.io/codecov/c/github/bdlm/errors/master.svg" alt="Coverage status"></a>
<a href="https://goreportcard.com/report/github.com/bdlm/errors"><img src="https://goreportcard.com/badge/github.com/bdlm/errors" alt="Go Report Card"></a>
<a href="https://github.com/bdlm/errors/issues"><img src="https://img.shields.io/github/issues-raw/bdlm/errors.svg" alt="Github issues"></a>
<a href="https://github.com/bdlm/errors/pulls"><img src="https://img.shields.io/github/issues-pr/bdlm/errors.svg" alt="Github pull requests"></a>
<a href="https://godoc.org/github.com/bdlm/errors"><img src="https://godoc.org/github.com/bdlm/errors?status.svg" alt="GoDoc"></a>
</p>
<a href="https://github.com/mkenney/software-guides/blob/master/STABILITY-BADGES.md#mature"><img src="https://img.shields.io/badge/stability-mature-008000.svg" alt="Mature"></a> This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). This package is considered mature, you should expect package stability in <strong>Minor</strong> and <strong>Patch</strong> version releases

- **Major**: backwards incompatible package updates
- **Minor**: feature additions
- **Patch**: bug fixes, backward compatible model and function changes, etc.

**[CHANGELOG](CHANGELOG.md)**<br>

<a href="https://pkg.go.dev/github.com/bdlm/errors#pkg-examples"><img src="https://godoc.org/github.com/bdlm/errors?status.svg" alt="GoDoc"></a>
<a href="https://travis-ci.org/bdlm/errors"><img src="https://travis-ci.org/bdlm/errors.svg?branch=master" alt="Build status"></a>
<a href="https://codecov.io/gh/bdlm/errors"><img src="https://img.shields.io/codecov/c/github/bdlm/errors/master.svg" alt="Coverage status"></a>
<a href="https://goreportcard.com/report/github.com/bdlm/errors"><img src="https://goreportcard.com/badge/github.com/bdlm/errors" alt="Go Report Card"></a>
<a href="https://github.com/bdlm/errors/issues"><img src="https://img.shields.io/github/issues-raw/bdlm/errors.svg" alt="Github issues"></a>
<a href="https://github.com/bdlm/errors/pulls"><img src="https://img.shields.io/github/issues-pr/bdlm/errors.svg" alt="Github pull requests"></a>
<a href="https://github.com/bdlm/errors/blob/master/LICENSE"><img src="https://img.shields.io/github/license/bdlm/errors.svg" alt="MIT"></a>

`bdlm/errors` provides simple, concise, useful error handling and annotation.

Expand All @@ -29,7 +34,7 @@ All notable changes to this project are documented in the [`CHANGELOG`](CHANGELO

## Quick start

See the [documentation](https://godoc.org/github.com/bdlm/errors#pkg-examples) for more examples.
See the [documentation](https://pkg.go.dev/github.com/bdlm/errors#pkg-examples) for more examples. All exported methods work with any `error` type as well as `nil` values.

```
go get github.com/bdlm/errors
Expand Down Expand Up @@ -110,10 +115,10 @@ See the [documentation](https://godoc.org/github.com/bdlm/errors#pkg-examples) f

## The `Error` interface

The exported package methods return an interface that exposes additional metadata and troubleshooting information:
The exported package methods return `github.com/bdlm/std/v2/errors` interfaces that expose metadata and troubleshooting information:

```go
// Error defines the error interface.
// Error defines a robust error stack interface.
type Error interface {
// Caller returns the associated Caller instance.
Caller() Caller
Expand All @@ -133,7 +138,7 @@ type Error interface {
Unwrap() Error
}

// Caller holds runtime.Caller data.
// Caller defines an interface to runtime caller results.
type Caller interface {
// File returns the file in which the call occurred.
File() string
Expand All @@ -148,6 +153,9 @@ type Caller interface {
Pc() uintptr

// Trace returns the call stack.
Trace() []Caller
Trace() Trace
}

// Trace defines an error trace.
type Trace []Caller
```
34 changes: 9 additions & 25 deletions caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,23 @@ import (
"fmt"
"runtime"
"strings"
)

// Caller holds runtime.Caller data for an error.
type Caller interface {
// File returns the file in which the call occurred.
File() string

// Func returns the name of the function in which the call occurred.
Func() string

// Line returns the line number in the file in which the call occurred.
Line() int

// Pc returns the program counter.
Pc() uintptr

// Trace returns the call stack.
Trace() []Caller
}
std_err "github.com/bdlm/std/v2/errors"
)

// caller holds runtime.Caller data.
// caller is a github.com/bdlm/std.Caller interface implementation and holds
// runtime.Caller data.
type caller struct {
file string
line int
ok bool
pc uintptr
trace []Caller
trace std_err.Trace
}

// NewCaller returns a new caller instance containing data for the current
// call stack.
func NewCaller() Caller {
trace := []Caller{}
// NewCaller returns a new Caller containing data for the current call stack.
func NewCaller() std_err.Caller {
trace := std_err.Trace{}
clr := caller{}
a := 0
for {
Expand Down Expand Up @@ -91,6 +75,6 @@ func (caller caller) String() string {
}

// Trace implements Caller.
func (caller caller) Trace() []Caller {
func (caller caller) Trace() std_err.Trace {
return caller.trace
}
33 changes: 8 additions & 25 deletions error.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
package errors

// Error defines the interface for accessing the details of an error managed
// by this package.
type Error interface {
// Caller returns the associated Caller instance.
Caller() Caller
import (
std_err "github.com/bdlm/std/v2/errors"
)

// Error implements error.
Error() string

// Has tests to see if the test error exists anywhere in the error
// stack.
Has(test error) bool

// Is tests to see if the test error matches most recent error in the
// stack.
Is(test error) bool

// Unwrap returns the next error, if any.
Unwrap() Error
}

// E is an Error interface implementation and simply wraps the exported
// methods as a convenience.
// E is a github.com/bdlm/std.Error interface implementation and simply wraps
// the exported package methods as a convenience.
type E struct {
caller Caller
caller std_err.Caller
err error
prev error
}

// Caller implements Error.
func (e E) Caller() Caller {
func (e E) Caller() std_err.Caller {
return e.caller
}

Expand All @@ -50,7 +33,7 @@ func (e E) Is(test error) bool {
}

// Unwrap implements Error.
func (e E) Unwrap() Error {
func (e E) Unwrap() std_err.Error {
return Unwrap(e)
}

Expand Down
20 changes: 10 additions & 10 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"testing"

"github.com/bdlm/errors"
"github.com/bdlm/errors/v2"

"github.com/stretchr/testify/assert"
)
Expand All @@ -19,7 +19,7 @@ func TestMarshaller(t *testing.T) {
byts, err := json.Marshal(err)
assert.Equal(nil, err, "err is not nil")
assert.Equal(
"[{\"caller\":\"#0 error_test.go:18 (github.com/bdlm/errors_test.TestMarshaller)\",\"error\":\"test 1\"}]",
"[{\"caller\":\"#0 error_test.go:18 (github.com/bdlm/errors/v2_test.TestMarshaller)\",\"error\":\"test 1\"}]",
string(byts),
"JSON did not encode properly",
)
Expand All @@ -32,7 +32,7 @@ func TestTrace(t *testing.T) {
err = errors.Trace(err)

assert.Equal(32, err.Caller().Line(), "caller did not reflect the correct line number")
assert.Equal("github.com/bdlm/errors_test.TestTrace", err.Caller().Func(), "caller did not reflect the correct function name")
assert.Equal("github.com/bdlm/errors/v2_test.TestTrace", err.Caller().Func(), "caller did not reflect the correct function name")

err = errors.Trace(nil)
assert.Equal(nil, err, "err is not nil")
Expand All @@ -45,10 +45,10 @@ func TestTrack(t *testing.T) {
err = errors.Track(err)

assert.Equal(44, err.Caller().Line(), "caller did not reflect the correct line number")
assert.Equal("github.com/bdlm/errors_test.TestTrack", err.Caller().Func(), "caller did not reflect the correct function name")
assert.Equal("github.com/bdlm/errors/v2_test.TestTrack", err.Caller().Func(), "caller did not reflect the correct function name")

assert.Equal(45, errors.Unwrap(err).Caller().Line(), "caller did not reflect the correct line number")
assert.Equal("github.com/bdlm/errors_test.TestTrack", errors.Unwrap(err).Caller().Func(), "caller did not reflect the correct function name")
assert.Equal("github.com/bdlm/errors/v2_test.TestTrack", errors.Unwrap(err).Caller().Func(), "caller did not reflect the correct function name")

err = errors.Track(nil)
assert.Equal(nil, err, "err is not nil")
Expand All @@ -58,9 +58,9 @@ func TestCallerString(t *testing.T) {
assert := assert.New(t)

err := errors.New("test 1")
caller := errors.GetCaller(err)
caller := errors.Caller(err)

assert.Equal("github.com/bdlm/errors_test.TestCallerString:60", caller.(fmt.Stringer).String(), "caller.String() did not return the correct output")
assert.Equal("github.com/bdlm/errors/v2_test.TestCallerString:60", caller.(fmt.Stringer).String(), "caller.String() did not return the correct output")
}

func TestHas(t *testing.T) {
Expand Down Expand Up @@ -169,16 +169,16 @@ func TestErrorf(t *testing.T) {
assert.Equal("test 1", err.Error(), "err is not 'test 1'")
}

func TestGetCaller(t *testing.T) {
func TestCaller(t *testing.T) {
var err error
assert := assert.New(t)

err = errors.New("test 1")
caller := errors.GetCaller(err)
caller := errors.Caller(err)
assert.NotEqual(nil, caller, "caller is nil")

err = nil
caller = errors.GetCaller(err)
caller = errors.Caller(err)
assert.Equal(nil, caller, "caller is not nil")
}

Expand Down
Loading