From 454784f61a317397189a22936066cb28adeef0f0 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 15 Oct 2023 20:48:50 +0400 Subject: [PATCH] Better serialize errors --- exe/referator | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/exe/referator b/exe/referator index 456aaf1..de270cd 100755 --- a/exe/referator +++ b/exe/referator @@ -14,8 +14,17 @@ def success(data = nil) puts "OK #{data.to_json}" end -def failure(msg) - puts "ERR #{String(msg).lines.join(' ')}" +def failure(data = nil) + puts "ERR #{data.to_json}" +end + +def serialize_err(err) + { + class: err.class.name.freeze, + message: err.message.freeze, + stacktrace: err.backtrace.map(&:freeze).freeze, + cause: err.cause.nil? ? nil : serialize_err(err.cause), + }.freeze end workdir = ARGV.first @@ -24,7 +33,6 @@ workdir = File.expand_path(workdir).freeze config = Referator::Config.new workdir footnotes = Referator::Footnotes.new config -stacktrace = nil while (line = $stdin.gets) begin @@ -38,9 +46,6 @@ while (line = $stdin.gets) exit when :ping success :pong - when :stacktrace - success stacktrace - stacktrace = nil ########## # Config # ########## @@ -91,7 +96,6 @@ while (line = $stdin.gets) raise 'Invalid command' end rescue => e - stacktrace = e.backtrace.map(&:freeze).freeze - failure e.detailed_message + failure serialize_err e end end