Skip to content

Commit d78ca17

Browse files
committed
Minor code cleanup; updated docs and travis-ci test matrix
1 parent e97ab0c commit d78ca17

File tree

5 files changed

+77
-75
lines changed

5 files changed

+77
-75
lines changed

.travis.yml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
language: go
22

33
go:
4+
- 1.8
45
- 1.7.5
56
- 1.7.4
67
- 1.7.3
78
- 1.7.2
89
- 1.7.1
910
- 1.7
11+
- tip
1012
- 1.6.4
1113
- 1.6.3
1214
- 1.6.2
1315
- 1.6.1
1416
- 1.6
15-
- tip
1617
- 1.5.4
1718
- 1.5.3
1819
- 1.5.2
@@ -32,15 +33,16 @@ go:
3233
- 1.1.2
3334
- 1.1.1
3435
- 1.1
35-
- 1.0.3
36-
- 1.0.2
37-
- 1.0.1
38-
- 1
3936

4037
matrix:
4138
fast_finish: true
4239
allow_failures:
4340
- go: tip
41+
- go: 1.6.4
42+
- go: 1.6.3
43+
- go: 1.6.2
44+
- go: 1.6.1
45+
- go: 1.6
4446
- go: 1.5.4
4547
- go: 1.5.3
4648
- go: 1.5.2
@@ -60,7 +62,3 @@ matrix:
6062
- go: 1.1.2
6163
- go: 1.1.1
6264
- go: 1.1
63-
- go: 1.0.3
64-
- go: 1.0.2
65-
- go: 1.0.1
66-
- go: 1

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# A Go package for handling of runtime panics
22

