77ENV [ "RAILS_ENV" ] = "development"
88
99require "action_controller"
10+ require "rack/cors"
1011
1112class RailsMiniApp < Rails ::Application
1213 config . hosts = nil
@@ -17,6 +18,18 @@ class RailsMiniApp < Rails::Application
1718 config . api_only = true
1819 config . force_ssl = false
1920
21+ # Add CORS middleware using Rack::Cors
22+ config . middleware . insert_before 0 , Rack ::Cors do
23+ allow do
24+ origins '*'
25+ resource '*' ,
26+ headers : :any ,
27+ methods : :any ,
28+ expose : :any ,
29+ credentials : false
30+ end
31+ end
32+
2033 initializer :configure_sentry do
2134 Sentry . init do |config |
2235 config . dsn = ENV [ "SENTRY_DSN" ]
@@ -30,38 +43,29 @@ class RailsMiniApp < Rails::Application
3043 config . release = "sentry-ruby-rails-mini-#{ Time . now . utc } "
3144
3245 config . transport . transport_class = Sentry ::DebugTransport
33- config . sdk_debug_transport_log_file = "/workspace/sentry/log/sentry_debug_events.log"
3446 config . background_worker_threads = 0
3547 end
3648 end
3749end
3850
3951class ErrorController < ActionController ::Base
40- before_action :set_cors_headers
41-
4252 def error
53+ # Let the exception bubble up so Sentry Rails integration captures it with proper context
4354 result = 1 / 0
4455 render json : { result : result }
4556 end
4657
47- private
48-
49- def set_cors_headers
50- response . headers [ 'Access-Control-Allow-Origin' ] = '*'
51- response . headers [ 'Access-Control-Allow-Methods' ] = 'GET, POST, PUT, DELETE, OPTIONS'
52- response . headers [ 'Access-Control-Allow-Headers' ] = 'Content-Type, Authorization, sentry-trace, baggage'
58+ def options
59+ head :ok
5360 end
5461end
5562
5663class EventsController < ActionController ::Base
57- before_action :set_cors_headers
58-
5964 def health
6065 render json : {
6166 status : "ok" ,
6267 timestamp : Time . now . utc . iso8601 ,
63- sentry_initialized : Sentry . initialized? ,
64- log_file_writable : check_log_file_writable
68+ sentry_initialized : Sentry . initialized?
6569 }
6670 end
6771
@@ -70,20 +74,8 @@ def trace_headers
7074 render json : { headers : headers }
7175 end
7276
73- private
74-
75- def check_log_file_writable
76- log_file_path = "/workspace/sentry/log/sentry_debug_events.log"
77- File . writable? ( File . dirname ( log_file_path ) ) &&
78- ( !File . exist? ( log_file_path ) || File . writable? ( log_file_path ) )
79- rescue
80- false
81- end
82-
83- def set_cors_headers
84- response . headers [ 'Access-Control-Allow-Origin' ] = '*'
85- response . headers [ 'Access-Control-Allow-Methods' ] = 'GET, POST, PUT, DELETE, OPTIONS'
86- response . headers [ 'Access-Control-Allow-Headers' ] = 'Content-Type, Authorization, sentry-trace, baggage'
77+ def options
78+ head :ok
8779 end
8880end
8981
@@ -94,7 +86,12 @@ def set_cors_headers
9486 get '/error' , to : 'error#error'
9587 get '/trace_headers' , to : 'events#trace_headers'
9688
97- # Add CORS headers for cross-origin requests from JS app
89+ # Handle OPTIONS preflight requests explicitly
90+ options '/health' , to : 'events#options'
91+ options '/error' , to : 'error#options'
92+ options '/trace_headers' , to : 'events#options'
93+
94+ # Catch-all OPTIONS handler for any other paths
9895 match '*path' , to : proc { |env |
9996 [ 200 , {
10097 'Access-Control-Allow-Origin' => '*' ,
0 commit comments