From d563c9a9f96053950c9443b6a6a678b355d3c91b Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Fri, 19 Oct 2012 15:47:49 +0400 Subject: [PATCH] #702: added handling errors --- app/controllers/api/v1/base_controller.rb | 4 +++ app/controllers/application_controller.rb | 32 ++++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 4fed0e67e..5a715cbae 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -12,6 +12,10 @@ class Api::V1::BaseController < ApplicationController protected + def set_locale + I18n.locale = :en + end + def error_message(subject, message) [message, subject.errors.full_messages].flatten.join('. ') end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 931f3fe0d..bb44887fd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,8 @@ # -*- encoding : utf-8 -*- class ApplicationController < ActionController::Base + AIRBRAKE_IGNORE = [ActionController::InvalidAuthenticityToken, + AbstractController::ActionNotFound] + protect_from_forgery layout :layout_by_resource @@ -18,23 +21,38 @@ class ApplicationController < ActionController::Base redirect_to forbidden_url, :alert => t("flash.exception_message") end - if !Rails.env.development? + unless Rails.application.config.consider_all_requests_local + rescue_from Exception, :with => :render_500 rescue_from ActiveRecord::RecordNotFound, ActionController::RoutingError, ActionController::UnknownController, - ::AbstractController::ActionNotFound do |exception| - respond_to do |format| - format.json { render :json => {:message => t("flash.404_message")}.to_json, :status => 404 } - format.html { redirect_to '/404.html', :alert => t("flash.404_message") } - end - end + AbstractController::ActionNotFound, :with => :render_404 end rescue_from Grit::NoSuchPathError, :with => :not_found protected + def render_404 + render_error 404 + end + + def render_500(e) + #check for exceptions Airbrake ignores by default and exclude them from manual Airbrake notification + unless AIRBRAKE_IGNORE.include? e.class + notify_airbrake(e) + end + render_error 500 + end + + def render_error(status) + respond_to do |format| + format.json { render :json => {:status => status, :message => t("flash.#{status}_message")}.to_json, :status => status } + format.html { redirect_to "/#{status}.html", :alert => t("flash.#{status}_message") } + end + end + def set_locale I18n.locale = check_locale( get_user_locale || (request.env['HTTP_ACCEPT_LANGUAGE'] ? request.env['HTTP_ACCEPT_LANGUAGE'][0,2].downcase : nil ))