JSON API friendly error pages

42 views
Skip to first unread message

Andrew Havens

unread,
May 8, 2012, 2:25:42 PM5/8/12
to phusion-...@googlegroups.com
I just came across this scenario and thought I should mention it here. I am in the process of developing a REST API which is JSON only. I happened to cause an application error because I forgot to "bundle install" some application dependencies. The response that I got back from my API request (with HTTP header Accept:application/json) was a bunch of HTML. I would like to receive JSON friendly error pages when the Accept header is set to only accept JSON. That would be a lot easier to read than parsing through an HTML document to find the text of the error message.

Is this a current feature of Passenger that I can enable? Or do I need to wait for this to be built?

--Andrew

Hongli Lai

unread,
May 8, 2012, 5:52:02 PM5/8/12
to phusion-...@googlegroups.com
Hey Andrew. In my opinion this kind of thing is outside the scope of
Phusion Passenger. The reason why the error pages exist is to inform
the system administrator that the application is incorrectly
configured or has a bug. It's not supposed to handle business logic.
You should make sure that your application starts correctly and that
it handles all business errors.

- Hongli
--
Phusion | Ruby & Rails deployment, scaling and tuning solutions

Web: http://www.phusion.nl/
E-mail: in...@phusion.nl
Chamber of commerce no: 08173483 (The Netherlands)

Hongli Lai

unread,
Jun 23, 2012, 5:09:06 AM6/23/12
to phusion-...@googlegroups.com
On Fri, Jun 22, 2012 at 11:52 PM, Andrew Havens <misbe...@gmail.com> wrote:
> I don't consider the server not starting as business logic. Also, this is a
> feature of http. If you say that you only accept json, the server should
> respond with json. A simple json version of the html page would suffice:
>
> {
> error_message: 'goes here',
> exception_class: 'goes here',
> application_root: 'goes here',
> backtrace: ...
> }
>
> I would be happy to contribute this feature if you could direct me to where
> the logic for rendering the template is taking place.

Ok, I don't mind accepting a patch for this.

Since 3.2 is nearing completion and has a completely different HTTP
I/O handler than 3.0, please work from the 'experimental' branch
instead of 'master'. The HTTP I/O handler can be found in
ext/common/agents/HelperAgent/RequestHandler.h.

sessionCheckedOut_real() is where it first notices that the
application has failed to spawn. When e != NULL, it will write an
error response to the client using writeErrorResponse(). You can
modify writeErrorResponse() to write JSON instead of HTML when
appropriate.

We require contributors to sign our contributor agreement before we
can merge their patches, so please sign this form:
http://www.phusion.nl/forms/contributor_agreement

Let me know if you have any questions.
Reply all
Reply to author
Forward
0 new messages