Skip to content

Commit 245260f

Browse files
committed
Override Sentry.capture_exception in specs
There are many places in the code where `StandardError` is rescued and the exception is reported to Sentry using `Sentry.capture_exception`, but the exception itself is then swallowed. I'm not totally convinced this is sensible even in production, because `StandardError` covers a multitude of exception types. However, it's definitely problematic in development and test environments where Sentry is typically not configured, so there's no way to see what exceptions might have been swallowed. We still have at least one flakey spec example [1] and that calls `SchoolTeacher::Invite.call` which includes one of the rescue blocks [2] mentioned above. The flakey example fails when the method returns an `OperationResponse` instance for which `OperationResponse#success?` returns `false`. And the most obvious reason for that to happen is if `TeacherInvitation.create!` raises an exception (e.g. because of a validation error). I'd like to get visibility of this exception when the example fails in a CircleCI build. This commit patches the `Sentry.capture_exception` method when Sentry is not configured to report errors (i.e. typically in development and test environments) to display the exception and stacktrace in stderr as a warning. It might be better to add a custom error reporter module (or even use the error reporter built into Rails [3]) and use that throughout the code, but that would have meant widespread changes to the code, so I think this is probably better for now to see whether it's a useful approach. [1]: https://github.com/RaspberryPiFoundation/editor-api/blob/f397e870f2a33cce1f53b9104c52314f5233572c/spec/concepts/school_teacher/invite_spec.rb#L14-L17 [2]: https://github.com/RaspberryPiFoundation/editor-api/blob/f397e870f2a33cce1f53b9104c52314f5233572c/lib/concepts/school_teacher/invite.rb#L10-L14 [3]: https://guides.rubyonrails.org/v7.1/error_reporting.html
1 parent 0268e83 commit 245260f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

config/initializers/sentry.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@
77

88
config.traces_sample_rate = 0.5
99
end
10+
11+
module Sentry
12+
module Overrides
13+
def capture_exception(exception, **options, &)
14+
warn "[Sentry stub] #{exception.class}: #{exception.message}"
15+
warn exception.backtrace.join("\n") if exception.backtrace
16+
super
17+
end
18+
end
19+
end
20+
21+
Sentry.singleton_class.prepend(Sentry::Overrides) unless Sentry.configuration.sending_allowed?

0 commit comments

Comments
 (0)