2012-03-27 2 views
6

Ruby에서 AOP를 사용하여 예외를 처리하려고합니다. 여기에 사용 된 툴킷은 Aquarium (http://aquarium.rubyforge.org/)입니다.Ruby의 SystemStackError Aquarium (Aspect Oriented Programming)을 사용한 예외 처리

필자는 작성된 ApplicationController 클래스의 모든 하위 항목 (하위 클래스)을 샘플 코드로 작성했습니다.

다음 프로그램을 실행하면 SystemStackError가 발생합니다 ("ulimit -s"를 사용하여 스택 제한을 설정해 보았습니다). 누군가 나를 도와주세요!. 또는 맵핑에 대한 제안 사항 : 수퍼 클래스의 서브 클래스 all_methods를 환영합니다. 미리 감사드립니다.

require 'aquarium' 

include Aquarium::Aspects 

class ApplicationController 
end 

class Abc < ApplicationController 
    def func 
     puts "func called" 
     raise Exception.new # SystemStackError is thrown before reaching place 
    end  
end 

    #Dummy class 
class Def < ApplicationController 
end 

Aspect.new :after_raising => Exception, 
    :in_types_and_descendents => "ApplicationController" do |jp, object, *args| 
     puts "Exception Handling Code" 
end 

a = Abc.new 
a.func 
당신은 모듈 (또는 스칼라의 특성을)이없는 경우에만 자바와 같은 언어에 대한 감각을 만드는 방법을 사용하도록 의무화했습니다
+0

here를 찾을 수 있습니다. –

+0

아니요, 작동하지 않습니다. –

+0

어쩌면 당신은 http://apidock.com/rails/ActiveSupport/Rescuable/ClassMethods/rescue_from 같은 것을 사용할 수 있습니까? – Fivell

답변

1

? 모듈 파일을 필요로하는 한 self.send : include 또는 이와 유사한 모듈을 모듈에 포함하면 추가 작업없이이 작업을 수행 할 수 있습니다.

아무튼 나는 Avdi Grimm의 Exceptional Ruby를 읽고 Ruby에서 예외가 작동하는 방식을 이해할 것을 제안합니다. 다시 말해 Java와 동일하지 않습니다.

루비는 의존성 주입을 필요로하지 않습니다. 언어의 철학에 완전히 위배됩니다.

1

당신도 내 작은 보석 - aspector을 사용할 수 있습니다.

aspect는 aspect 루비를 사용하는 일반적인 루비 클래스로, 메소드 실행 전후에 로직을 정의합니다. 측면은 독립적으로 테스트하고 클래스에 적용 할 수 있습니다. 이 문제의 경우 잘 모르겠어요,하지만 예외는 매우 심각한 오류를 포함 할 수 있습니다대로 올리고 StandardError를 구출한다 나는 아래의 코드 예제를 포함했지만 완전한 예는

class ExceptionHandler < Aspector::Base 
    around options[:methods] do |proxy, *args, &block| 
    begin 
     proxy.call *args, &block 
    rescue Exception => e 
     puts "Exception Handling Code" 
    end 
    end 
end 

ExceptionHandler.apply Abc, :methods => Abc.instance_methods 

a = Abc.new 
a.func 
관련 문제