Class: BaseCRM::Middlewares::RaiseError
- Inherits:
-
Faraday::Response::Middleware
- Object
- Faraday::Response::Middleware
- BaseCRM::Middlewares::RaiseError
- Defined in:
- lib/basecrm/middlewares/raise_error.rb
Instance Method Summary collapse
Instance Method Details
#errors_collection(env, error_klass) ⇒ Object
26 27 28 29 30 |
# File 'lib/basecrm/middlewares/raise_error.rb', line 26 def errors_collection(env, error_klass) envelope = JSON.parse(env[:body], symbolize_names: true) errors = envelope[:errors] ErrorsCollection.new(errors.map { |e| error_klass.new(e) }, envelope[:meta]) end |
#on_complete(env) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/basecrm/middlewares/raise_error.rb', line 5 def on_complete(env) status = env[:status] return if (200...300).member?(status) content_type = env[:response_headers]['content-type'] error_klass = case status when 422 then BaseCRM::ResourceError when 400...500 then BaseCRM::RequestError when 500...600 then BaseCRM::ServerError end if !content_type.nil? && content_type.include?('json') raise errors_collection(env, error_klass) else error = {:error=>{:code=>status, :message=>"Unknown error occurred."}, :meta=>{}} raise error_klass.new(error) end end |