fluyenta-ruby 0.1.15 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 75ca1f2c2598c130631334f3dec5b1a282c429e3d826303b4ea7ec19650a4027
4
- data.tar.gz: a12753631b95878565d3718d8ba5afe6e72a01a5df225b8d08ac7c0f6edf5471
3
+ metadata.gz: 5ff0fdda08335c6c73917352efc84c2321cc5c82aebb032a489fa51aa4c8c89d
4
+ data.tar.gz: 074a432171c594e5d98a06ff0706155f8395d6047500d3922f174077bada8197
5
5
  SHA512:
6
- metadata.gz: 8d39569edb9136b4f3ea2553f6f222339c0d602de213c4803ebb66c39b1f9bf4744ab253c8bd9863ce8374ab35cdf095c45d4e7882a5ce10eccdbe82de40266d
7
- data.tar.gz: f9f180f1ae570fe10e6705b52266e477866b829eec1061755f9380c856e9cc2450c3cbfd5bfa5ccc4be8ede83818fdb146886a8790a93edd7ac45d986f175da0
6
+ metadata.gz: 4f52256347141f0a27b937400a7fddf9456b1e4989be1e9412cf6cd7beaf7a6f924872bd8e66f8cb54c2c912a9e2cc8eb0daa769699a3d64ab2b566bf2b1d27f
7
+ data.tar.gz: 2cb8f11747ba0593974698635b72dd44636e0e5bd7b4f5f3ab28722739219cdd6c4f5814e6668ced5689b70c8a97f95c4b471715240e0e9ce1ef9d8cf5b6ecf5
@@ -18,6 +18,7 @@ module BrainzLab
18
18
 
19
19
  def call(env)
20
20
  return @app.call(env) unless DevTools.enabled?
21
+ return @app.call(env) if env['REQUEST_METHOD'] == 'OPTIONS'
21
22
 
22
23
  path = env['PATH_INFO']
23
24
  asset_prefix = DevTools.asset_path
@@ -38,6 +38,7 @@ module BrainzLab
38
38
  return false unless DevTools.debug_panel_enabled?
39
39
  return false unless DevTools.allowed_environment?
40
40
  return false unless DevTools.allowed_ip?(extract_ip(env))
41
+ return false if env['REQUEST_METHOD'] == 'OPTIONS'
41
42
  return false if asset_request?(env['PATH_INFO'])
42
43
  return false if devtools_asset_request?(env['PATH_INFO'])
43
44
  return false if turbo_stream_request?(env)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
4
+
3
5
  module BrainzLab
4
6
  module DevTools
5
7
  module Middleware
@@ -10,29 +12,33 @@ module BrainzLab
10
12
  end
11
13
 
12
14
  def call(env)
15
+ return @app.call(env) if env['REQUEST_METHOD'] == 'OPTIONS'
13
16
  return @app.call(env) unless should_handle?(env)
14
17
 
15
18
  begin
16
19
  status, headers, body = @app.call(env)
17
20
 
18
21
  # Check if this is an error response that we should intercept
19
- if status >= 400 && html_response?(headers) && !json_request?(env)
22
+ if status >= 400 && html_response?(headers) && !json_request?(env) && !api_path?(env)
20
23
  # Check if this looks like Rails' default error page
21
24
  body_content = collect_body(body)
22
25
  if body_content.include?('Action Controller: Exception caught') || body_content.include?('background: #C00')
23
26
  # Extract exception info from the page
24
27
  exception_info = extract_exception_from_html(body_content)
25
28
  if exception_info
26
- data = collect_debug_data_from_info(env, exception_info)
27
- return render_error_page_from_info(exception_info, data)
29
+ data = collect_debug_data_from_info(env, exception_info, status)
30
+ return render_error_page_from_info(exception_info, data, status)
28
31
  end
29
32
  end
30
33
  end
31
34
 
32
35
  [status, headers, body]
33
36
  rescue Exception => e
34
- # Don't intercept if request wants JSON
35
- return raise_exception(e) if json_request?(env)
37
+ # For JSON/API requests, return a proper JSON error response
38
+ if json_request?(env) || api_path?(env)
39
+ capture_to_reflex(e)
40
+ return json_error_response(e)
41
+ end
36
42
 
37
43
  # Still capture to Reflex if available
38
44
  capture_to_reflex(e)
@@ -87,7 +93,7 @@ module BrainzLab
87
93
  .gsub(' ', ' ')
88
94
  end
89
95
 
90
- def collect_debug_data_from_info(env, info)
96
+ def collect_debug_data_from_info(env, info, status = 500)
91
97
  context = defined?(BrainzLab::Context) ? BrainzLab::Context.current : nil
