Sha256: 6654c0815c77b45b3f1cf45a234ea75fbd2d339032f994e4f327264988b380a4

Contents?: true

Size: 1.42 KB

Versions: 8

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module RailsWorkflow
  # Default error builder. Can be changed in configuration.
  # Manages errors building
  class ErrorBuilder
    attr_accessor :exception, :context

    def self.handle(exception, context)
      new(exception, context).handle
    end

    def initialize(exception, context)
      @exception = exception
      @context = context
    end

    def handle
      create_error(context)
      process_parent(target)
    end

    private

    def create_error(context)
      error = RailsWorkflow::Error.create(
        parent: target,
        message: exception.message.first(250),
        stack_trace: exception.backtrace.join("<br/>\n")
      )

      error.create_context(data: context)
    end

    # Changing custom process or operation classes to default classes.
    # If we store error with a custom class and somebody will delete
    # or rename this class - we will not be able to load error.
    def target
      @target ||= begin
        parent = context[:parent]
        if parent.is_a? RailsWorkflow::Operation
          parent.becomes(RailsWorkflow::Operation)
        elsif parent.is_a? RailsWorkflow::Process
          parent.becomes(RailsWorkflow::Process)
        end
      end
    end

    def process_parent(subject)
      return if subject.nil?

      subject.status = Status::ERROR
      subject.save
      process_parent(subject.parent) if subject.parent.present?
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rails_workflow-0.7.2 lib/rails_workflow/error_builder.rb
rails_workflow-0.7.1 lib/rails_workflow/error_builder.rb
rails_workflow-0.7.0 lib/rails_workflow/error_builder.rb
rails_workflow-0.4.4 lib/rails_workflow/error_builder.rb
rails_workflow-0.4.3 lib/rails_workflow/error_builder.rb
rails_workflow-0.4.2 lib/rails_workflow/error_builder.rb
rails_workflow-0.4.1 lib/rails_workflow/error_builder.rb
rails_workflow-0.4.0 lib/rails_workflow/error_builder.rb