Changed the content of error messages raised by code in a Script event command

This commit is contained in:
Maruno17
2021-07-23 20:06:06 +01:00
parent 912631a8b1
commit a1cff9cc36
2 changed files with 49 additions and 53 deletions

View File

@@ -4,7 +4,17 @@
class Reset < Exception class Reset < Exception
end end
class EventScriptError < Exception
attr_accessor :event_message
def initialize(message)
super(nil)
@event_message = message
end
end
def pbGetExceptionMessage(e,_script="") 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 = e.message.dup
emessage.force_encoding(Encoding::UTF_8) emessage.force_encoding(Encoding::UTF_8)
if e.is_a?(Hangup) if e.is_a?(Hangup)
@@ -18,27 +28,26 @@ def pbGetExceptionMessage(e,_script="")
end end
def pbPrintException(e) def pbPrintException(e)
emessage = "" emessage = pbGetExceptionMessage(e)
if $EVENTHANGUPMSG && $EVENTHANGUPMSG!=""
emessage = $EVENTHANGUPMSG # Message with map/event ID generated elsewhere
$EVENTHANGUPMSG = nil
else
emessage = pbGetExceptionMessage(e)
end
# begin message formatting # begin message formatting
message = "[Pokémon Essentials version #{Essentials::VERSION}]\r\n" message = "[Pokémon Essentials version #{Essentials::VERSION}]\r\n"
message += "#{Essentials::ERROR_TEXT}\r\n" # For third party scripts to add to message += "#{Essentials::ERROR_TEXT}\r\n" # For third party scripts to add to
message += "Exception: #{e.class}\r\n" if !e.is_a?(EventScriptError)
message += "Message: #{emessage}\r\n" message += "Exception: #{e.class}\r\n"
# show last 10/25 lines of backtrace message += "Message: "
message += "\r\nBacktrace:\r\n" end
btrace = "" message += "#{emessage}"
if e.backtrace # show last 10/25 lines of backtrace
maxlength = ($INTERNAL) ? 25 : 10 if !e.is_a?(EventScriptError)
e.backtrace[0, maxlength].each { |i| btrace += "#{i}\r\n" } 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 end
btrace.gsub!(/Section(\d+)/) { $RGSS_SCRIPTS[$1.to_i][1] } rescue nil
message += btrace
# output to log # output to log
errorlog = "errorlog.txt" errorlog = "errorlog.txt"
errorlog = RTP.getSaveFileName("errorlog.txt") if (Object.const_defined?(:RTP) rescue false) 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.") 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 # Give a ~500ms coyote time to start holding Control
t = System.delta t = System.delta
until (System.delta - t) >= 500000 until (System.delta - t) >= 500_000
Input.update Input.update
if Input.press?(Input::CTRL) if Input.press?(Input::CTRL)
Input.clipboard = message Input.clipboard = message

View File

@@ -140,53 +140,40 @@ class Interpreter
e = $! e = $!
raise if e.is_a?(SystemExit) || "#{e.class}" == "Reset" raise if e.is_a?(SystemExit) || "#{e.class}" == "Reset"
event = get_self event = get_self
s = "Backtrace:\r\n" # Gather text for error message
message = pbGetExceptionMessage(e) message = pbGetExceptionMessage(e)
backtrace_text = ""
if e.is_a?(SyntaxError) if e.is_a?(SyntaxError)
script.each_line { |line| script.each_line { |line|
line.gsub!(/\s+$/, "") line.gsub!(/\s+$/, "")
if line[/^\s*\(/] if line[/^\s*\(/]
message += "\r\n***Line '#{line}' shouldn't begin with '('. Try\r\n" message += "\r\n***Line '#{line}' shouldn't begin with '('. Try putting the '('\r\n"
message += "putting the '(' at the end of the previous line instead,\r\n" message += "at the end of the previous line instead, or using 'extendtext.exe'."
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'"
end end
} }
else else
for bt in e.backtrace[0, 10] backtrace_text += "\r\n"
s += bt + "\r\n" backtrace_text += "Backtrace:"
end e.backtrace[0, 10].each { |i| backtrace_text += "\r\n#{i}" }
s.gsub!(/Section(\d+)/) { $RGSS_SCRIPTS[$1.to_i][1] } backtrace_text.gsub!(/Section(\d+)/) { $RGSS_SCRIPTS[$1.to_i][1] } rescue nil
backtrace_text += "\r\n"
end end
message = "Exception: #{e.class}\r\nMessage: " + message + "\r\n" # Assemble error message
message += "\r\n***Full script:\r\n#{script}\r\n" err = "Script error in Interpreter\r\n"
if event && $game_map if $game_map
map_name = ($game_map.name rescue nil) || "???" 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" if event
err += "#{message}\r\n#{s}" err = "Script error in event #{event.id} (coords #{event.x},#{event.y}), map #{$game_map.map_id} (#{map_name})\r\n"
if e.is_a?(Hangup) else
$EVENTHANGUPMSG = err err = "Script error in Common Event, map #{$game_map.map_id} (#{map_name})\r\n"
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
end end
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
end end
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------