Class: Jekyll::L10n::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-l10n/utils/error_handler.rb

Overview

Handles errors with graceful fallback and logging.

ErrorHandler provides error handling utilities for gracefully managing exceptions, logging them with context, and providing default fallback values. This keeps the build process running even when individual operations fail.

Key responsibilities:

  • Execute code with error catching

  • Log errors with context information

  • Provide fallback default values on error

  • Optional debug backtraces

Examples:

result = ErrorHandler.handle_with_default('file loading', {}) do
  load_file('config.yml')
end
# Returns loaded data or {} on error

Class Method Summary collapse

Class Method Details

.handle_with_default(context, default_value) { ... } ⇒ Object

Execute code with error catching and fallback value.

Executes the provided block and catches any StandardError, logging it with context and returning the default value.

Parameters:

  • context (String)

    Context describing what was being done

  • default_value (Object)

    Default value to return on error

Yields:

  • Code to execute

Returns:

  • (Object)

    Result of block or default_value on error



47
48
49
50
51
52
# File 'lib/jekyll-l10n/utils/error_handler.rb', line 47

def self.handle_with_default(context, default_value)
  yield
rescue StandardError => e
  log_error(context, e)
  default_value
end

.handle_with_logging(context) { ... } ⇒ Object?

Execute code with error catching and fallback logging.

Executes the provided block and catches any StandardError, logging it with context and returning nil.

Parameters:

  • context (String)

    Context describing what was being done

Yields:

  • Code to execute

Returns:

  • (Object, nil)

    Result of block or nil on error



31
32
33
34
35
36
# File 'lib/jekyll-l10n/utils/error_handler.rb', line 31

def self.handle_with_logging(context)
  yield
rescue StandardError => e
  log_error(context, e)
  nil
end

.log_error(context, error) ⇒ void

This method returns an undefined value.

Log an error with context.

Logs error message and optionally backtrace if DEBUG environment variable is set.

Parameters:

  • context (String)

    Context describing what was being done

  • error (StandardError)

    The error that occurred



61
62
63
64
# File 'lib/jekyll-l10n/utils/error_handler.rb', line 61

def self.log_error(context, error)
  Jekyll.logger.error 'Localization', "Error in #{context}: #{error.message}"
  Jekyll.logger.debug 'Localization', error.backtrace.join("\n") if ENV['DEBUG']
end