Class: FmRest::V1::RaiseErrors

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/fmrest/v1/raise_errors.rb

Overview

FM Data API response middleware for raising exceptions on API response errors

Constant Summary collapse

ERROR_RANGES =
{
  -1         => APIError::UnknownError,
  100        => APIError::ResourceMissingError,
  101        => APIError::RecordMissingError,
  102..199   => APIError::ResourceMissingError,
  200..299   => APIError::AccountError,
  300..399   => APIError::LockError,
  400        => APIError::ParameterError,
  401        => APIError::NoMatchingRecordsError,
  402..499   => APIError::ParameterError,
  500..599   => APIError::ValidationError,
  800..899   => APIError::SystemError,
  952        => APIError::InvalidToken,
  953        => APIError::MaximumDataAPICallsExceeded,
  1200..1299 => APIError::ScriptError,
  1400..1499 => APIError::ODBCError
}.freeze

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fmrest/v1/raise_errors.rb', line 29

def on_complete(env)
  # Sometimes, especially when using FileMaker Cloud, a failed
  # authorization request will return a 401 (Unauthorized) with text/html
  # content-type instead of the regular JSON, so we need to catch it
  # manually here, emulating a regular account error
  if !(/\bjson$/ === env.response_headers["content-type"]) && env.status == 401
    raise FmRest::APIError::AccountError.new(212, "Authentication failed (HTTP 401: Unauthorized)")
  end

  # From this point on we want JSON
  return unless env.body.is_a?(Hash)

  # Sniff for either straight JSON parsing or Spyke's format
  if env.body[:metadata] && env.body[:metadata][:messages]
    check_errors(env.body[:metadata][:messages])
  elsif env.body["messages"]
    check_errors(env.body["messages"])
  end
end