92
98
  collector_data = Data::Collector.get_request_data
93
99
 
@@ -113,7 +119,7 @@ module BrainzLab
113
119
  }
114
120
  end
115
121
 
116
- def render_error_page_from_info(info, data)
122
+ def render_error_page_from_info(info, data, status = 500)
117
123
  # Create a simple exception-like object
118
124
  exception = StandardError.new(info[:message])
119
125
  exception.define_singleton_method(:class) do
@@ -126,7 +132,7 @@ module BrainzLab
126
132
  html = @renderer.render(exception, data)
127
133
 
128
134
  [
129
- 500,
135
+ status,
130
136
  {
131
137
  'Content-Type' => 'text/html; charset=utf-8',
132
138
  'Content-Length' => html.bytesize.to_s,
@@ -169,6 +175,48 @@ module BrainzLab
169
175
  env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'
170
176
  end
171
177
 
178
+ def api_path?(env)
179
+ path = env['PATH_INFO'] || ''
180
+ path.start_with?('/api/')
181
+ end
182
+
183
+ def exception_to_status(exception)
184
+ case exception.class.name
185
+ when 'ActionController::RoutingError', 'AbstractController::ActionNotFound'
186
+ 404
187
+ when 'ActionController::MethodNotAllowed'
188
+ 405
189
+ when 'ActionController::BadRequest', 'ActionDispatch::Http::Parameters::ParseError'
190
+ 400
191
+ when 'ActionController::UnknownFormat'
192
+ 406
193
+ else
194
+ 500
195
+ end
196
+ end
197
+
198
+ def json_error_response(exception)
199
+ status_code = exception_to_status(exception)
200
+ message = case status_code
201
+ when 400 then 'Bad request'
202
+ when 404 then 'Not found'
203
+ when 405 then 'Method not allowed'
204
+ when 406 then 'Not acceptable'
205
+ else 'Internal server error'
206
+ end
207
+
208
+ body = JSON.generate({ error: message })
209
+ [
210
+ status_code,
211
+ {
212
+ 'Content-Type' => 'application/json; charset=utf-8',
213
+ 'Content-Length' => body.bytesize.to_s,
214
+ 'X-Content-Type-Options' => 'nosniff'
215
+ },
216
+ [body]
217
+ ]
218
+ end
219
+
172
220
  def capture_to_reflex(exception)
173
221
  return unless defined?(BrainzLab::Reflex)
174
222
 
@@ -76,7 +76,7 @@ module BrainzLab
76
76
 
77
77
  @client.send_batch(events: events, metrics: metrics)
78
78
  rescue StandardError => e
79
- BrainzLab.debug("[Flux] Batch send failed: #{e.message}")
79
+ BrainzLab.debug_log("[Flux] Batch send failed: #{e.message}")
80
80
  end
81
81
 
82
82
  def start_flush_thread
@@ -86,7 +86,7 @@ module BrainzLab
86
86
  begin
87
87
  flush! if size.positive?
88
88
  rescue StandardError => e
89
- BrainzLab.debug("[Flux] Flush thread error: #{e.message}")
89
+ BrainzLab.debug_log("[Flux] Flush thread error: #{e.message}")
90
90
  end
91
91
  end
92
92
  end
@@ -48,11 +48,11 @@ module BrainzLab
48
48
 
49
49
  response = http.request(request)
50
50
 
51
- BrainzLab.debug("[Flux] Request failed: #{response.code} - #{response.body}") unless response.is_a?(Net::HTTPSuccess)
51
+ BrainzLab.debug_log("[Flux] Request failed: #{response.code} - #{response.body}") unless response.is_a?(Net::HTTPSuccess)
52
52
 
53
53
  response
54
54
  rescue StandardError => e
55
- BrainzLab.debug("[Flux] Request error: #{e.message}")
55
+ BrainzLab.debug_log("[Flux] Request error: #{e.message}")
56
56
  nil
57
57
  end
58
58
 
@@ -223,7 +223,11 @@ module BrainzLab
223
223
  context.request_method = request.request_method
224
224
  context.request_path = request.path
225
225
  context.request_url = request.url
226
- context.request_params = filter_params(request.params.to_h)
226
+ context.request_params = begin
227
+ filter_params(request.params.to_h)
228
+ rescue ActionDispatch::Http::Parameters::ParseError
229
+ {}
230
+ end
227
231
  context.request_headers = extract_headers(env)
228
232
 
229
233
  # Add breadcrumb for request start
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BrainzLab
4
- VERSION = '0.1.15'
4
+ VERSION = '0.1.17'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluyenta-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.15
4
+ version: 0.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fluyenta