2

I have a server with devise and simple-authentication-token, and a client that use activeResource to authenticate user from server by json. The authantication works fine, but when I try retrieve an entity, the activeResource give me an internal server error. What do I need make to treat this error?

codeStarted GET "/news/1.jsonuser_email=mauricio1@gmail.com&user_token=JcmL-bouqbxSzeNznEb" for ::1 at 2015-05-08 16:41:13 -0300
Processing by NewsController#show as JSON
   Parameters: {"user_email"=>"mauricio1@gmail.com", "user_token"=>"JcmL-bouqbxSzeNznEb", "id"=>"1"}
Completed 500 Internal Server Error in 114ms

ActiveResource::Redirection (Failed.  Response code = 302.  Response message = Found . => http://localhost:3001/users/sign_in.json):
   app/controllers/news_controller.rb:68:in `set_news'


Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.0ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (21.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.5ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (35.1ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.5ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (144.0ms)

My NewsController.rb

class NewsController < ApplicationController
  before_action :set_news, only: [:show, :edit, :update, :destroy]

  # GET /news
  # GET /news.json
  def index
    @news = News.all
  end

  # GET /news/1
  # GET /news/1.json
  def show
  end

  # GET /news/new
  def new
    @news = News.new
  end

  # GET /news/1/edit
  def edit
  end

  # POST /news
  # POST /news.json
  def create
    @news = News.new(news_params)

    respond_to do |format|
      if @news.save
        format.html { redirect_to @news, notice: 'News was successfully created.' }
        format.json { render :show, status: :created, location: @news }
      else
        format.html { render :new }
        format.json { render json: @news.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /news/1
  # PATCH/PUT /news/1.json
  def update
    respond_to do |format|
      if @news.update(news_params)
        format.html { redirect_to @news, notice: 'News was successfully updated.' }
        format.json { render :show, status: :ok, location: @news }
      else
        format.html { render :edit }
        format.json { render json: @news.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /news/1
  # DELETE /news/1.json
  def destroy
    @news.destroy
    respond_to do |format|
      format.html { redirect_to news_index_url, notice: 'News was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_news
      @news = News.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def news_params
      params[:news]
    end
end

Problem resolved with:

def set_news
  begin
    @news = News.find(params[:id])
  rescue ActiveResource::Redirection
    respond_to do |format|   
      format.json { render :json => { :state => {:code => 403, :message => "Forbidden"} }, :status => :forbidden }
    end
  rescue ActiveResource::ResourceNotFound
    respond_to do |format|   
      format.json { render :json => { :state => {:code => 404, :message => "Not Found"} }, :status => :not_found }
    end
  rescue Exception => e
    respond_to do |format|   
      format.json { render :json => { :state => {:code => 422, :message => "Unprocessabel Entity"} }, :status => :unprocessable_entity }
    end
  end
end
  • include `news_controller.rb` in your question please. That would help... – Cyzanfar May 08 '15 at 20:19
  • @Cyzanfar, the set_credentials method only put email and token in http header. It's implemented in my News model. They are used to devise for authenticate user. – Maurício Silva May 08 '15 at 20:37
  • You should not `rescue Exception` like you have above. Instead you should `rescue StandardError`. Further explanation: http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby – Tom Jan 05 '16 at 16:33

0 Answers0