diff --git a/Data/Scripts/001_Technical/001_Debugging/003_Errors.rb b/Data/Scripts/001_Technical/001_Debugging/003_Errors.rb index 80d409a05..22b6eea1e 100644 --- a/Data/Scripts/001_Technical/001_Debugging/003_Errors.rb +++ b/Data/Scripts/001_Technical/001_Debugging/003_Errors.rb @@ -4,7 +4,17 @@ class Reset < Exception end +class EventScriptError < Exception + attr_accessor :event_message + + def initialize(message) + super(nil) + @event_message = message + end +end + def pbGetExceptionMessage(e,_script="") + return e.event_message.dup if e.is_a?(EventScriptError) # Message with map/event ID generated elsewhere emessage = e.message.dup emessage.force_encoding(Encoding::UTF_8) if e.is_a?(Hangup) @@ -18,27 +28,26 @@ def pbGetExceptionMessage(e,_script="") end def pbPrintException(e) - emessage = "" - if $EVENTHANGUPMSG && $EVENTHANGUPMSG!="" - emessage = $EVENTHANGUPMSG # Message with map/event ID generated elsewhere - $EVENTHANGUPMSG = nil - else - emessage = pbGetExceptionMessage(e) - end + emessage = pbGetExceptionMessage(e) # begin message formatting message = "[Pokémon Essentials version #{Essentials::VERSION}]\r\n" message += "#{Essentials::ERROR_TEXT}\r\n" # For third party scripts to add to - message += "Exception: #{e.class}\r\n" - message += "Message: #{emessage}\r\n" - # show last 10/25 lines of backtrace - message += "\r\nBacktrace:\r\n" - btrace = "" - if e.backtrace - maxlength = ($INTERNAL) ? 25 : 10 - e.backtrace[0, maxlength].each { |i| btrace += "#{i}\r\n" } + if !e.is_a?(EventScriptError) + message += "Exception: #{e.class}\r\n" + message += "Message: " + end + message += "#{emessage}" + # show last 10/25 lines of backtrace + if !e.is_a?(EventScriptError) + message += "\r\n\r\nBacktrace:\r\n" + backtrace_text = "" + if e.backtrace + maxlength = ($INTERNAL) ? 25 : 10 + e.backtrace[0, maxlength].each { |i| backtrace_text += "#{i}\r\n" } + end + backtrace_text.gsub!(/Section(\d+)/) { $RGSS_SCRIPTS[$1.to_i][1] } rescue nil + message += backtrace_text end - btrace.gsub!(/Section(\d+)/) { $RGSS_SCRIPTS[$1.to_i][1] } rescue nil - message += btrace # output to log errorlog = "errorlog.txt" errorlog = RTP.getSaveFileName("errorlog.txt") if (Object.const_defined?(:RTP) rescue false) @@ -55,7 +64,7 @@ def pbPrintException(e) print("#{message}\r\nThis exception was logged in #{errorlogline}.\r\nHold Ctrl when closing this message to copy it to the clipboard.") # Give a ~500ms coyote time to start holding Control t = System.delta - until (System.delta - t) >= 500000 + until (System.delta - t) >= 500_000 Input.update if Input.press?(Input::CTRL) Input.clipboard = message diff --git a/Data/Scripts/003_Game processing/003_Interpreter.rb b/Data/Scripts/003_Game processing/003_Interpreter.rb index 547428d2f..f4e604fad 100644 --- a/Data/Scripts/003_Game processing/003_Interpreter.rb +++ b/Data/Scripts/003_Game processing/003_Interpreter.rb @@ -140,53 +140,40 @@ class Interpreter e = $! raise if e.is_a?(SystemExit) || "#{e.class}" == "Reset" event = get_self - s = "Backtrace:\r\n" + # Gather text for error message message = pbGetExceptionMessage(e) + backtrace_text = "" if e.is_a?(SyntaxError) script.each_line { |line| line.gsub!(/\s+$/, "") if line[/^\s*\(/] - message += "\r\n***Line '#{line}' shouldn't begin with '('. Try\r\n" - message += "putting the '(' at the end of the previous line instead,\r\n" - message += "or using 'extendtext.exe'." - end - if line[/\:\:\s*$/] - message += "\r\n***Line '#{line}' can't end with '::'. Try putting\r\n" - message += "the next word on the same line, e.g. 'PBSpecies:" + ":MEW'" + message += "\r\n***Line '#{line}' shouldn't begin with '('. Try putting the '('\r\n" + message += "at the end of the previous line instead, or using 'extendtext.exe'." end } else - for bt in e.backtrace[0, 10] - s += bt + "\r\n" - end - s.gsub!(/Section(\d+)/) { $RGSS_SCRIPTS[$1.to_i][1] } + backtrace_text += "\r\n" + backtrace_text += "Backtrace:" + e.backtrace[0, 10].each { |i| backtrace_text += "\r\n#{i}" } + backtrace_text.gsub!(/Section(\d+)/) { $RGSS_SCRIPTS[$1.to_i][1] } rescue nil + backtrace_text += "\r\n" end - message = "Exception: #{e.class}\r\nMessage: " + message + "\r\n" - message += "\r\n***Full script:\r\n#{script}\r\n" - if event && $game_map + # Assemble error message + err = "Script error in Interpreter\r\n" + if $game_map map_name = ($game_map.name rescue nil) || "???" - err = "Script error in event #{event.id} (coords #{event.x},#{event.y}), map #{$game_map.map_id} (#{map_name}):\r\n" - err += "#{message}\r\n#{s}" - if e.is_a?(Hangup) - $EVENTHANGUPMSG = err - raise - end - elsif $game_map - map_name = ($game_map.name rescue nil) || "???" - err = "Script error in map #{$game_map.map_id} (#{map_name}):\r\n" - err += "#{message}\r\n#{s}" - if e.is_a?(Hangup) - $EVENTHANGUPMSG = err - raise - end - else - err = "Script error in interpreter:\r\n#{message}\r\n#{s}" - if e.is_a?(Hangup) - $EVENTHANGUPMSG = err - raise + if event + err = "Script error in event #{event.id} (coords #{event.x},#{event.y}), map #{$game_map.map_id} (#{map_name})\r\n" + else + err = "Script error in Common Event, map #{$game_map.map_id} (#{map_name})\r\n" end end - raise err + err += "Exception: #{e.class}\r\n" + err += "Message: #{message}\r\n\r\n" + err += "***Full script:\r\n#{script}" # \r\n" + err += backtrace_text + # Raise error + raise EventScriptError.new(err) end end #-----------------------------------------------------------------------------