Class: ProxES::Middleware::ErrorHandling
- Inherits:
-
Object
- Object
- ProxES::Middleware::ErrorHandling
- Includes:
- Wisper::Publisher
- Defined in:
- lib/proxes/middleware/error_handling.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#error(message, code = 500) ⇒ Object
Response Helpers.
-
#initialize(app, logger = nil) ⇒ ErrorHandling
constructor
A new instance of ErrorHandling.
- #log_not_authorized(request) ⇒ Object
- #login_and_redirect(request) ⇒ Object
- #redirect(destination, code = 302) ⇒ Object
Constructor Details
#initialize(app, logger = nil) ⇒ ErrorHandling
Returns a new instance of ErrorHandling.
14 15 16 17 |
# File 'lib/proxes/middleware/error_handling.rb', line 14 def initialize(app, logger = nil) @app = app @logger = logger || ::Ditty::Services::Logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/proxes/middleware/error_handling.rb', line 10 def logger @logger end |
Instance Method Details
#call(env) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/proxes/middleware/error_handling.rb', line 19 def call(env) request = ProxES::Request.from_env(env) response = @app.call env broadcast(:es_request_failed, request, response) unless (200..299).cover?(response[0]) response rescue Errno::EHOSTUNREACH error 'Could not reach Elasticsearch at ' + ENV['ELASTICSEARCH_URL'] rescue Errno::ECONNREFUSED, Faraday::ConnectionFailed, SocketError error 'Elasticsearch not listening at ' + ENV['ELASTICSEARCH_URL'] rescue Pundit::NotAuthorizedError, Ditty::Helpers::NotAuthenticated => e broadcast(:es_request_denied, request, e) request raise e if ENV['APP_ENV'] == 'development' return [401, {}, []] if request.head? request.html? && request.user.nil? ? login_and_redirect(request) : error('Not Authorized', 401) rescue StandardError => e broadcast(:es_request_denied, request, e) request raise e if ENV['APP_ENV'] == 'development' return [403, {}. []] if request.head? error 'Forbidden', 403 end |
#error(message, code = 500) ⇒ Object
Response Helpers
50 51 52 53 54 |
# File 'lib/proxes/middleware/error_handling.rb', line 50 def error(, code = 500) headers = { 'Content-Type' => 'application/json' } headers['WWW-Authenticate'] = 'Basic realm="Authorization Required"' if code == 401 [code, headers, ['{"error":"' + + '"}']] end |
#log_not_authorized(request) ⇒ Object
44 45 46 47 |
# File 'lib/proxes/middleware/error_handling.rb', line 44 def (request) user = request.user ? request.user.email : 'unauthenticated request' logger.error "Access denied for #{user} by security layer: #{request.detail} #{request.indices.join(',')}" end |
#login_and_redirect(request) ⇒ Object
56 57 58 59 |
# File 'lib/proxes/middleware/error_handling.rb', line 56 def login_and_redirect(request) request.session['omniauth.origin'] = request.url unless request.url == '/_proxes/auth/login' redirect '/_proxes/auth/login' end |
#redirect(destination, code = 302) ⇒ Object
61 62 63 |
# File 'lib/proxes/middleware/error_handling.rb', line 61 def redirect(destination, code = 302) [code, { 'Location' => destination }, []] end |