-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
10,418 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
go_import_path: go.fraixed.es/errors | ||
language: go | ||
|
||
matrix: | ||
include: | ||
- go: "1.11.x" | ||
env: GO111MODULE=on | ||
env: LINT=true | ||
- go: "1.10.x" | ||
- go: "1.9.x" | ||
- go: "1.8.x" | ||
- go: tip | ||
env: GO111MODULE=on | ||
allow_failures: | ||
- go: tip | ||
env: GO111MODULE=on | ||
|
||
before_install: | ||
# setup some env vars | ||
- GO_FILES=$(find . -iname '*.go' | grep -v /vendor/) # All the .go files, excluding vendor/ | ||
- PKGS=$(go list ./... | grep -v /vendor/) # All the import paths, excluding vendor/ | ||
|
||
# Install CI tools | ||
- make .go-tools-install-ci | ||
|
||
# Install goveralls, Go integration for Coveralls.io. | ||
- go get -u github.com/mattn/goveralls | ||
|
||
script: | ||
- test -z $(gofmt -s -l $GO_FILES) # Fail if a .go file hasn't been formatted with gofmt | ||
- if [ "$LINT" = true ]; then make lint; fi | ||
- go test -v -covermode=count -coverprofile=profile.cov $PKGS | ||
- goveralls -coverprofile=profile.cov -service=travis-ci | ||
|
||
notifications: | ||
email: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
GOLANGCI_LINT_VERSION := 1.12.3 | ||
GOLANGCI_LINT_BIN := $(realpath .bin/golangci-lint) | ||
|
||
define HELP_MSG | ||
Execute one of the following targets: | ||
|
||
endef | ||
|
||
export HELP_MSG | ||
|
||
|
||
.PHONY: help | ||
help: ## Show this help | ||
@echo "$$HELP_MSG" | ||
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/:.*##/:##/' | column -t -s '##' | ||
|
||
.PHONY: go-tools-install | ||
go-tools-install: .gti-golangci-lint ## Install Go tools | ||
|
||
.PHONY: lint | ||
lint: ## Lint the code | ||
@$(GOLANGCI_LINT_BIN) run --enable-all --exclude-use-default=false | ||
|
||
.PHONY: .go-tools-install-ci | ||
.go-tools-install-ci: .gti-golangci-lint | ||
|
||
.PHONY: .gti-golangci-lint | ||
.gti-golangci-lint: | ||
@mkdir -p .bin | ||
@curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b .bin v$(GOLANGCI_LINT_VERSION) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
type callStack []uintptr | ||
|
||
// newCallStack creates a callStack of calls skipping the call to | ||
// runtime.Callers, newCallStack and the caller of newCallStack. | ||
// The newCallStack is skipped because it's meant to be used by the errors | ||
// consturctors and they shouldn't appear in the error value call stack. | ||
func newCallStack() callStack { | ||
var ( | ||
depth = 20 | ||
pcs = make([]uintptr, depth) | ||
l = runtime.Callers(3, pcs) | ||
) | ||
|
||
for l == depth { | ||
depth += 10 | ||
pcs = make([]uintptr, depth) | ||
l = runtime.Callers(3, pcs) | ||
} | ||
|
||
pcs = pcs[:l] | ||
|
||
return callStack(pcs) | ||
} | ||
|
||
// Format satisfies the fmt.Formatter interface. | ||
// It only prints the value if the verb 'v' and flag '+' are used, printing the | ||
// complete function identifier (package path + name), the file and the line. | ||
// When the additional '-' is used, then only the function identifier is | ||
// printed. | ||
func (cs callStack) Format(state fmt.State, verb rune) { | ||
if verb != 'v' { | ||
return | ||
} | ||
|
||
if len(cs) > 0 { | ||
var frs = runtime.CallersFrames(cs) | ||
for { | ||
var f, more = frs.Next() | ||
|
||
if state.Flag('-') { | ||
_, _ = fmt.Fprintf(state, "\t%s", f.Function) | ||
} else { | ||
_, _ = fmt.Fprintf(state, "\t%s\n\t\t%s:%d", f.Function, f.File, f.Line) | ||
} | ||
|
||
if !more { | ||
break | ||
} | ||
|
||
fmt.Fprint(state, "\n") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCallStack_Format(t *testing.T) { | ||
var ( | ||
ptName = t.Name() | ||
cstk callStack | ||
skippedFn = func() { cstk = newCallStack() } | ||
f1 = func() { skippedFn() } | ||
) | ||
f1() | ||
|
||
t.Run("'v' verb", func(t *testing.T) { | ||
var s = fmt.Sprintf("%v", cstk) | ||
var sls = strings.Split(s, "\n") | ||
|
||
assert.Contains(t, sls[0], fmt.Sprintf("errors.%s.func2", ptName)) | ||
assert.Contains(t, sls[1], "errors/callstack_test.go:17") | ||
}) | ||
|
||
t.Run("'v' verb and '-' flags", func(t *testing.T) { | ||
var s = fmt.Sprintf("%-v", cstk) | ||
var sls = strings.Split(s, "\n") | ||
|
||
assert.Contains(t, sls[0], fmt.Sprintf("errors.%s.func2", ptName)) | ||
}) | ||
|
||
t.Run("any other verb", func(t *testing.T) { | ||
var verbs = [...]string{ | ||
"t", "b", "c", "d", "o", "q", "x", "X", "U", "e", "E", "f", "F", "g", "G", "q", "p", "s", | ||
} | ||
|
||
var f = fmt.Sprintf("%%%s", verbs[rand.Intn(len(verbs))]) | ||
var s = fmt.Sprintf(f, cstk) | ||
assert.Empty(t, s) | ||
}) | ||
|
||
t.Run("empty stack", func(t *testing.T) { | ||
var ( | ||
ecstk = callStack(nil) | ||
s = fmt.Sprintf("%v", ecstk) | ||
) | ||
|
||
assert.Empty(t, s) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package errors | ||
|
||
// Code is the interface that any error code must satisfies. | ||
type Code interface { | ||
String() string | ||
Message() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package errors | ||
|
||
import "github.com/gofrs/uuid" | ||
|
||
// New creates a new error with c and mds. | ||
func New(c Code, mds ...MD) error { | ||
var id, _ = uuid.NewV4() | ||
|
||
return derror{ | ||
c: c, | ||
id: id, | ||
mds: mds, | ||
cs: newCallStack(), | ||
} | ||
} | ||
|
||
// Wrap creates a new error with c and mds, wrapping err. | ||
func Wrap(err error, c Code, mds ...MD) error { | ||
var ( | ||
id, _ = uuid.NewV4() | ||
derr = derror{ | ||
c: c, | ||
id: id, | ||
mds: mds, | ||
cs: newCallStack(), | ||
} | ||
) | ||
|
||
if de, ok := err.(derror); ok { | ||
de.cs = nil | ||
|
||
derr.werr = de | ||
} else { | ||
derr.werr = err | ||
} | ||
|
||
return derr | ||
} |
Oops, something went wrong.