Skip to content

Commit 82500a1

Browse files
authored
Merge pull request #261 from rails/truncate-error-backtrace
Improve backtrace truncation
2 parents 5d200d0 + 262c0c8 commit 82500a1

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

app/models/solid_queue/failed_execution.rb

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,45 @@ def retry
3333
end
3434

3535
private
36-
BACKTRACE_LINES_LIMIT = 400
36+
JSON_OVERHEAD = 256
3737

3838
def expand_error_details_from_exception
3939
if exception
40-
self.error = { exception_class: exception.class.name, message: exception.message, backtrace: exception.backtrace.first(BACKTRACE_LINES_LIMIT) }
40+
self.error = { exception_class: exception_class_name, message: exception_message, backtrace: exception_backtrace }
41+
end
42+
end
43+
44+
def exception_class_name
45+
exception.class.name
46+
end
47+
48+
def exception_message
49+
exception.message
50+
end
51+
52+
def exception_backtrace
53+
if (limit = determine_backtrace_size_limit) && exception.backtrace.to_json.bytesize > limit
54+
truncate_backtrace(exception.backtrace, limit)
55+
else
56+
exception.backtrace
57+
end
58+
end
59+
60+
def determine_backtrace_size_limit
61+
column = self.class.connection.schema_cache.columns_hash(self.class.table_name)["error"]
62+
if column.limit.present?
63+
column.limit - exception_class_name.bytesize - exception_message.bytesize - JSON_OVERHEAD
64+
end
65+
end
66+
67+
def truncate_backtrace(lines, limit)
68+
[].tap do |truncated_backtrace|
69+
lines.each do |line|
70+
if (truncated_backtrace << line).to_json.bytesize > limit
71+
truncated_backtrace.pop
72+
break
73+
end
74+
end
4175
end
4276
end
4377
end

0 commit comments

Comments
 (0)