Skip to content

Commit a77d387

Browse files
committed
Improve backtrace truncation
So that instead of truncating to a fixed amount of lines, we check how much space we have and add lines until we hit the limit.
1 parent 90f4e13 commit a77d387

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
37+
DEFAULT_BACKTRACE_LINES_LIMIT = 400
3738

3839
def expand_error_details_from_exception
3940
if exception
40-
self.error = { exception_class: exception.class.name, message: exception.message, backtrace: exception.backtrace.first(BACKTRACE_LINES_LIMIT) }
41+
self.error = { exception_class: exception_class_name, message: exception_message, backtrace: exception_backtrace }
42+
end
43+
end
44+
45+
def exception_class_name
46+
exception.class.name
47+
end
48+
49+
def exception_message
50+
exception.message
51+
end
52+
53+
def exception_backtrace
54+
if column = self.class.connection.schema_cache.columns_hash(self.class.table_name)["error"]
55+
limit = column.limit - exception_class_name.bytesize - exception_message.bytesize - JSON_OVERHEAD
56+
57+
if exception.backtrace.to_json.bytesize <= limit
58+
exception.backtrace
59+
else
60+
truncate_backtrace(exception.backtrace, limit)
61+
end
62+
else
63+
exception.backtrace.take(DEFAULT_BACKTRACE_LINES_LIMIT)
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)