Skip to content

Can't handle exceptions on iOS in Net::Session#get #48

@jlmuir

Description

@jlmuir

I have an iOS app where the user supplies a base URL for a service, and the app makes HTTP requests against the service via Flow's Net module. The app will crash with a RuntimeError while making an HTTP request if a problem occurs such as: the URL contains a host part that does not resolve in DNS, a connection to the service could not be established, etc. There seems to be no way to handle exceptions related to making the HTTP request.

To reproduce:

$ motion create Hello
$ cd Hello
$ echo "gem 'motion-flow'" >> Gemfile

Add the following (which contains a bad host part (i.e., a trailing x) in the base_url_supplied_by_user value) to AppDelegate#application:didFinishLaunchingWithOptions: after @window.makeKeyAndVisible:

base_url_supplied_by_user = 'https://httpbin.orgx'
session = Net.build(base_url_supplied_by_user) {}
begin
  session.get("/ip") do |response|
    if response.status == 200
      UI.alert({title: 'Client IP Address', message: response.body['origin'], default: 'OK'}) {}
    else
      UI.alert({title: 'HTTP Error', message: response.status_message, default: 'OK'}) {}
    end
  end
rescue RuntimeError => e
  UI.alert({title: 'Error', message: e.message, default: 'OK'}) {}
end

Then run it in a simulator:

$ rake

This results in a crash with the following message:

The operation couldn't be completed. (kCFErrorDomainCFNetwork error 310.) (RuntimeError)

Similarly, if no HTTP server is running on port 80 on the loopback device, changing the value of base_url_supplied_by_user to 'http://127.0.0.1' results in a crash with the following message:

Could not connect to the server. (RuntimeError)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions