显示Rails调试错误(detailed_exceptions),如果请求用户是管理员?

huangapple go评论92阅读模式
英文:

Show Rails debugging errors (detailed_exceptions) if the request user is an admin?

问题

特别是对于较小的项目,与其在生产环境中得到更多的反馈,不如:

显示Rails调试错误(detailed_exceptions),如果请求用户是管理员?

我看到有一个名为 Rails.configuration.consider_all_requests_local 的变量。
我想知道是否可以使用这个变量来允许 admin 级别的用户在生产环境中看到完整的调试信息,当 production.rb 配置通常设置为:

# 禁用完整的错误报告并启用缓存。
config.consider_all_requests_local = false

我尝试了几种在应用程序控制器中设置 consider_all_requests_local = true 的函数变体,但并没有影响到错误是否实际显示。

英文:

Especially for smaller projects, it's nice to get more feedback in production than:

显示Rails调试错误(detailed_exceptions),如果请求用户是管理员?

I saw there's a variable called Rails.configuration.consider_all_requests_local.
I'm wondering if this could be used to allow admin level users to see the full debugging in production, when the production.rb configuration usually sets:

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false

I've tried several variations of functions setting consider_all_requests_local = true inside of the application controller, but it's not impacting errors actually being displayed or not.

答案1

得分: 0

根据 https://guides.rubyonrails.org/configuring.html#config-consider-all-requests-local,有一个名为 show_detailed_exceptions? 的函数,旨在在控制器中进行覆盖以指定哪些请求应在错误时提供调试信息。

因此,在 app/controllers/application_controller.rb 中,我只是添加了 show_details_exceptions? 函数:

class ApplicationController < ActionController::Base

  def show_detailed_exceptions?
    request.session[:user_id].present? && User.find(request.session[:user_id]).is_admin?
  end

end

两个Firefox窗口并排显示

Rails在localhost上运行,但RAILS_ENV=production

左窗口 右窗口
普通非管理员用户 admin用户身份登录
典型的生产风格异常 开发风格异常
“出现问题了” 故障排除更快
默认的“Firefox容器” 单独的 Firefox容器

显示Rails调试错误(detailed_exceptions),如果请求用户是管理员?

英文:

According to https://guides.rubyonrails.org/configuring.html#config-consider-all-requests-local there's a function called show_detailed_exceptions? that's designed to be overridden in controllers to specify which requests should provide debugging information on errors.

So in app/controllers/application_controller.rb I just added the show_details_exceptions? function:

class ApplicationController < ActionController::Base

  def show_detailed_exceptions?
    request.session[:user_id].present? && User.find(request.session[:user_id]).is_admin?
  end

end

Two firefox windows side by side

Rails running on localhost, but RAILS_ENV=production.

Left Window Right Window
Normal, non admin user logged in as admin user
Typical production style exception Development style exception
"something went wrong" Much faster to troubleshoot
Default "firefox container" separate firefox container

显示Rails调试错误(detailed_exceptions),如果请求用户是管理员?

huangapple
  • 本文由 发表于 2023年4月19日 16:07:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定