Module: Rwc::Concerns::ErrorHandling::Core

Defined in:
lib/rwc/concerns/error_handling.rb

Constant Summary collapse

ERROR_CLASSES =

A list of error classes that can be rescued during safe execution.

[
  ActiveRecord::RecordNotFound,
  ActiveRecord::RecordInvalid,
  ActiveModel::UnknownAttributeError,
  ActiveRecord::StatementInvalid,
  InvalidInputError,
  ServiceError
].freeze

Instance Method Summary collapse

Instance Method Details

#run_checks! ⇒ Object

Runs input validation checks. Must be implemented by the including class.

Raises:

  • (NotImplementedError) —

    When not implemented in a subclass.



40
41
42
# File 'lib/rwc/concerns/error_handling.rb', line 40

def run_checks!
  raise InvalidInputError, input.errors.flat_map(&:message) unless input.valid?
end

#safely_execute {|void| ... } ⇒ Context

Safely executes a block of code, running checks if needed.

Yields:

  • (void) —

    The block of code to execute.

Returns:

  • (Context) —

    The context after execution.



29
30
31
32
33
34
35
# File 'lib/rwc/concerns/error_handling.rb', line 29

def safely_execute(&block)
  run_checks! if should_run_checks?
  block.call
  context
rescue *ERROR_CLASSES => e
  fail!(error: e)
end