JSON format for errors (e.g. wrong password) -- how/where to customize?

771 views
Skip to first unread message

Tom Harrison

unread,
May 21, 2012, 5:50:05 PM5/21/12
to plataforma...@googlegroups.com
I would like my JSON errors from Devise to follow a format that is consistent with the rest of my app's API, e.g. 

{"error": {"parameter": "widget_color", "code": "123", "message": "Unknown widget color: chartreuse"}

Currently Devise is returning user errors like {"error": "Invalid password"}.

Is there a good way for me to capture all errors before they are returned and format them?

(I know variants of this question have been asked several thousand times, but a lot of answers are outdated, and none is very complete.  If I can get a good general direction, I will be happy to write a How-To guide for the wiki).

Thanks in advance.

Tom H

Carlos Antonio da Silva

unread,
May 21, 2012, 6:19:26 PM5/21/12
to plataforma...@googlegroups.com
You probably need to override the failure app and this method: https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb#L132

Take a look at the wiki, there are examples there to help you working with the failure app in case you have doubts, or just ping more questions if you need.

-- 
At.
Carlos Antonio

Tom Harrison

unread,
May 21, 2012, 9:38:03 PM5/21/12
to plataforma...@googlegroups.com
Carlos Antonio --

Thanks!  I had missed this article in the Wiki: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated, but it was the answer, along with your pointer to the method.

Works beautifully.  Ruby really is cool, isn't it?  :-)

Tom

Carlos Antonio da Silva

unread,
May 22, 2012, 12:49:18 AM5/22/12
to plataforma...@googlegroups.com
Awesome :)

Perhaps you could expand the wiki with a new page containing examples of what you want to achieve? It'd certainly help people having similar doubts in the future.

And yeah, agreed, Ruby is really cool :)
Cheers

-- 
At.
Carlos Antonio

Tom Harrison

unread,
May 22, 2012, 3:17:29 PM5/22/12
to plataforma...@googlegroups.com
Alas, overriding the FailureApp class seems to work on SessionsController actions (e.g. users/sign_in) but is handled differently by RegistrationsController stuff, which appears just to do 

Tom Harrison

unread,
May 22, 2012, 3:20:29 PM5/22/12
to plataforma...@googlegroups.com
Whoops, hit return early...

Alas, overriding the FailureApp class seems to work on SessionsController actions (e.g. users/sign_in) but is handled differently by RegistrationsController stuff, which appears just to do "respond_with resource".  So I guess I'll need to override some or all of the controller classes.  Sigh.

Unless anyone has any great ideas, I'll keep plugging away before I post a How To :-L

Tom

Carlos Antonio da Silva

unread,
May 22, 2012, 9:50:40 PM5/22/12
to plataforma...@googlegroups.com
It makes sense, the FailureApp is called when a guest tries to access a resource only available for signed in users. Registration and others are completely different things, and they are supposed to respond with a hash containing the resource errors: https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_controller/metal/responder.rb#L278

-- 
At.
Carlos Antonio

Tom Harrison

unread,
May 23, 2012, 8:55:36 AM5/23/12
to plataforma...@googlegroups.com
A little more overriding, and all is working as I want.  I really appreciate the pointers, and as soon as I get things working the way I want, I'll write a post on the Devise Wiki about using Devise with a JSON API, which is what I am working on.  A lot of the answers out there are incomplete, or outdated, so (at least for now) maybe your help, and my write up would save people time :-)

Thanks again!

Tom

Carlos Antonio da Silva

unread,
May 23, 2012, 9:00:42 AM5/23/12
to plataforma...@googlegroups.com
Great, for sure it'd help some people out there :)
Let me know if I can help with anything else.

-- 
At.
Carlos Antonio

On Wednesday, May 23, 2012 at 9:55 AM, Tom Harrison wrote:

outdated

Tom Harrison

unread,
May 25, 2012, 5:27:22 PM5/25/12
to plataforma...@googlegroups.com
Almost there with my attempt to create a unified JSON message structure for the whole app -- another question:

I have overridden the FailureApp#http_auth_body and have a message, e.g. "invalid" back from Warden.  What I really want is something like the ActiveRecord::errors object.  

I looked through the methods and instance variables available in the context of my custom failure class.  Any pointers as to if and how I might be able to get at the current errors.

Thanks!

Carlos Antonio da Silva

unread,
May 25, 2012, 6:02:19 PM5/25/12
to plataforma...@googlegroups.com
During a failure in sign in, Devise does not use an errors object, instead it uses unified flash messages. This is something by design, I'm not sure you'd want to change that. Perhaps you can just format the json response the same way with { :errors => .. }?

-- 
At.
Carlos Antonio

Tom Harrison

unread,
May 25, 2012, 6:11:54 PM5/25/12
to plataforma...@googlegroups.com
Thanks.  Kinda what I had concluded.  My pattern with the json response for my fancy API contains the field affected and the message (coming soon, a custom code per message).  This gives the client the ability to identify which field to highlight in their UI.

So there's no great rocket science involved in reverse engineering the few possible messages (all nicely documented in I18N), so I guess that's the way I'll go.

Thanks (as always) Carlos -- you're the best.

Tom

Carlos Antonio da Silva

unread,
May 25, 2012, 7:05:30 PM5/25/12
to plataforma...@googlegroups.com
Great, no problem, glad to help :)

It seems like the final result is getting good. Please let me know if you'll be able to release it somehow, either via gem / blog post / wiki page / other way.

Cheers.



-- 
At.
Carlos Antonio

straig...@gmail.com

unread,
Aug 31, 2013, 12:30:41 PM8/31/13
to plataforma...@googlegroups.com, tom.har...@gmail.com
Sorry for resurrecting an old thread, Tom any chance you can post your solution? I am looking to do the same for Sessons and Registrations with a custom JSON API response for errors
Reply all
Reply to author
Forward
0 new messages