Return a error message instead of throwing a 500 (fixes #360)

If the user provided bad input, a 500 error would be thrown
since some variables weren't defined. Instead, we now return
a JSON object with a simple error message.