Module: GdsApi::ExceptionHandling

Included in:
JsonClient
Defined in:
lib/gds_api/exceptions.rb

Instance Method Summary collapse

Instance Method Details

#build_specific_http_error(error, url, details = nil, request_body = nil) ⇒ Object



65
66
67
68
69
# File 'lib/gds_api/exceptions.rb', line 65

def build_specific_http_error(error, url, details = nil, request_body = nil)
  message = "URL: #{url}\nResponse body:\n#{error.http_body}\n\nRequest body:\n#{request_body}"
  code = error.http_code
  error_class_for_code(code).new(code, message, details)
end

#error_class_for_code(code) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gds_api/exceptions.rb', line 71

def error_class_for_code(code)
  case code
  when 401
    GdsApi::HTTPUnauthorized
  when 403
    GdsApi::HTTPForbidden
  when 404
    GdsApi::HTTPNotFound
  when 409
    GdsApi::HTTPConflict
  when 410
    GdsApi::HTTPGone
  when 422
    GdsApi::HTTPUnprocessableEntity
  when (400..499)
    GdsApi::HTTPClientError
  when (500..599)
    GdsApi::HTTPServerError
  else
    GdsApi::HTTPErrorResponse
  end
end

#ignoring(exception_or_exceptions) ⇒ Object



54
55
56
57
58
59
# File 'lib/gds_api/exceptions.rb', line 54

def ignoring(exception_or_exceptions)
  yield
rescue *exception_or_exceptions
  # Discard the exception
  nil
end

#ignoring_missing(&block) ⇒ Object



61
62
63
# File 'lib/gds_api/exceptions.rb', line 61

def ignoring_missing(&block)
  ignoring([HTTPNotFound, HTTPGone], &block)
end