Class: Otto::RouteHandlers::InstanceMethodHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/otto/route_handlers.rb

Overview

Handler for instance methods (existing Otto pattern) Maintains backward compatibility for Controller#action patterns

Instance Attribute Summary

Attributes inherited from BaseHandler

#otto_instance, #route_definition

Instance Method Summary collapse

Methods inherited from BaseHandler

#initialize

Constructor Details

This class inherits a constructor from Otto::RouteHandlers::BaseHandler

Instance Method Details

#call(env, extra_params = {}) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/otto/route_handlers.rb', line 218

def call(env, extra_params = {})
  req = Rack::Request.new(env)
  res = Rack::Response.new

  begin
    # Apply the same extensions and processing as original Route#call
    setup_request_response(req, res, env, extra_params)

    # Create instance and call method (existing Otto behavior)
    instance = target_class.new(req, res)
    result = instance.send(route_definition.method_name)

    # Only handle response if response_type is not default
    if route_definition.response_type != 'default'
      handle_response(result, res, {
        instance: instance,
        request: req
      })
    end

  rescue => e
    # Error handling - return 500 with proper headers like main Otto error handler
    error_id = SecureRandom.hex(8)
    Otto.logger.error "[#{error_id}] #{e.class}: #{e.message}"
    Otto.logger.debug "[#{error_id}] Backtrace: #{e.backtrace.join("\n")}" if Otto.debug

    res.status = 500
    res.headers['content-type'] = 'text/plain'

    if Otto.env?(:dev, :development)
      res.write "Server error (ID: #{error_id}). Check logs for details."
    else
      res.write "An error occurred. Please try again later."
    end

    # Add security headers if available
    if otto_instance&.respond_to?(:security_config) && otto_instance.security_config
      otto_instance.security_config.security_headers.each do |header, value|
        res.headers[header] = value
      end
    end
  end

  finalize_response(res)
end