2011-02-23 3 views
2

rails3을 사용하고 있으며 404.html과 500.html에 매핑되는 예외 목록을 표시하는 모든 예외 목록을 찾고 있습니다 PRODUCTION 모드에서.모든 예외는 404.html에 매핑되며 예외는 500.html에 매핑됩니다

은 지금은 내 application_controller에

rescue_from ActiveRecord::RecordNotFound, :with => :render_404 

같은 것을 추가해야하고 나는 그것을 좋아하지 않는다. Rails가 자동으로 처리해야한다고 생각합니다.

답변

0

레일스는 요청한 URL을 컨트롤러/작업에 올바르게 매핑 할 수없는 경우 (경로가 사용자의 경로와 일치하지 않는 경우) 404 오류를 표시합니다. 처리되지 않은 다른 예외는 대신 500 오류를 표시합니다.

1

내 응용 프로그램 컨트롤러에이 일을 해요 :

rescue_from Exception, :with => :handle_error 

def handle_error(exception) 
    if exceptions_to_treat_as_404.include?(exception.class) 
    render_404 
    else 
    raise exception if Rails.env == 'development' 
    body = exception_error_message(exception) 

    #to logger 
    logger.fatal(body) 

    #to email 
    from = '<[email protected]>' 
    recipients = "<[email protected]>" 
    subject = "[ERROR] (#{exception.class}) #{exception.message.inspect}" 
    GenericEmail.create(:subject => subject, :from => from, :recipients => recipients, :body => body) 

    #to PageRequest table 
    log_request(true) 

    #render error message 
    render_500 
    end 
end 


def exceptions_to_treat_as_404 
    exceptions = [AbstractController::ActionNotFound, 
     ActiveRecord::RecordNotFound, 
       ActionController::UnknownController, 
       URI::InvalidURIError, 
       ActionController::UnknownAction] 
    exceptions << ActionController::RoutingError if ActionController.const_defined?(:RoutingError) 
    exceptions 
end