programing

Javascript 요청에 대한 Rescue_from

jooyons 2023. 8. 18. 22:31
반응형

Javascript 요청에 대한 Rescue_from

나의 Rails 2.3.8 응용 프로그램에서 나는 예외를 위한 rescue_from 코드를 가지고 있었는데, 이것은 Javascript 작업 중에 던져집니다.

rescue_from ::Exception, :with => :show_js_errors

...

def show_js_errors exception
  if request.format == :js
    flash[:error] = 'some error occured'
    render :update do |page|
      page.redirect_to({:controller => '/home', :action => :index})
    end
  else
    # use default error handling for non-JS requests
    rescue_action_without_handler(exception)
  end
end

따라서 Ajax 호출에 오류가 발생하면 사용자에게 오류 메시지가 표시됩니다.레일 3에서는 "without_handler" 메서드가 더 이상 존재하지 않기 때문에 기본 오류 처리를 간단히 호출할 수 없습니다.

갱신하다

검색 3시간 만에 올린 글인데, 올린 지 30분 만에 스스로 해결책을 찾았습니다.

예외를 다시 제기하십시오.

사용자가 오류 처리 중이므로 이 예외를 제외하고는 더 이상 처리되지 않습니다.

예외를 다시 제기하십시오.

def show_js_errors exception
  if request.format == :js
    flash[:error] = 'some error occured'
    render :update do |page|
      page.redirect_to({:controller => '/home', :action => :index})
    end
  else
    raise # <<
  end
end

http://simonecarletti.com/blog/2009/11/re-raise-a-ruby-exception-in-a-rails-rescue_from-statement/ 은 다음과 같이 동의합니다.

rescue_from ActiveRecord::StatementInvalid do |exception|
  if exception.message =~ /invalid byte sequence for encoding/
    rescue_invalid_encoding(exception)
  else
    raise
  end
end

[...]예외가 올바르게 다시 렌더링되지만 표준 레일즈 복구 메커니즘에 의해 [sic] 캡처되지 않으며 표준 예외 페이지가 렌더링되지 않습니다.

언급URL : https://stackoverflow.com/questions/6813723/rescue-from-for-javascript-requests

반응형