ruby-on-railsrubyruby-on-rails-3exceptionimport-contacts

Rescue Authentication Error while retrieving contacts from cardmagic/contacts gem


I am using cardmagic/contacts gem to import contacts from email addresses in my application. It is working fine but my problem is I want to catch the authentication error like wrong username/password for following code.

@contacts = Contacts.guess(@username, @password)

I want to display that error to user.

I have tried this but not working for me.

@contacts = Contacts.guess(@username, @password)  rescue Contacts::AuthenticationError  

So please guide me how to do so. Thank you.


Solution

  • def import
      @username = params[:user]
      @password = params[:pass]
      begin
        @contacts = Contacts.guess(@username, @password)  
      rescue
        @error_message = e.message
        render :action => 'new'
      end
      redirect_to root_path
    end
    

    new.html.erb:

    <% if @error_message %>
      <%= @error_message %>
    <% end %>
    HTML to get username & password inputs