2009-05-22 6 views
0

예 : 여기CommandLine :: Application은 내 예외를 삼킨다. 피하는 법?

$ ruby test.rb 
ERROR: foo 

그리고 문제가 시작에

require 'commandline' 
class App < CommandLine::Application 
    def initialize 
    end 
    def main 
     raise 'foo' 
    end 
end 

결과 : 개발하는 동안 항상 내 코드에서 깊은 어딘가에 던져 예외가있을 것입니다 내가 스택 트레이스를 볼 필요가 일부 난도질하지 메시지. rampion

덕분에, 지금은이 솔루션을 사용하고 있습니다 :

require 'commandline' 
class App < CommandLine::Application 
    def initialize 
    end 
    def main 
     raise 'foo' 
     rescue Exception => e 
      puts format_like_real_exception e 
    end 
    def format_like_real_exception(e) 
     s = '' 
     b = e.backtrace 
     s << b.shift << ': ' << e.message << "\n" 
     b.each { |l| s << "\t" << l << "\n" } 
     s 
    end 
end 

포맷터가 필요하지 않습니다 물론,하지만 난 그들이 원래 서식하고있는 방법을 선호합니다.

답변

1

다른 방법으로는, 당신은 단지 main에 오류를 구출 수 : 위대한

require 'rubygems' 
require 'commandline' 
class App < CommandLine::Application 
    def initialize 
    end 
    def main 
     raise 'foo' 
    rescue Exception => e 
     puts "BACKTRACE:" 
     puts e.backtrace 
    end 
end 
+0

, 내가 너무 내 Q와 포맷터에 추가. – mark

0

지금까지 발견 된 솔루션 : 사용하지 않는 뭔가 다른

  1. 변경 CommandLine::Application
  2. CommandLine::Application_wo_AutoRun에 이름 변경 방법 main, 예를 들어 '인스턴스를 반환
  3. 호출 정적 메서드 App.run 시작 그런 다음 start 방법을 호출하십시오.

전체 예제 :

require 'commandline' 
class App < CommandLine::Application_wo_AutoRun 
    def initialize 
    end 
    def start 
     raise 'foo' 
    end 
end 
app = App.run 
app.start 

documentationApplication_wo_AutoRun을 언급하지만, 더 이상 진행하는 방법을 지원하지 않습니다. 근원 공부 만이 여기에서 도움이되었습니다.

관련 문제