handles_errors.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. module ApplicationController::HandlesErrors
  2. extend ActiveSupport::Concern
  3. included do
  4. rescue_from StandardError, with: :internal_server_error
  5. rescue_from ExecJS::RuntimeError, with: :internal_server_error
  6. rescue_from ActiveRecord::RecordNotFound, with: :not_found
  7. rescue_from ActiveRecord::StatementInvalid, with: :unprocessable_entity
  8. rescue_from ActiveRecord::RecordInvalid, with: :unprocessable_entity
  9. rescue_from ArgumentError, with: :unprocessable_entity
  10. rescue_from Exceptions::UnprocessableEntity, with: :unprocessable_entity
  11. rescue_from Exceptions::NotAuthorized, with: :unauthorized
  12. end
  13. def not_found(e)
  14. logger.error e
  15. respond_to_exception(e, :not_found)
  16. http_log
  17. end
  18. def unprocessable_entity(e)
  19. logger.error e
  20. respond_to_exception(e, :unprocessable_entity)
  21. http_log
  22. end
  23. def internal_server_error(e)
  24. logger.error e
  25. respond_to_exception(e, :internal_server_error)
  26. http_log
  27. end
  28. def unauthorized(e)
  29. error = humanize_error(e.message)
  30. response.headers['X-Failure'] = error.fetch(:error_human, error[:error])
  31. respond_to_exception(e, :unauthorized)
  32. http_log
  33. end
  34. private
  35. def respond_to_exception(e, status)
  36. status_code = Rack::Utils.status_code(status)
  37. respond_to do |format|
  38. format.json { render json: humanize_error(e.message), status: status }
  39. format.any do
  40. @exception = e
  41. @traceback = !Rails.env.production?
  42. file = File.open(Rails.root.join('public', "#{status_code}.html"), 'r')
  43. render inline: file.read, status: status
  44. end
  45. end
  46. end
  47. def humanize_error(error)
  48. data = {
  49. error: error
  50. }
  51. case error
  52. when /Validation failed: (.+?)(,|$)/i
  53. data[:error_human] = $1
  54. when /(already exists|duplicate key|duplicate entry)/i
  55. data[:error_human] = 'Object already exists!'
  56. when /null value in column "(.+?)" violates not-null constraint/i
  57. data[:error_human] = "Attribute '#{$1}' required!"
  58. when /Field '(.+?)' doesn't have a default value/i
  59. data[:error_human] = "Attribute '#{$1}' required!"
  60. when 'Exceptions::NotAuthorized'
  61. data[:error] = 'Not authorized'
  62. data[:error_human] = data[:error]
  63. end
  64. if Rails.env.production? && data[:error_human].present?
  65. data[:error] = data.delete(:error_human)
  66. end
  67. data
  68. end
  69. end