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



27
28
29
30
31
32
33
34
# File 'lib/acidic_job/serializers/exception_serializer.rb', line 27

def deserialize(hash)
  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
end

#serialize(exception) ⇒ Object



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

def serialize(exception)
  hash = {
    "class" => exception.class.name,
    "message" => exception.message,
    "cause" => exception.cause,
    "backtrace" => {}
  }

  exception&.backtrace&.map do |trace|
    path, _, location = trace.rpartition("/")

    next if hash["backtrace"].key?(path)

    hash["backtrace"][path] = location
  end

  super(hash)
end

#serialize?(argument) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/acidic_job/serializers/exception_serializer.rb', line 36

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