Class: Notion::Http::Middlewares::RaiseError

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/notion/http/middlewares/raise_error.rb

Overview

A middleware that raise Notion::Errors::HttpError

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/notion/http/middlewares/raise_error.rb', line 22

def call(env)
  super
rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed
  raise Notion::Errors::TimeoutError.new("timeout_error", "A unified client error for timeouts")
rescue ::Faraday::ParsingError
  parsing_error_message = "Raised by middlewares that parse the response, like the JSON response middleware."
  raise Notion::Errors::ParsingError.new("parsing_error", parsing_error_message, env.response)
end

#on_complete(env) ⇒ Object

Raises:

  • (error_class)


8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/notion/http/middlewares/raise_error.rb', line 8

def on_complete(env)
  return if env.success?

  body = env.body
  return unless body

  error_code = body["code"]
  error_message = body["message"]

  error_class = Notion::Errors::NOTION_ERROR_CODES[error_code]
  error_class ||= Notion::Errors::HttpError
  raise error_class.new(error_code, error_message, env.response)
end