Skip to content

Commit 932cd95

Browse files
committed
examples
1 parent a96e318 commit 932cd95

File tree

11 files changed

+35
-30
lines changed

11 files changed

+35
-30
lines changed

builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
// ErrorBuilder is a utility to compose an error from type.
9-
// Typically, a direct usage is not required: either Type methods of helpers like Decorate is sufficient.
10-
// Only use builder if no neater alternative is available.
9+
// Typically, a direct usage is not required: either Type methods of helpers like Decorate are sufficient.
10+
// Only use builder if no simpler alternative is available.
1111
type ErrorBuilder struct {
1212
errorType *Type
1313
message string

error.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (e *Error) WithUnderlyingErrors(errs ...error) *Error {
5050
}
5151

5252
// Property extracts a dynamic property value from an error.
53-
// A property may belong to this error, or be extracted from the original cause.
53+
// A property may belong to this error or be extracted from the original cause.
5454
// The transparency rules are respected to some extent: both the original cause and the transparent wrapper
5555
// may have accessible properties, but an opaque wrapper hides the original properties.
5656
func (e *Error) Property(key Property) (interface{}, bool) {
@@ -73,7 +73,7 @@ func (e *Error) Property(key Property) (interface{}, bool) {
7373

7474
// HasTrait checks if an error possesses the expected trait.
7575
// Trait check works just as a type check would: opaque wrap hides the traits of the cause.
76-
// Traits are always a property of a type rather than of an instance, so trait check is an alternative to a type check.
76+
// Traits are always properties of a type rather than of an instance, so trait check is an alternative to a type check.
7777
// This alternative is preferable, though, as it is less brittle and generally creates less of a dependency.
7878
func (e *Error) HasTrait(key Trait) bool {
7979
cause := e
@@ -108,6 +108,7 @@ func (e *Error) IsOfType(t *Type) bool {
108108
// Type returns the exact type of this error.
109109
// With transparent wrapping, such as in Decorate(), returns the type of the original cause.
110110
// The result is always not nil, even if the resulting type is impossible to successfully type check against.
111+
//
111112
// NB: the exact error type may fail an equality check where a IsOfType() check would succeed.
112113
// This may happen if a type is checked against one of its supertypes, for example.
113114
// Therefore, handle direct type checks with care or avoid it altogether and use TypeSwitch() or IsForType() instead.
@@ -135,7 +136,7 @@ func (e *Error) Message() string {
135136
// Cause returns the immediate (wrapped) cause of current error.
136137
// This method could be used to dig for root cause of the error, but it is not advised to do so.
137138
// Errors should not require a complex navigation through causes to be properly handled, and the need to do so is a code smell.
138-
// Manually extracting cause defeats features such as properties of wrap, behaviour of properties etc.
139+
// Manually extracting cause defeats features such as opaque wrap, behaviour of properties etc.
139140
// This method is, therefore, reserved for system utilities, not for general use.
140141
func (e *Error) Cause() error {
141142
return e.cause
@@ -145,8 +146,8 @@ func (e *Error) Cause() error {
145146
// Supported verbs:
146147
//
147148
// %s simple message output
148-
// %v same as %s
149-
// %+v full output complete with a stack trace
149+
// %v same as %s
150+
// %+v full output complete with a stack trace
150151
//
151152
// In is nearly always preferable to use %+v format.
152153
// If a stack trace is not required, it should be omitted at the moment of creation rather in formatting.

example_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ func ExampleError_Format() {
7676
//
7777
//Error full: common.assertion_failed: example
7878
// at github.com/joomcode/errorx_test.someFunc()
79-
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:102
79+
// /Users/username/go/src/github.com/joomcode/errorx/example_test.go:102
8080
// at github.com/joomcode/errorx_test.nestedCall()
81-
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:98
81+
// /Users/username/go/src/github.com/joomcode/errorx/example_test.go:98
8282
// at github.com/joomcode/errorx_test.ExampleError_Format()
83-
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:66
83+
// /Users/username/go/src/github.com/joomcode/errorx/example_test.go:66
8484
// <...> more
8585
}
8686

@@ -97,7 +97,7 @@ func ExampleEnhanceStackTrace() {
9797
// Example output:
9898
//Error full: another goroutine, cause: common.assertion_failed: example
9999
// at github.com/joomcode/errorx_test.ExampleEnhanceStackTrace()
100-
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:94
100+
// /Users/username/go/src/github.com/joomcode/errorx/example_test.go:94
101101
// at testing.runExample()
102102
// /usr/local/Cellar/go/1.10.3/libexec/src/testing/example.go:122
103103
// at testing.runExamples()
@@ -110,11 +110,11 @@ func ExampleEnhanceStackTrace() {
110110
// (1 duplicated frames)
111111
// ----------------------------------
112112
// at github.com/joomcode/errorx_test.someFunc()
113-
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:106
113+
// /Users/username/go/src/github.com/joomcode/errorx/example_test.go:106
114114
// at github.com/joomcode/errorx_test.nestedCall()
115-
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:102
115+
// /Users/username/go/src/github.com/joomcode/errorx/example_test.go:102
116116
// at github.com/joomcode/errorx_test.ExampleEnhanceStackTrace.func1()
117-
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:90
117+
// /Users/username/go/src/github.com/joomcode/errorx/example_test.go:90
118118
// at runtime.goexit()
119119
// /usr/local/Cellar/go/1.10.3/libexec/src/runtime/asm_amd64.s:2361
120120
}

panic.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import "fmt"
1313
// as panics must not be a way to report conventional errors and are therefore rare.
1414
// With this in mind, it is better to err on the side of completeness rather than brevity.
1515
//
16-
// This function never returns, but the signature may be convenient, i.e.:
17-
// * return nil, errorx.Panic(err)
18-
// * panic(errorx.Panic(err))
16+
// This function never returns, but the signature may be used for convenience:
17+
//
18+
// return nil, errorx.Panic(err)
19+
// panic(errorx.Panic(err))
20+
//
1921
func Panic(err error) error {
2022
panic(newPanicErrorWrapper(err))
2123
}

property.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func ExtractPayload(err error) (interface{}, bool) {
5454
}
5555

5656
// ExtractProperty attempts to extract a property value by a provided key.
57-
// A property may belong to this error, or be extracted from the original cause.
57+
// A property may belong to this error or be extracted from the original cause.
5858
func ExtractProperty(err error, key Property) (interface{}, bool) {
5959
typedErr := Cast(err)
6060
if typedErr == nil {

stackframe.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ type frame interface {
1010
Line() int
1111
}
1212

13-
type FrameHelper struct {
13+
type frameHelper struct {
1414
}
1515

16-
var frameHelper = &FrameHelper{}
16+
var frameHelperSingleton = &frameHelper{}
1717

1818
type defaultFrame struct {
1919
frame *runtime.Frame
@@ -31,7 +31,7 @@ func (f *defaultFrame) Line() int {
3131
return f.frame.Line
3232
}
3333

34-
func (c *FrameHelper) GetFrames(pcs []uintptr) []frame {
34+
func (c *frameHelper) GetFrames(pcs []uintptr) []frame {
3535
frames := runtime.CallersFrames(pcs[:])
3636
result := make([]frame, 0, len(pcs))
3737

stacktrace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (st *stackTrace) formatStackTrace(s fmt.State) {
101101
return
102102
}
103103

104-
frames := frameHelper.GetFrames(pc)
104+
frames := frameHelperSingleton.GetFrames(pc)
105105
for _, frame := range frames {
106106
io.WriteString(s, "\n at ")
107107
io.WriteString(s, frame.Function())

switch.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ var (
1717
// For error types not in the 'types' list, including non-errorx errors, NotRecognisedType() is returned.
1818
// It is safe to treat NotRecognisedType() as 'any other type of not-nil error' case.
1919
// The effect is equivalent to a series of IsOfType() checks.
20-
// NB: if more than one provided types matches with error, the first match in the providers list is recognised.
20+
//
21+
// NB: if more than one provided types matches the error, the first match in the providers list is recognised.
2122
func TypeSwitch(err error, types ...*Type) *Type {
2223
typed := Cast(err)
2324

@@ -42,7 +43,8 @@ func TypeSwitch(err error, types ...*Type) *Type {
4243
// For error types that lack any of the provided traits, including non-errorx errors, CaseNoTrait() is returned.
4344
// It is safe to treat CaseNoTrait() as 'any other kind of not-nil error' case.
4445
// The effect is equivalent to a series of HasTrait() checks.
45-
// NB: if more than one provided types matches with error, the first match in the providers list is recognised.
46+
47+
// NB: if more than one provided types matches the error, the first match in the providers list is recognised.
4648
func TraitSwitch(err error, traits ...Trait) Trait {
4749
typed := Cast(err)
4850

trait.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func RegisterTrait(label string) Trait {
1515
}
1616

1717
// HasTrait checks if an error possesses the expected trait.
18-
// Traits are always a property of a type rather than of an instance, so trait check is an alternative to a type check.
18+
// Traits are always properties of a type rather than of an instance, so trait check is an alternative to a type check.
1919
// This alternative is preferable, though, as it is less brittle and generally creates less of a dependency.
2020
func HasTrait(err error, key Trait) bool {
2121
typedErr := Cast(err)

type.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (t *Type) WrapWithNoMessage(err error) *Error {
7474
}
7575

7676
// IsOfType is a type check for error.
77-
// Returns true either both are exactly the same type, or if the same is true for one of current type's ancestors.
77+
// Returns true either if both are of exactly the same type, or if the same is true for one of current type's ancestors.
7878
func (t *Type) IsOfType(other *Type) bool {
7979
current := t
8080
for current != nil {
@@ -89,7 +89,7 @@ func (t *Type) IsOfType(other *Type) bool {
8989
}
9090

9191
// IsOfType is a type check for errors.
92-
// Returns true either both are exactly the same type, or if the same is true for one of current type's ancestors.
92+
// Returns true either if both are of exactly the same type, or if the same is true for one of current type's ancestors.
9393
// For an error that does not have an errorx type, returns false.
9494
func IsOfType(err error, t *Type) bool {
9595
e := Cast(err)

0 commit comments

Comments
 (0)