-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the functional options pattern to configure interceptor
Also adresse other minor/style issues.
- Loading branch information
Showing
3 changed files
with
92 additions
and
62 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,63 @@ | ||
package sentrygrpc | ||
|
||
import ( | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type options struct { | ||
repanic bool | ||
reportOn ReportOn | ||
} | ||
|
||
func buildOptions(ff ...Option) options { | ||
opts := options{ | ||
reportOn: ReportAlways, | ||
} | ||
|
||
for _, f := range ff { | ||
f(&opts) | ||
} | ||
|
||
return opts | ||
} | ||
|
||
// Option configures reporting behavior. | ||
type Option func(*options) | ||
|
||
// WithRepanic configures whether to panic again after recovering from | ||
// a panic. Use this option if you have other panic handlers. | ||
func WithRepanic(b bool) Option { | ||
return func(o *options) { | ||
o.repanic = b | ||
} | ||
} | ||
|
||
// WithReportOn configures whether to report on errors. | ||
func WithReportOn(r ReportOn) Option { | ||
return func(o *options) { | ||
o.reportOn = r | ||
} | ||
} | ||
|
||
// ReportOn decides error should be reported to sentry. | ||
type ReportOn func(error) bool | ||
|
||
// ReportAlways returns true if err is non-nil. | ||
func ReportAlways(err error) bool { | ||
return err != nil | ||
} | ||
|
||
// ReportOnCodes returns true if error code matches on of the given codes. | ||
func ReportOnCodes(cc ...codes.Code) ReportOn { | ||
return func(err error) bool { | ||
c := status.Code(err) | ||
for i := range cc { | ||
if c == cc[i] { | ||
return true | ||
} | ||
} | ||
|
||
return 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
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