Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faraday error handling #86

Open
mfenner opened this issue Nov 4, 2014 · 1 comment
Open

Faraday error handling #86

mfenner opened this issue Nov 4, 2014 · 1 comment
Labels

Comments

@mfenner
Copy link
Member

mfenner commented Nov 4, 2014

2014-10-29 12:44:39.386 [FATAL] Faraday::ParsingError (757: unexpected token at '<html><body><h1>504 Gateway Time-out</h1>
The server didn't respond in time.
</body></html>
'):
  app/models/search_crossref.rb:51:in `get'
  app/models/search_result.rb:16:in `from_crossref'
  app/models/search_result.rb:26:in `block in from_cache'
  app/models/search_result.rb:25:in `from_cache'
  app/controllers/reports_controller.rb:157:in `block in prepare_visualization_data'
  app/controllers/reports_controller.rb:156:in `prepare_visualization_data'
  app/controllers/reports_controller.rb:201:in `multiple_documents_visualizations'
  app/controllers/reports_controller.rb:99:in `visualizations'
  app/controllers/application_controller.rb:42:in `save_session_dois' (pid:25371)
Faraday::ParsingError (757: unexpected token at '<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
'):
  app/models/search_crossref.rb:51:in `get'
2014-10-29 11:39:14.393 [FATAL] MultiJson::ParseError (795: unexpected token at 'alihack<%eval request("alihack.com")%> '):
  json (1.8.1) lib/json/common.rb:155:in `parse'
@mfenner mfenner added the bug label Nov 4, 2014
@mfenner
Copy link
Member Author

mfenner commented Nov 4, 2014

I use this approach (https://github.com/articlemetrics/lagotto/blob/master/app/models/concerns/networkable.rb#L198-L216):

def parse_error_response(string)
      if is_json?(string)
        string = JSON.parse(string)
      elsif is_xml?(string)
        string = Hash.from_xml(string)
      end
      string = string['error'] if string.is_a?(Hash) && string['error']
      string
    end

    def is_xml?(string)
      Nokogiri::XML(string).errors.empty?
    end

    def is_json?(string)
      JSON.parse(string)
    rescue JSON::ParserError
      false
    end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant