处理activeResource的返回错误代码

时间:2015-05-08 19:53:56

标签: ruby-on-rails devise

我有一个带有devise和simple-authentication-token的服务器,以及一个使用activeResource通过json从服务器验证用户的客户端。验证工作正常,但是当我尝试检索实体时,activeResource会给我一个内部服务器错误。我需要做些什么来处理这个错误?

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

问题已解决:

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

0 个答案:

没有答案