Class: AcidicJob::Serializers::ExceptionSerializer

Inherits:
ActiveJob::Serializers::ObjectSerializer
  • Object
show all
Defined in:
lib/acidic_job/serializers/exception_serializer.rb

Instance Method Summary collapse

Instance Method Details

#deserialize(hash) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acidic_job/serializers/exception_serializer.rb', line 24

def deserialize(hash)
  if hash.key?("class")
    exception_class = hash["class"].constantize
    exception = exception_class.new(hash["message"])
    exception.set_backtrace(hash["backtrace"].map do |path, location|
                              [path, location].join("/")
                            end)
    exception
  elsif hash.key?("yaml")
    if YAML.respond_to?(:unsafe_load)
      YAML.unsafe_load(hash["yaml"])
    else
      YAML.load(hash["yaml"]) # rubocop:disable Security/YAMLLoad
    end
  end
end

#serialize(exception) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/acidic_job/serializers/exception_serializer.rb', line 8

def serialize(exception)
  compressed_backtrace = {}
  exception.backtrace&.map do |trace|
    path, _, location = trace.rpartition("/")
    next if compressed_backtrace.key?(path)

    compressed_backtrace[path] = location
  end
  exception.set_backtrace(compressed_backtrace.map do |path, location|
    [path, location].join("/")
  end)
  exception.cause&.set_backtrace([])

  super({ "yaml" => exception.to_yaml })
end

#serialize?(argument) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/acidic_job/serializers/exception_serializer.rb', line 41

def serialize?(argument)
  defined?(Exception) && argument.is_a?(Exception)
end