3-
Package calmly implements convenient runtime panic recovery and handling in [Go](http://golang.org).
3+
[![Release](https://img.shields.io/github/release/agext/calmly.svg?style=flat)](https://github.com/agext/calmly/releases/latest)
4+
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/agext/calmly) 
5+
[![Build Status](https://travis-ci.org/agext/calmly.svg?branch=master&style=flat)](https://travis-ci.org/agext/calmly)
6+
[![Coverage Status](https://coveralls.io/repos/github/agext/calmly/badge.svg?style=flat)](https://coveralls.io/github/agext/calmly)
7+
[![Go Report Card](https://goreportcard.com/badge/github.com/agext/calmly?style=flat)](https://goreportcard.com/report/github.com/agext/calmly)
8+
49

5-
## Maturity
10+
Package calmly implements convenient runtime panic recovery and handling in [Go](http://golang.org).
611

7-
[![Build Status](https://travis-ci.org/agext/calmly.svg?branch=master)](https://travis-ci.org/agext/calmly)
12+
## Project Status
813

9-
v1.0 Stable: Guaranteed no breaking changes to the API in future v1.x releases. No known bugs or performance issues. Probably safe to use in production, though provided on "AS IS" basis.
14+
v1.0.1 Stable: Guaranteed no breaking changes to the API in future v1.x releases. Probably safe to use in production, though provided on "AS IS" basis.
1015

1116
This package is being actively maintained. If you encounter any problems or have any suggestions for improvement, please [open an issue](https://github.com/agext/calmly/issues). Pull requests are welcome.
1217

1318
## Overview
1419

15-
[![GoDoc](https://godoc.org/github.com/agext/calmly?status.png)](https://godoc.org/github.com/agext/calmly)
16-
1720
When a panic condition needs to be handled by the program (rather than crashing it), wrap the code that can trigger such condition in a `Try`, which allows you to `Catch` the panic for further processing.
1821

1922
The `Outcome` of a `Try`ed code also offers convenience methods to:

calmly.go

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -72,90 +72,90 @@ func Try(f interface{}) (o *Outcome) {
7272

7373
// Catch calls the provided function passing the receiver Outcome as argument,
7474
// only if the Outcome is at PANIC level.
75-
func (this *Outcome) Catch(f func(*Outcome)) *Outcome {
76-
if this.level == PANIC {
77-
f(this)
75+
func (o *Outcome) Catch(f func(*Outcome)) *Outcome {
76+
if o.level == PANIC {
77+
f(o)
7878
}
79-
return this
79+
return o
8080
}
8181

8282
// KeepCalm downgrades a PANIC to ERROR level, to avoid triggering a panic upon
8383
// logging the outcome.
84-
func (this *Outcome) KeepCalm() *Outcome {
85-
if this.level == PANIC {
86-
this.level = ERROR
84+
func (o *Outcome) KeepCalm() *Outcome {
85+
if o.level == PANIC {
86+
o.level = ERROR
8787
}
88-
return this
88+
return o
8989
}
9090

9191
// Escalate converts a PANIC into a FATAL condition, to trigger program
9292
// termination upon logging the outcome.
93-
func (this *Outcome) Escalate() *Outcome {
94-
if this.level == PANIC {
95-
this.level = FATAL
93+
func (o *Outcome) Escalate() *Outcome {
94+
if o.level == PANIC {
95+
o.level = FATAL
9696
}
97-
return this
97+
return o
9898
}
9999

100100
// Log sends the error-condition Outcome to the provided log, using the appropriate
101101
// logging function: FATAL conditions are logged using Fatal(), PANIC using
102102
// Panic(), and ERROR using Print(). Non-error conditions are not logged
103103
// because there is no information stored in the Outcome, beside
104104
// what the Try-ed function returned (and is better suited to log itself).
105-
func (this *Outcome) Log(log Logger) *Outcome {
106-
switch this.level {
105+
func (o *Outcome) Log(log Logger) *Outcome {
106+
switch o.level {
107107
case FATAL:
108-
log.Fatal(this)
108+
log.Fatal(o)
109109
case PANIC:
110-
log.Panic(this)
110+
log.Panic(o)
111111
case ERROR:
112-
log.Print(this)
112+
log.Print(o)
113113
}
114-
return this
114+
return o
115115
}
116116

117117
// Level returns the error level stored by the receiver.
118-
func (this *Outcome) Level() int8 {
119-
return this.level
118+
func (o *Outcome) Level() int8 {
119+
return o.level
120120
}
121121

122122
// SetLevel sets the error level stored by the receiver.
123-
func (this *Outcome) SetLevel(l int8) *Outcome {
123+
func (o *Outcome) SetLevel(l int8) *Outcome {
124124
if levelName(l) != "?" {
125-
this.level = l
125+
o.level = l
126126
}
127-
return this
127+
return o
128128
}
129129

130130
// Code returns the error code stored by the receiver.
131-
func (this *Outcome) Code() int {
132-
return this.code
131+
func (o *Outcome) Code() int {
132+
return o.code
133133
}
134134

135135
// SetCode sets the error code stored by the receiver.
136-
func (this *Outcome) SetCode(c int) *Outcome {
137-
this.code = c
138-
return this
136+
func (o *Outcome) SetCode(c int) *Outcome {
137+
o.code = c
138+
return o
139139
}
140140

141141
// Text returns the error text stored by the receiver.
142-
func (this *Outcome) Text() string {
143-
return this.text
142+
func (o *Outcome) Text() string {
143+
return o.text
144144
}
145145

146146
// SetText sets the error text stored by the receiver.
147-
func (this *Outcome) SetText(t string) *Outcome {
148-
this.text = t
149-
return this
147+
func (o *Outcome) SetText(t string) *Outcome {
148+
o.text = t
149+
return o
150150
}
151151

152152
// Info returns the error info stored by the receiver.
153-
func (this *Outcome) Info() []string {
154-
return this.info
153+
func (o *Outcome) Info() []string {
154+
return o.info
155155
}
156156

157157
// addInfo adds (more) error info to the receiver.
158-
func (this *Outcome) addInfo(calldepth int, s ...string) *Outcome {
158+
func (o *Outcome) addInfo(calldepth int, s ...string) *Outcome {
159159
for i, line := range s {
160160
if line == "debug.stack" {
161161
calldepth *= 2
@@ -181,41 +181,41 @@ func (this *Outcome) addInfo(calldepth int, s ...string) *Outcome {
181181
break
182182
}
183183
}
184-
this.info = append(this.info, s...)
185-
return this
184+
o.info = append(o.info, s...)
185+
return o
186186
}
187187

188188
// AddInfo adds (more) error info to the receiver.
189-
func (this *Outcome) AddInfo(s ...string) *Outcome {
190-
return this.addInfo(2, s...)
189+
func (o *Outcome) AddInfo(s ...string) *Outcome {
190+
return o.addInfo(2, s...)
191191
}
192192

193193
// Value provides the value returned by the Try-ed function, if any.
194-
func (this *Outcome) Value() interface{} {
195-
return this.val
194+
func (o *Outcome) Value() interface{} {
195+
return o.val
196196
}
197197

198198
// Err provides the error returned by the Try-ed function, if any.
199-
func (this *Outcome) Err() error {
200-
return this.err
199+
func (o *Outcome) Err() error {
200+
return o.err
201201
}
202202

203203
// Result provides the value and error returned by the Try-ed function, if any.
204-
func (this *Outcome) Result() (interface{}, error) {
205-
return this.val, this.err
204+
func (o *Outcome) Result() (interface{}, error) {
205+
return o.val, o.err
206206
}
207207

208208
// Error returns a string representation of the Outcome if it is in an error condition,
209209
// or an empty string if no error or panic occurred. Note that the Try-ed function
210210
// returning a non-nil error does not constitute an error condition for the Outcome.
211211
// That error value can be retrieved via Err or Result.
212212
// This is also useful for satisfying the `error` interface.
213-
func (this *Outcome) Error() string {
214-
if this.level == OK {
213+
func (o *Outcome) Error() string {
214+
if o.level == OK {
215215
return ""
216216
}
217-
if this.code != 0 {
218-
return this.text + fmt.Sprintf(" (code: 0x%04x)", this.code)
217+
if o.code != 0 {
218+
return o.text + fmt.Sprintf(" (code: 0x%04x)", o.code)
219219
}
220-
return this.text
220+
return o.text
221221
}

calmly_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ type mockLogger struct {
2424
log string
2525
}
2626

27-
func (this *mockLogger) Print(s ...interface{}) {
28-
this.log += fmt.Sprintln(s...)
27+
func (ml *mockLogger) Print(s ...interface{}) {
28+
ml.log += fmt.Sprintln(s...)
2929
}
30-
func (this *mockLogger) Fatal(s ...interface{}) {
31-
this.log += "[FATAL] " + fmt.Sprintln(s...)
30+
func (ml *mockLogger) Fatal(s ...interface{}) {
31+
ml.log += "[FATAL] " + fmt.Sprintln(s...)
3232
}
33-
func (this *mockLogger) Panic(s ...interface{}) {
34-
this.log += "[PANIC] " + fmt.Sprintln(s...)
33+
func (ml *mockLogger) Panic(s ...interface{}) {
34+
ml.log += "[PANIC] " + fmt.Sprintln(s...)
3535
}
3636

3737
func TestLevelNames(t *testing.T) {
@@ -131,7 +131,7 @@ func TestStack(t *testing.T) {
131131
}
132132
divByZero := func() {
133133
a, b := 1, 0
134-
a /= b
134+
_ = a / b
135135
}
136136

137137
out := Try(divByZero)
@@ -213,7 +213,7 @@ func TestStack(t *testing.T) {
213213
t.Errorf(`Try(goodFunc).Err() = %v, want %v`, oe, nil)
214214
}
215215
if orv, ore := out.Result(); orv != ov || ore != oe {
216-
t.Errorf(`Try(goodFunc).Result() should equal (Try(goodFunc).Value(), Try(goodFunc).Err()); got (%v, %v != %v, $v)`, orv, ore, ov, oe)
216+
t.Errorf(`Try(goodFunc).Result() should equal (Try(goodFunc).Value(), Try(goodFunc).Err()); got (%v, %v != %v, %v)`, orv, ore, ov, oe)
217217
}
218218
if oes, exp := out.Error(), ""; oes != exp {
219219
t.Errorf(`Try(goodFunc).Error() = %q, want %q`, oes, exp)
@@ -246,7 +246,7 @@ func TestStack(t *testing.T) {
246246
t.Errorf(`Try(badFunc).Err() = %v, want %v`, oe, nil)
247247
}
248248
if orv, ore := out.Result(); orv != ov || ore != oe {
249-
t.Errorf(`Try(badFunc).Result() should equal (Try(badFunc).Value(), Try(badFunc).Err()); got (%v, %v != %v, $v)`, orv, ore, ov, oe)
249+
t.Errorf(`Try(badFunc).Result() should equal (Try(badFunc).Value(), Try(badFunc).Err()); got (%v, %v != %v, %v)`, orv, ore, ov, oe)
250250
}
251251
if oes, exp := out.Error(), ot; oes != exp {
252252
t.Errorf(`Try(badFunc).Error() = %q, want %q`, oes, exp)

helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func levelName(l int8) string {
4343
return "?"
4444
}
4545

46+
// Logger defines the interface expected by the Log method of Outcome
4647
type Logger interface {
4748
Fatal(...interface{})
4849
Panic(...interface{})

0 commit comments

Comments
 (0)