Merge pull request #705 from warpc/702-handling-errors-on-api-requests
[refs #702]: correct response for 404 and 500 code in REST API
This commit is contained in:
commit
1d041667a3
|
@ -12,6 +12,10 @@ class Api::V1::BaseController < ApplicationController
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
def set_locale
|
||||||
|
I18n.locale = :en
|
||||||
|
end
|
||||||
|
|
||||||
def error_message(subject, message)
|
def error_message(subject, message)
|
||||||
[message, subject.errors.full_messages].flatten.join('. ')
|
[message, subject.errors.full_messages].flatten.join('. ')
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
# -*- encoding : utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
|
AIRBRAKE_IGNORE = [ActionController::InvalidAuthenticityToken,
|
||||||
|
AbstractController::ActionNotFound]
|
||||||
|
|
||||||
protect_from_forgery
|
protect_from_forgery
|
||||||
|
|
||||||
layout :layout_by_resource
|
layout :layout_by_resource
|
||||||
|
@ -18,23 +21,38 @@ class ApplicationController < ActionController::Base
|
||||||
redirect_to forbidden_url, :alert => t("flash.exception_message")
|
redirect_to forbidden_url, :alert => t("flash.exception_message")
|
||||||
end
|
end
|
||||||
|
|
||||||
if !Rails.env.development?
|
unless Rails.application.config.consider_all_requests_local
|
||||||
|
|
||||||
|
rescue_from Exception, :with => :render_500
|
||||||
rescue_from ActiveRecord::RecordNotFound,
|
rescue_from ActiveRecord::RecordNotFound,
|
||||||
ActionController::RoutingError,
|
ActionController::RoutingError,
|
||||||
ActionController::UnknownController,
|
ActionController::UnknownController,
|
||||||
::AbstractController::ActionNotFound do |exception|
|
AbstractController::ActionNotFound, :with => :render_404
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue_from Grit::NoSuchPathError, :with => :not_found
|
rescue_from Grit::NoSuchPathError, :with => :not_found
|
||||||
|
|
||||||
protected
|
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
|
def set_locale
|
||||||
I18n.locale = check_locale( get_user_locale ||
|
I18n.locale = check_locale( get_user_locale ||
|
||||||
(request.env['HTTP_ACCEPT_LANGUAGE'] ? request.env['HTTP_ACCEPT_LANGUAGE'][0,2].downcase : nil ))
|
(request.env['HTTP_ACCEPT_LANGUAGE'] ? request.env['HTTP_ACCEPT_LANGUAGE'][0,2].downcase : nil ))
|
||||||
|
|
Loading…
Reference in New Issue