From ea4c483e4142a0516ae35b1a06cd48770021b815 Mon Sep 17 00:00:00 2001 From: Andrew Newdigate Date: Fri, 23 Oct 2020 08:59:56 +0200 Subject: [PATCH] Abstract Capture behind an interface, allowing the implementation to be mocked out --- errortracking/capture.go | 26 +++++--------------------- errortracking/capturer.go | 8 ++++++++ errortracking/capturer_sentry.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 errortracking/capturer.go create mode 100644 errortracking/capturer_sentry.go diff --git a/errortracking/capture.go b/errortracking/capture.go index e1135594..240ec9d3 100644 --- a/errortracking/capture.go +++ b/errortracking/capture.go @@ -1,27 +1,11 @@ package errortracking -import ( - "reflect" - - "github.com/getsentry/sentry-go" -) +// DefaultCapturer is the default capture implementation for the errortracking package +// At present, no other implementations exist, but mock implementations can be +// injected in tests if neccessary +var DefaultCapturer = &sentryCapturer{} // Capture will report an error to the error reporting service func Capture(err error, opts ...CaptureOption) { - event := sentry.NewEvent() - event.Level = sentry.LevelError - - for _, v := range opts { - v(event) - } - - event.Exception = []sentry.Exception{ - { - Type: reflect.TypeOf(err).String(), - Value: err.Error(), - Stacktrace: sentry.ExtractStacktrace(err), - }, - } - - sentry.CaptureEvent(event) + DefaultCapturer.Capture(err, opts...) } diff --git a/errortracking/capturer.go b/errortracking/capturer.go new file mode 100644 index 00000000..f37c077a --- /dev/null +++ b/errortracking/capturer.go @@ -0,0 +1,8 @@ +package errortracking + +// Capturer is an interface for capturing and reporting errors to LabKits +// error tracking system +type Capturer interface { + // Capture will report an error to the error reporting service + Capture(err error, opts ...CaptureOption) +} diff --git a/errortracking/capturer_sentry.go b/errortracking/capturer_sentry.go new file mode 100644 index 00000000..643ac01e --- /dev/null +++ b/errortracking/capturer_sentry.go @@ -0,0 +1,30 @@ +package errortracking + +import ( + "reflect" + + "github.com/getsentry/sentry-go" +) + +// sentryCapturer is an implementation of Capturer that relies on Sentry as its backend +type sentryCapturer struct { +} + +func (c *sentryCapturer) Capture(err error, opts ...CaptureOption) { + event := sentry.NewEvent() + event.Level = sentry.LevelError + + for _, v := range opts { + v(event) + } + + event.Exception = []sentry.Exception{ + { + Type: reflect.TypeOf(err).String(), + Value: err.Error(), + Stacktrace: sentry.ExtractStacktrace(err), + }, + } + + sentry.CaptureEvent(event) +} -- GitLab