首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试在Rails中呈现JSON错误“can't find Plan with id”,但无法

尝试在Rails中呈现JSON错误“can't find Plan with id”,但无法
EN

Stack Overflow用户
提问于 2020-11-05 00:36:42
回答 1查看 134关注 0票数 0

我想呈现一个在找不到Plan id时发生的错误。我在用失眠症。问题是,它甚至还没有到达if语句。

代码语言:javascript
运行
复制
class Api::V1::PlansController < Api::V1::BaseController
  before_action :authorize_request

  def show
    @plan = Plan.find(params[:id])
    if @plan.errors.any?
      render json 'wrong'
    else
      @accounts = @plan.accounts
      render json: @plan, status: :created
    end
  end

end

EN

Stack Overflow用户

发布于 2020-11-05 04:50:49

如果找不到if,ActiveRecord::FinderMethods#find将引发ActiveRecord::RecordNotFound异常。当一个异常被抛出时,它会停止执行。

您可以使用rescue_from来处理异常

代码语言:javascript
运行
复制
# Do not use the scope resolution operator when declaring classes and modules
# @see https://github.com/rubocop-hq/ruby-style-guide#namespace-definition
module Api
  module V1
    class PlansController < BaseController
      before_action :authorize_request
      rescue_from ActiveRecord::RecordNotFound, with: :not_found

      def show
        @plan = Plan.find(params[:id])
        render json: @plan
      end

      private

      def not_found
        render json: { error: 'not found' }, status: :not_found       
      end      
    end
  end
end

最初,使用find_by的建议听起来可能是个好主意,直到您意识到异常确实很有用,因为它会暂停操作的执行并防止出现零错误。

代码语言:javascript
运行
复制
module Api
  module V1
    class PlansController < BaseController
      # ... 
      before_action :set_plan

      def update
        # this would create a nil error if we had used 'find_by'
        if @plan.update(plan_params)
          # ...
        else
          # ...
        end
      end

      private
      def set_plan
        @plan = Plan.find(params[:id])
      end
    end
  end
end

使用rescue_from也是一个非常强大的模式,因为它允许您在继承链中向上移动错误处理,而不是重复自己:

代码语言:javascript
运行
复制
module Api
  module V1
    class BaseController < ::ActionController::Api
      rescue_from ActiveRecord::RecordNotFound, with: :not_found

      private
    
      def not_found
         render json: { error: 'not found' }, status: :not_found       
      end   
    end
  end
end

但最有可能的是,你根本就不需要它。Rails通过发送404 - Not Found响应在框架级别上拯救ActiveRecord::RecordNotFound。客户端应该不需要更多的上下文,在这种情况下,状态码和返回完全不必要的JSON错误消息响应是一种反模式。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64684101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档