diff --git a/docs/changelog.md b/docs/changelog.md index e5d6ed2..5862b5a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Added a `Parse` function for use in xpcalls + ## [0.2.0] - 2024-09-02 ### Changed diff --git a/src/Types.luau b/src/Types.luau new file mode 100644 index 0000000..5b10b0d --- /dev/null +++ b/src/Types.luau @@ -0,0 +1,7 @@ +export type ParsedError = { + Raw: string, + Message: string, + Trace: string, +} + +return {} diff --git a/src/init.luau b/src/init.luau index 3850107..ccad4ec 100644 --- a/src/init.luau +++ b/src/init.luau @@ -3,10 +3,13 @@ local RunService = game:GetService("RunService") local PackageLinksAllowed = RunService:IsStudio() +local Types = require(script.Types) + local LogList = nil local Metadata = { PackageURL = "", PackageName = "Package", + TraceLevel = 3, } -- Functions @@ -39,16 +42,16 @@ end [Learn More](https://google.com) ]=] -local function Format(logId: string, ...: any): string +local function Format(logId: string, trace: string, ...: any): string assert(LogList[logId], "Invalid log ID") - local Traceback = debug.traceback(nil, 3) - return string.gsub( - `[{Metadata.PackageName}] {LogList[logId] or "Package"}{if Metadata.PackageURL ~= "" + local FormattedLog = string.format(LogList[logId] or "Unknown", ...) + return (string.gsub( + `[{Metadata.PackageName}] {FormattedLog}{if Metadata.PackageURL ~= "" then `\nLearn: {Metadata.PackageURL}` - else ""}\nTrace: {Traceback}`, + else ""}\nTrace: {(string.gsub(trace, "\n", ""))}`, "\n", "\n\t\t\t\t" - ) + )) end --[=[ @@ -57,7 +60,7 @@ end [Learn More](https://google.com) ]=] local function Fatal(logId: string, ...: any): never - error(Format(logId, ...), 0) + error(Format(logId, debug.traceback(nil, Metadata.TraceLevel), ...), 0) end --[=[ @@ -66,7 +69,7 @@ end [Learn More](https://google.com) ]=] local function Warn(logId: string, ...: any) - warn(Format(logId, ...)) + warn(Format(logId, debug.traceback(nil, Metadata.TraceLevel), ...)) end --[=[ @@ -81,6 +84,23 @@ local function Assert(condition: T, logId: string, ...: any): T return Fatal(logId, ...) end +--[=[ + Extracts info from xpcall errors to beautify it. + + [Learn More](https://google.com) +]=] +local function Parse(error: string): Types.ParsedError + return { + Raw = error, + Message = string.gsub(error, "^.+:%d+:%s*", ""), + Trace = debug.traceback(nil, Metadata.TraceLevel), + } +end + +-- Types + +export type ParsedError = Types.ParsedError + -- Module return table.freeze({ @@ -90,4 +110,5 @@ return table.freeze({ Fatal = Fatal, Warn = Warn, Assert = Assert, + Parse = Parse, }) diff --git a/tests/client/hello.luau b/tests/client/hello.luau deleted file mode 100644 index 71679f5..0000000 --- a/tests/client/hello.luau +++ /dev/null @@ -1,14 +0,0 @@ -local Debugger = require(game.ReplicatedStorage.Packages.Debugger) - -Debugger.SetMetadata({ - PackageURL = "https://lumin-dev.github.io/LuminFramework/", - PackageName = "LuminFramework" -}) - -Debugger.SetLogs({ - sigma = "hi can you please say hi" -}) - -return function() - Debugger.Warn("sigma", "err") -end diff --git a/tests/client/test.client.luau b/tests/client/test.client.luau index 58dc149..3474661 100644 --- a/tests/client/test.client.luau +++ b/tests/client/test.client.luau @@ -1,2 +1,24 @@ -local hi = require(script.Parent.hello) -hi() \ No newline at end of file +local Debugger = require(game.ReplicatedStorage.Packages.debugger) +local TestModule = require(script.Parent.testmodule) + +Debugger.SetMetadata({ + PackageURL = "https://lumin-dev.github.io/LuminFramework/", + PackageName = "Lumin Framework", + TraceLevel = 2, +}) + +Debugger.SetLogs({ + Good = "hi can you please say hi", + Bad = "OH NO - %s", +}) + +Debugger.Warn("Good") + +local result, message = xpcall(function() + TestModule() +end, Debugger.Parse) + +if not result then + local error_message = message :: Debugger.ParsedError + Debugger.Warn("Bad", error_message.Message) +end diff --git a/tests/client/testmodule.luau b/tests/client/testmodule.luau new file mode 100644 index 0000000..963f00a --- /dev/null +++ b/tests/client/testmodule.luau @@ -0,0 +1,3 @@ +return function() + print(workspace.DoesNotExist) +end diff --git a/wally.toml b/wally.toml index 212f954..1fa6c95 100644 --- a/wally.toml +++ b/wally.toml @@ -1,7 +1,7 @@ [package] name = "lumin/debugger" description = "A customizable and extendable debugger for open source software." -version = "0.2.0" +version = "0.3.0" license = "MIT" registry = "https://github.com/UpliftGames/wally-index" realm = "shared"