Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 290469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:00:02+00:00 2026-05-12T06:00:02+00:00

I am making an activeresource call to a service, and I’d like some custom

  • 0

I am making an activeresource call to a service, and I’d like some custom error messages as feedback. I have some validations that aren’t normal model validations, so I can’t just return @object.errors.

So, for instance, one of my validations is this. Two objects have a many to many relationship, but I want to restrict one object to only have a limited number (say 2) of relationships to other objects. Here’s some code:

In the client:

response = Customer.find(customer_id).put(:add_user, :user_id => user_id)

This puts a request to add a user to the customer. Then in the service I want to check that this addition is valid.

def add_user    
 @user = User.find(params[:user_id])
 @customer = Customer.find(params[:id])
 if @customer.users.length > 2
  render :xml => "ERR_only_2_users_allowed", :status => :unprocessable_entity
 end
end

Here’s my problem. In active resource, if the return status is an error, the client side completely fails. I could change the status to 200 and I get back the body err msg fine, but this seems to defeat the purpose of having error reponse codes.

I can put the whole request call from the client in a begin/rescue block

begin
    response = Customer.find(customer_id).put(:add_user, :user_id => user_id)
  rescue ActiveResource::ResourceInvalid => e
    #return error code
end

but when I catch the 422 (unprocessable_entity) response, I get nothing of the body back, so I don’t get my custom error message. response = nil

Does anyone know how I can achieve these custom error message with the proper response codes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:00:02+00:00Added an answer on May 12, 2026 at 6:00 am

    This may or may not be your problem, but both of ours seem very close. I’m using a custom put method, but his should work for you too. What’s going on is that the code that does this:

    rescue ResourceInvalid => error
         errors.from_xml(error.response.body)
    end
    

    Is only working with the standard save method. If you want errors added when other methods are called it looks like you need to do it yourself.

    I had to add it to
    vendor/rails/activeresource/lib/active_resource/custom_methods.rb

    Here is what my diff from git looks like:
    old code:

    def put(method_name, options = {}, body = '')
      connection.put(custom_method_element_url(method_name, options), body, self.class.headers)
    end
    

    new code:

    def put(method_name, options = {}, body = '')
      begin
        connection.put(custom_method_element_url(method_name, options), body, self.class.headers)
      rescue ResourceInvalid => error
        errors.from_xml(error.response.body)
      end
      self 
    end
    

    So look at the stack trace when get the exception thrown for the 422 and see which method it’s calling exactly. Then add something like what I have and you should be good to go.

    Don’t ask me why the activeresource folks thought validations should only work with their save method. the save method does a create or update, but calling ‘put or post’ is the exact same thing, IMO. If we want validations to work on save we want them to work on put and post…anyway give it a shot.

    I’m not sure if i need the self at the end…i may not. I’m not totally done with this as I just figured out how to make it work.
    Erik

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

making a call to a web service method with nusoap returns an error array(3)
While making some final tests of a class-library that I'm writing for Windows Mobile
I have a web app that is using ActiveResource to talk to another server
Making game of life I need to a have a grid that is 30x20
I am trying to debug an ActiveResource call that is not working. What's the
Making a ship game because I am incredibly original.. With that aside, I have
im making an application that store multipe information like name description images for each
Making an adobe flex ui in which data that is calculated must use proprietary
Making UML sequence diagram in VS 2010RC I've observed that there is no activation
Making websites that appear correctly in IE is a big problem. Is there any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.