4

I'm working on a number of projects on cloud9 IDE, and it's really frustrating that I can't get the better errors gem to work correctly. It isn't supposed to need initializing; it should just work out of the box. However, I still only get the usual ugly red errors page. I should specify that it is included in my gemfile, and I have bundle install already.

How can I get better errors to work correctly? Is there an installation step I'm missing?

elersong
  • 756
  • 4
  • 12
  • 29

5 Answers5

5

The trick, I used, to get the 'better_errors' gem working in Cloud9 is setting the value of TRUSTED_IP to the public IP address of the computer my browser session is attached to. (As far as I can tell, it has nothing to do with the IP address of the remote server or Cloud9 server addresses.)

I'll outline the process I used to get 'better_errors' working on my Cloud9 workspace, from my Chromebook on my residential network... maybe it will also work for you and others!

  1. Add gem "better_errors" to the development group in the project Gemfile.
  2. Add gem "binding_of_caller" to the project Gemfile.
  3. Run $bundle in the project Cloud9 terminal.
  4. Edit the project config/environments/development.rb file and add the following line of code to the end of the Rails.application.configure block.

    BetterErrors::Middleware.allow_ip! ENV['TRUSTED_IP'] if ENV['TRUSTED_IP']
    
  5. Create a new "runner" in Cloud9 by clicking "Run" > "Run With" > "New Runner".
  6. Cloud9 creates an basic runner file in a new tab to modify. Replace the contents of this file with following code.

    {
       "cmd": [
         "bash",
         "--login",
         "-c",
         "TRUSTED_IP=XXX.XXX.XXX.XXX rails server -p $port -b $ip $args"
      ],
      "working_dir": "$project_path",
      "info": "Your code is running at \\033[01;34m$url\\033[00m.\n\\033[01;31m",
      "selector": "source.ru"
    }
    
  7. Replace XXX.XXX.XXX.XXX in the code above with the local computer's public IP address. (I use http://ifconfig.me/ to find the public IP assigned to my Chromebook.)
  8. Save the runner file with the name RoR.run into the /.c9/runners path for the project.
  9. Start the projects server by using this new runner. Click Run > Run With > RoR.
  10. Use the popup link that Cloud9 displays, after the runner starts the server, to view the app. Enjoy 'better_errors'!

NOTE: I still have not figured out how to automate the process of feeding the external IP address of my local computer into the RoR.run file that lives on the Cloud9 workspace. I just update it manually every time I move to a new network or my external IP address changes.

WARNING: I actually just started learning RoR, so I have no idea if this is the "correct" way to get this gem to work in a cloud dev server/service environment. I also have no idea how safe this would be. I suspect that my solution exposes the 'better_errors' in-browser REPL to all computers that resolve to that same external IP address. If you are working on a sensitive codebase/database please do not implement my solution.

Grokcodile
  • 166
  • 2
  • 5
  • I also like to have the console display on routing error pages when I'm working on Cloud9. To do this I add `config.web_console.whitelisted_ips = ENV['TRUSTED_IP']` to my projects **config/environments/development.rb** file. – Grokcodile Mar 27 '15 at 02:05
2

I just tested this in cloud9.io and this is the simplest way to make this work in cloud9.io:

Add the following line to config/environments/development.rb:

BetterErrors::Middleware.allow_ip! 'xxx.xxx.xxx.0/24'

where xxx.xxx.xxx is the first three sections of the IP address of the local machine that you are using to connect to cloud9.io

Praveen Angyan
  • 7,227
  • 29
  • 34
2

There is a good answer in the better errors issues and c0 docs.

Issues: https://github.com/charliesome/better_errors/issues/318

c9 Help https://community.c9.io/t/white-listing-remote-addr-for-better-errors-gem/4976/4

Use a Rack::Request object to get the IP. You can put the following code in your view.

   if Rails.env.development?
     request = Rack::Request.new(env)
     puts "@@@@@@ Request IP_ADDRESS = #{request.ip}"
   end

Change the last quadrant of the IP you get to 0/24. For example.

BetterErrors::Middleware.allow_ip! '76.168.69.0/24'     <--note: change the last quad to 0/24 and of course your ip address will be different than 76.168.69.xx
xander-miller
  • 529
  • 5
  • 12
2

Yeah!! I got it! Automatically! Here is my solution:

1- Similar as described by @Grokcodile: edit the project config/environments/development.rb file and add the following line of code to the Rails.application.configure block.

BetterErrors::Middleware.allow_ip! ENV['TRUSTED_IP'] if ENV['TRUSTED_IP']

config.web_console.whitelisted_ips = ENV['TRUSTED_IP']

2- At the Cloud9 edit the ~/.bashrc...

vi ~/.bashrc

add the line (enter, alt+a):

export TRUSTED_IP='0.0.0.0/0.0.0.0'

Save it (esc, :wq)

3- run rails s -b $IP -p $PORT as usual...

4- Enjoy better errors!!

If you also work on this project at a Virtual Machine(vagrant):

1- edit at your VM (vagrant) your ~/.bash_profile (my case) and add:

export TRUSTED_IP=x.x.x.x
export PORT=3000
export IP=0.0.0.0

x.x.x.x must be equal to the REMOTE_ADDR of ENV. (This in not a problem like cloud9 because at my VM the IP doesn't change everytime: 10.0.2.2 always for me).

With this I am now able to use the gem foreman: foreman start at both places with the Procfile:

web: rails s -b $IP -p $PORT

This works because the global env variables are set on both.

I am just starting to learn RoR too, so, hope this is the right thing to do without bringing more problems in the future.

Felipe Maion
  • 336
  • 1
  • 12
0

Because Cloud9 is all web-based you don't access it from localhost so by default better errors won't work. If you take a look at the security section of their README (https://github.com/charliesome/better_errors) you can add the following to config/environments/development.rb:

BetterErrors::Middleware.allow_ip! <ipaddress>

So that the errors page shows for your IP. You can find your apparent IP by hitting the old error page's "Show env dump" and looking at "REMOTE_ADDR".

Tim
  • 7,660
  • 3
  • 30
  • 33
  • I added the line you mentioned above, and I replaced the `` with a string containing my ip `"xx.xx.xx.xx"` and I got no change. Still showing the standard error page after I reboot the server. – elersong Nov 25 '14 at 20:09
  • I'm out of ideas. It might be worth sending an email to support@c9.io so they can diagnose why it's not working. – Tim Nov 26 '14 at 09:25