refactor: improve error handling and output formatting in RunCode and RunScriptInPlayMode

- Enhanced error handling in `RunCode.luau` to capture and report syntax errors and unexpected errors more effectively.
- Updated output formatting to ensure consistent handling of returned results and error messages.
- Modified `RunScriptInPlayMode.luau` to return JSON-encoded results for table outputs, improving interoperability with other services.
This commit is contained in:
Joe 2026-03-13 17:12:37 -05:00
parent 0e318b6b33
commit f95035d5ad
2 changed files with 43 additions and 27 deletions

View file

@ -82,39 +82,51 @@ local function runCodeWithOutput(command: string): string
output ..= header .. " " .. table.concat(strResults, "\t") .. "\n"
end
local function executeCode(): { any }
local chunk = loadstring(command) :: any
local chunkfenv = getfenv(chunk)
local chunk, syntaxErr = loadstring(command)
if not chunk then
addToOutput("[SYNTAX ERROR]", syntaxErr or "Unknown syntax error")
return output
end
local oldPrint = print
chunkfenv.print = function(...)
oldPrint(...)
addToOutput("[OUTPUT]", ...)
end
local chunkfenv = getfenv(chunk :: any)
local oldWarn = warn
chunkfenv.warn = function(...)
oldWarn(...)
addToOutput("[WARNING]", ...)
end
local savedPrint = chunkfenv.print
local savedWarn = chunkfenv.warn
local savedError = chunkfenv.error
local oldError = error
chunkfenv.error = function(...)
oldError(...)
addToOutput("[ERROR]", ...)
end
local errorCaptured = false
local results = table.pack(chunk())
chunkfenv.print = function(...)
savedPrint(...)
addToOutput("[OUTPUT]", ...)
end
chunkfenv.warn = function(...)
savedWarn(...)
addToOutput("[WARNING]", ...)
end
chunkfenv.error = function(message: any, level: number?)
errorCaptured = true
addToOutput("[ERROR]", message)
savedError(message, (level or 1) + 1)
end
local ok, resultOrErr = pcall(function()
return table.pack((chunk :: any)())
end)
chunkfenv.print = savedPrint
chunkfenv.warn = savedWarn
chunkfenv.error = savedError
if ok then
local results = resultOrErr :: { any }
if #results > 0 then
addToOutput("[RETURNED RESULTS]", table.unpack(results))
end
return results
end
local ok, errorMessage = pcall(executeCode)
if not ok then
addToOutput("[UNEXPECTED ERROR]", errorMessage)
elseif not errorCaptured then
addToOutput("[UNEXPECTED ERROR]", resultOrErr)
end
return output

View file

@ -1,5 +1,6 @@
--!strict
local HttpService = game:GetService("HttpService")
local ServerScriptService = game:GetService("ServerScriptService")
local StudioTestService = game:GetService("StudioTestService")
@ -126,7 +127,10 @@ local function runScriptInPlayMode(args: Args): string?
error(`Failed to run script in play mode: {result}`)
end
return result
if type(result) == "table" then
return HttpService:JSONEncode(result)
end
return tostring(result)
end
local function handleRunScriptInPlayMode(args: Args): string?