Set correct HTTP status code on error

Re-submit of #1057.

After reading through the git docs and learning lots of new stuff, I am now confident enough to try it again. Passed "all" of the tests (casperjs itself doesn't work but that's a different issue).