How can I return an error message to an ActiveResource get request?
From the documentation I can find, the proper reponse to an invalid ActiveResource get request is to return :status => :unprocessable_entity
I tried passing an error message along with the response but the ActiveResource model doesn't seem to receive anything other than the http error code. This doesn't happen for http success codes - i.e. I can pass a valid @user object back with :status => :ok
My users_controller on the server application has code like this:
...
respond_to do |format|
if @user
if @user.active?
format.xml { render :json => @user, :status => :ok, :location => @user }
else
format.xml { render :json => '{"error":"disabled user"}', :status => :unprocessable_entity }
end
else
format.xml { render :json => '{"error":"incorrect login details"}', :status => :unprocessable_entity }
end
end
end
...
The ActiveResource user model on the client application has:
class User < ActiveResource::Base
self.site = "http://localhost:3000/"
self.format = :json
self.user = "user"
self.password = "pass"
def self.login(user_name, password)
res = get(:login, :user =>{:user_name => user_name, :password => password)
User.new(res['user'])
rescue
raise $!.inspect
end
end
The problem? If I enter invalid user details then all I get back is ActiveResource::ResourceInvalid: Failed with 422
My error string doesn't get passed along. The 422 code does seem to pass additional information along for other ActiveResource requests (e.g. a put request to save the model), but not for get requests.
So how can I make my code differentiate between say, invalid login details and a disabled user?
One possibility would be to return different http status codes depending on the error, and have the client model display switch on these to display different messages to the user. But this seems like a work-around rather than a proper solution.
Surely there must be a way to pass an error message back to the ActiveResource get request. Does anyone know how?
rails | ActiveResource | error codes