Skip to content

Commit 77cd2e1

Browse files
committed
Only subscribe to Rails.error if enabled
The previous behavior could lead to strange errors if solid_errors is not configured in the given environment. The error reporter would try to write to solid_errors table, but the table is not available in the default database. With that if statement in place, the subscriber will only be added, if solid_errors is enabled for the environment.
1 parent 2ba4fd2 commit 77cd2e1

4 files changed

Lines changed: 79 additions & 2 deletions

File tree

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ end
153153

154154
### Configuration
155155

156-
You can configure Solid Errors via the Rails configuration object, under the `solid_errors` key. Currently, 6 configuration options are available:
156+
You can configure Solid Errors via the Rails configuration object, under the `solid_errors` key. Currently, the following configuration options are available:
157157

158+
* `enabled` - Enable or disable error tracking. Defaults to `true`. See [Disabling error tracking](#disabling-error-tracking) for more information.
158159
* `connects_to` - The database configuration to use for the Solid Errors database. See [Database Configuration](#database-configuration) for more information.
159160
* `username` - The username to use for HTTP authentication. See [Authentication](#authentication) for more information.
160161
* `password` - The password to use for HTTP authentication. See [Authentication](#authentication) for more information.
@@ -191,6 +192,17 @@ Running Solid Errors in a separate database is recommended, but it's also possib
191192

192193
You won't have multiple databases, so `database.yml` doesn't need to have the errors database configuration.
193194

195+
#### Disabling error tracking
196+
197+
Solid Errors is enabled by default. If you want to disable error tracking in a specific environment, you can set `enabled` to `false`:
198+
199+
```ruby
200+
# config/environments/development.rb
201+
config.solid_errors.enabled = false
202+
```
203+
204+
When disabled, Solid Errors will not subscribe to the Rails error reporter and no errors will be recorded.
205+
194206
#### Authentication
195207

196208
Solid Errors does not restrict access out of the box. You must secure the dashboard yourself. However, it does provide basic HTTP authentication that can be used with basic authentication or Devise. All you need to do is setup a username and password.

lib/solid_errors.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_relative "solid_errors/engine"
77

88
module SolidErrors
9+
mattr_accessor :enabled, default: true
910
mattr_accessor :connects_to
1011
mattr_accessor :base_controller_class, default: "::ActionController::Base"
1112
mattr_writer :username

lib/solid_errors/engine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Engine < ::Rails::Engine
1818
end
1919

2020
initializer "solid_errors.active_record.error_subscriber" do
21-
Rails.error.subscribe(SolidErrors::Subscriber.new)
21+
Rails.error.subscribe(SolidErrors::Subscriber.new) if SolidErrors.enabled
2222
end
2323
end
2424
end

test/engine_test.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class EngineTest < Minitest::Test
6+
def setup
7+
@config = Rails.application.config
8+
@original_config_enabled = @config.solid_errors.enabled
9+
@original_module_enabled = SolidErrors.enabled
10+
@subscriber = nil
11+
12+
# Unsubscribe any existing SolidErrors subscribers from boot
13+
unsubscribe_all_solid_errors_subscribers
14+
end
15+
16+
def teardown
17+
Rails.error.unsubscribe(@subscriber) if @subscriber
18+
@config.solid_errors.enabled = @original_config_enabled
19+
SolidErrors.enabled = @original_module_enabled
20+
21+
# Re-register subscriber if it was originally enabled
22+
if @original_module_enabled
23+
Rails.error.subscribe(SolidErrors::Subscriber.new)
24+
end
25+
end
26+
27+
def test_subscriber_is_enabled_when_enabled_is_true
28+
@config.solid_errors.enabled = true
29+
30+
run_subscriber_initializer
31+
32+
assert solid_errors_subscriber, "Subscriber should be registered when enabled is true"
33+
end
34+
35+
def test_subscriber_is_disabled_when_enabled_is_false
36+
@config.solid_errors.enabled = false
37+
38+
run_subscriber_initializer
39+
40+
refute solid_errors_subscriber, "Subscriber should not be registered when enabled is false"
41+
end
42+
43+
private
44+
45+
def run_subscriber_initializer
46+
config_initializer = SolidErrors::Engine.initializers.find { |i| i.name == "solid_errors.config" }
47+
SolidErrors::Engine.instance_exec(Rails.application, &config_initializer.block)
48+
49+
subscriber_initializer = SolidErrors::Engine.initializers.find { |i| i.name == "solid_errors.active_record.error_subscriber" }
50+
SolidErrors::Engine.instance_exec(Rails.application, &subscriber_initializer.block)
51+
end
52+
53+
def solid_errors_subscriber
54+
subscribers = Rails.error.instance_variable_get(:@subscribers)
55+
@subscriber = subscribers.find { |s| s.is_a?(SolidErrors::Subscriber) }
56+
end
57+
58+
def unsubscribe_all_solid_errors_subscribers
59+
subscribers = Rails.error.instance_variable_get(:@subscribers)
60+
subscribers.select { |s| s.is_a?(SolidErrors::Subscriber) }.each do |s|
61+
Rails.error.unsubscribe(s)
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)