2017-12-07 15 views
0

내가 태그 로그에 대한 내 자신의 Exception를 구축하기 위해 노력하고있어 :레일 - 사용자 정의 예외 (오류)

raise Exceptions::SecurityException "Something security related happened.

: 내가 함께 새로운 예외를 호출 할 수 있기를 기대하고 있습니다

module Exceptions 
    class GeneralException < StandardError 
    LOGGER_NAME = 'Base' 

    def initialize(message) 
     @logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT)) 
     @logger.tagged(get_logger_name) { @logger.error message } 
     @message = message 
    end 

    def get_logger_name 
     self.class::LOGGER_NAME 
    end 
    end 

    class InvalidDataException < GeneralException; end 

    class SecurityException < GeneralException 
    LOGGER_NAME = 'Security' 
    end 

    class ElasticSearchException < GeneralException 
    LOGGER_NAME = 'Elastic' 
    end 
end 

문제는 내가이를 호출 할 때 내가 얻을 수 있습니다 :

NoMethodError: undefined method 'SecurityException' for Exceptions:Module

어떤 생각을 올바르게이 오류를 제기하는 방법?

+1

에, SecurityException가없는 방법입니다. 그것은 수업입니다. 먼저 메시지를 인스턴스화하고 메시지를 전달해야합니다. – jemonsanto

답변

1

음, 아주 쉽게, 당신은 오류의 인스턴스를 제기해야합니다

raise Exceptions::SecurityException.new "Something security related happend." 

또는

raise Exceptions::SecurityException, "Something security related happend."