Module: Ant::Server::GrapeDecorator
- Defined in:
- lib/ant/server/grape.rb
Overview
Descorator for using grape with ant. This will implement:
-
exception handling
-
logs
-
JSend format
Constant Summary collapse
- HTTP_CODES =
{ success: 200, fail: 400, error: 500, fatal: 500 }.freeze
Class Method Summary collapse
- .configure_custom_exceptions(base) ⇒ Object
-
.configure_global_exception_handler(base) ⇒ Object
:nocov: #.
-
.configure_grape_exceptions(base) ⇒ Object
:nocov: #.
- .configure_handlers(base) ⇒ Object
- .configure_logger(base) ⇒ Object
- .extract_http_code(exception, level) ⇒ Object
- .handler ⇒ Object
- .included(base) ⇒ Object
Class Method Details
.configure_custom_exceptions(base) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/ant/server/grape.rb', line 31 def self.configure_custom_exceptions(base) Server::Response.resources(:exceptions).each do |exception_class, level| base.rescue_from(exception_class) do |ex| response = Ant::Server::GrapeDecorator.handler.call(env, level, ex) http_code = Ant::Server::GrapeDecorator.extract_http_code(ex, level) error!(response, http_code) end end end |
.configure_global_exception_handler(base) ⇒ Object
:nocov: #
52 53 54 55 56 57 58 59 |
# File 'lib/ant/server/grape.rb', line 52 def self.configure_global_exception_handler(base) base.rescue_from(:all) do |ex| level = :fatal response = Ant::Server::GrapeDecorator.handler.call(env, level, ex) http_code = Ant::Server::GrapeDecorator.extract_http_code(ex, level) error!(response, http_code) end end |
.configure_grape_exceptions(base) ⇒ Object
:nocov: #
42 43 44 45 46 47 48 49 |
# File 'lib/ant/server/grape.rb', line 42 def self.configure_grape_exceptions(base) base.rescue_from(Grape::Exceptions::Base) do |ex| ant_ex = Ant::Exceptions::AntFail.new(ex.) response = Ant::Server::GrapeDecorator .handler.call(env, :fail, ant_ex) error!(response, 400) end end |
.configure_handlers(base) ⇒ Object
61 62 63 64 65 |
# File 'lib/ant/server/grape.rb', line 61 def self.configure_handlers(base) configure_custom_exceptions(base) configure_grape_exceptions(base) configure_global_exception_handler(base) end |
.configure_logger(base) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ant/server/grape.rb', line 77 def self.configure_logger(base) base.before do params[:__init_time] = Time.now end base.after do params = env['api.endpoint'].params request = env['api.endpoint'].request pkg = RequestResponse.new(request: request, params: params) Server::Response.logger.access(pkg) end end |
.extract_http_code(exception, level) ⇒ Object
26 27 28 29 |
# File 'lib/ant/server/grape.rb', line 26 def self.extract_http_code(exception, level) default = HTTP_CODES[level] || 500 exception.respond_to?(:http_code) ? exception.http_code : default end |
.handler ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ant/server/grape.rb', line 11 def self.handler lambda do |env, level, ex| params = env['api.endpoint'].params request = env['api.endpoint'].request pkg = RequestResponse.new(request: request, params: params) pkg.exception = ex Server::Response.logger.send(level, pkg) Server::Response.format.send(level, pkg) end end |
.included(base) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/ant/server/grape.rb', line 67 def self.included(base) base.formatter(:json, lambda do |response, _| pkg = RequestResponse.new(request: {}, params: {}) pkg.result = response Server::Response.format.send(:success, pkg).to_json end) configure_logger(base) configure_handlers(base) end |