Module: RfcReader::ErrorContext

Defined in:
lib/rfc_reader/error_context.rb

Defined Under Namespace

Classes: ContextError

Class Method Summary collapse

Class Method Details

.handlerInteger

Yields a handler context where any ‘StandardError` is caught and the error message is printed to stderr along with the context. It prints only a short message by default and prints the full error message if the `DEBUG` error message is set. It then exits with a non-zero error code.

If no error has been raised, it returns the result of the block as an integer. If the result of block cannot be turned into an integer, it returns zero.

Returns:

  • (Integer)

    exit code



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rfc_reader/error_context.rb', line 80

def self.handler
  error = nil

  begin
    result = yield
    return result.respond_to?(:to_i) ? result.to_i : 0
  rescue ContextError => e
    error = e
  rescue StandardError => e
    error = ContextError.new(cause: e)
  end

  if ENV["DEBUG"]
    warn error.full_message
  else
    warn error.short_message
    warn "Note: Set the `DEBUG` environment variable to see the full error context"
  end

  1
end

.wrap(context) ⇒ Object

Yields an error context. Any ‘StandardError` that gets raised in this block gets wrapped by `ContextError` automatically and re-raised.

If no error has been raised, it returns the result of the block.

Parameters:

  • context (String)

Returns:

  • yielded block value



65
66
67
68
69
# File 'lib/rfc_reader/error_context.rb', line 65

def self.wrap(context)
  yield
rescue StandardError => e
  raise ContextError.new(cause: e, context: context)
end