0

I have a simple application using Flask and MongoEngine and I'm having some trouble maintaining the full functionality on Heroku. I'm not having any errors thrown, but when I actually try to access it, my application throws a 500 error if I try to read from it. This the relevant part of my __init__ file that sets up the connection:

MONGO_URL = os.environ.get("MONGOHQ_URL")

app = Flask(__name__)
if MONGO_URL:
    credentials = re.sub(r"(.*?)//(.*?)(@hatch)", r"\2",MONGO_URL)
    username = credentials.split(":")[0]
    password = credentials.split(":")[1]
    app.config["MONGODB_DB"] = MONGO_URL.split("/")[-1]
    connect(
        MONGO_URL.split("/")[-1],
        host=MONGO_URL,
        port=1043,
        username=username,
        password=password
    )

The log of the error is this:

heroku[router]: at=info method=GET path=/admin/create/ host=tranquil-taiga-1563.herokuapp.com fwd="66.31.20.171" dyno=web.1 connect=2ms service=8ms status=500 bytes=291
app[web.1]: 10.191.63.167 - - "GET /admin/create/ HTTP/1.1" 500 -

Not quite sure if this is relevant, but the file that actually calls the view (in case my assumption about the source of the error is incorrect)

https://gist.github.com/Slater-Victoroff/6148884)

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
  • a 500 error may be caused by a run-time exception. Maybe a SytaxError? Maybe something one of the libs throws? It would be hard to guess without looking at actual logs. Can you paste the output of the 500 page (persumably containing a stack trace)? If not, can you look at your server's log file to find such a trace? If not, can you install a handler to print such a trace? – Nitzan Shaked Aug 04 '13 at 06:22
  • The thing that's been really troubling me is that there isn't any stacktrace, just a 500 error. Is there some way to force heroku to a lower level of logging to actually see why it errored? It's also worth noting that this only happens on heroku and in foreman it's all fine. – Slater Victoroff Aug 04 '13 at 14:51
  • It's not Heroku that does the logging, it's your application. I don't think there's a specific setting for exception logging -- the Python runtime will print those to `stderr` if it gets them. My guess is that you have a generic error handler installed -- go there and look around? – Nitzan Shaked Aug 04 '13 at 16:36
  • @NitzanShaked Oh dorp, I feel really silly. Apparently I just needed to set a SECRET_KEY for csrf to work. Sorry about that, since you really helped out do you want to post an answer on that so I can accept it? – Slater Victoroff Aug 04 '13 at 17:05
  • I don't feel I deserve credit here. Thanks anyways :-) – Nitzan Shaked Aug 04 '13 at 20:37
  • Out of curiosity: there *was* a generic error handler, right? And it's flask-wtf that does the csrf? – Nitzan Shaked Aug 04 '13 at 20:40
  • @NitzanShaked Nah, I tracked it down by just printing traces every line until I found the called none. Also yes, it's flask-wtf doing the csrf. – Slater Victoroff Aug 05 '13 at 00:01

1 Answers1

1

It was kind of a silly problem, but in case somebody comes by here I figure it's worth having a an answer for this problem.

Basically, since I was using WTForms for all of my submissions and whatnot, the above code was very nearly correct. The only change I needed to make (Which WTForms seems to require for csrf authentication) was to add this line:

app.config["SECRET_KEY"] = "secret_key"

Above the connect command.

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144