0

I have this program that I have managed to upload to heroku without error. But when I open it in the browser, then if I keep in there a line that uses Regexp, I get "internal server error". If I comment it out it's fine.

I have looked at similarly titled questions about Heroku giving "internal server error", but they don't involve Regexp, and they haven't given me any ideas as to what I need to do to make it work. For example Heroku deployment internal server error the person was using python and a package he had installed was incompatible, that doesn't seem relevant to my issue. This one Heroku + Django Internal server Error a guy had to import something called djcelery. Well, maybe I have to import something somewhere, but I don't know what, but it wouldn't be djcelery 'cos i'm not using django.

C:\rubytest\regexinheroku1>dir /b
aaa.rb
config.ru
Gemfile
Gemfile.lock

C:\rubytest\regexinheroku1>

here is aaa.rb

C:\rubytest\regexinheroku1>type aaa.rb
require 'sinatra'

get '/' do
z=Regexp.new('.').match?('qwerty')   
"aaaa"
end

This succeeds

C:\rubytest\regexinheroku1>git push heroku master

.... deployed to Heroku

Here is the problem though

If I go to the URL, it says "Internal Server Error"

enter image description here

However, if I change aaa.rb by commenting out the regexp line

C:\rubytest\regexinheroku1>type aaa.rb
require 'sinatra'

get '/' do
# z=Regexp.new('.').match?('qwerty')   
"aaaa"
end

Then it works fine, a page comes up and says "aaaa" as expected.

If you want to reproduce this and the above isn't enough, here are all the steps

So you see the files

C:\rubytest\regexinheroku2>dir /b
aaa.rb
config.ru
Gemfile

The contents of each file 

C:\rubytest\regexinheroku2>type aaa.rb
require 'sinatra'

get '/' do
z=Regexp.new('.').match?('qwerty')  # if I uncomment this, I get internal server error
"aaaa"
end


C:\rubytest\regexinheroku2>type config.ru
require './aaa'
run Sinatra::Application
C:\rubytest\regexinheroku2>

C:\rubytest\regexinheroku2>type Gemfile
source "http://rubygems.org/"
gem 'sinatra'
gem "haml"

And see the commands I run to get the application successfully deployed

C:\rubytest\regexinheroku2>bundle install
...

C:\rubytest\regexinheroku2>git init
Initialized empty Git repository in C:/rubytest/regexinheroku2/.git/

C:\rubytest\regexinheroku2>git add -A
warning: LF will be replaced by CRLF in Gemfile.lock.
The file will have its original line endings in your working directory.

C:\rubytest\regexinheroku2>git commit -m "sddsfsdfsd"
[master (root-commit) 12cf382] sddsfsdfsd
warning: LF will be replaced by CRLF in Gemfile.lock.
The file will have its original line endings in your working directory.
 4 files changed, 39 insertions(+)
 create mode 100644 Gemfile
 create mode 100644 Gemfile.lock
 create mode 100644 aaa.rb
 create mode 100644 config.ru

C:\rubytest\regexinheroku2>heroku create
path1 gitmisc done
Creating app... done,   abc
https://abc.herokuapp.com/ | ....

C:\rubytest\regexinheroku2>git push heroku master
....
remote: Verifying deploy... done.
To https://git.heroku.com/abc.git
 * [new branch]      master -> master

C:\rubytest\regexinheroku2>
barlop
  • 12,887
  • 8
  • 80
  • 109

1 Answers1

2

As with all errors, the first thing to do is check the log. The log should (almost) always provide a better clue to the error than the generic "internal server error" that's public-facing.

However, in this case I'm almost certain the issue is that your local machine is using ruby version >= 2.4.0 (possibly 2.5.1, which is the latest?), whereas heroku is running ruby version 2.3.7. From the documentation:

If your Gemfile does not contain a ruby entry, you will get MRI 2.3.7 on Cedar-14 and Heroku-16 stacks. On Heroku-18 you will get MRI 2.4.4.

To fix this, I would recommend including:

ruby '2.5.1'

(or whatever version you're using) in the Gemfile. This is good practice in general, as it ensures your local and production environments are the same.

The actual issue here, to be specific, is that Ruby 2.4.0 added the method Regexp#match?. So Heroku is currently throwing an "unknown method" error.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • yeah thanks, makes sense. The log says `NoMethodError - undefined method 'match?' for /a/:Regexp` so you're right – barlop May 05 '18 at 16:14
  • Two things worth adding to that A)To check the log do heroku logs B)Your solution of writing `ruby '2.5.1'` in the Gemfile is brilliant and lets me use `match?`. As an alternative to your solution, I can write `Regexp.new('.').match('abcdefg')[0]` which is consistent with an early ruby version https://ruby-doc.org/core-1.8.7/Regexp.html#method-i-match But your solution is better. – barlop May 05 '18 at 16:21