Skip to content

Commit

Permalink
Prepend Error and Warning Messages With [Cmdr] (evaera#311)
Browse files Browse the repository at this point in the history
* Prepend Error and Warning Messages With [Cmdr]

* Update Error Messages
  • Loading branch information
wilyt1 authored Sep 15, 2023
1 parent 67fc84b commit 294ea68
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Cmdr/CmdrClient/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local Util = require(Shared:WaitForChild("Util"))

if RunService:IsClient() == false then
error(
"Server scripts cannot require the client library. Please require the server library to use Cmdr in your own code."
"[Cmdr] Server scripts cannot require the client library. Please require the server library from the server to use Cmdr in your own code."
)
end

Expand Down
4 changes: 2 additions & 2 deletions Cmdr/Shared/Argument.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function Argument.new(command, argumentDefinition, value)
self.Prefix = prefix

if self.Type == nil then
error(string.format("%s has an unregistered type %q", self.Name or "<none>", parsedType or "<none>"))
error((`[Cmdr] {self.Name or "none"} has an unregistered type %q`):format(parsedType or "<none>"))
end
end

Expand Down Expand Up @@ -376,7 +376,7 @@ function Argument:GetValue(): any
local parsedValue = self:ParseValue(i)

if type(parsedValue) ~= "table" then
error(("Listable types must return a table from Parse (%s)"):format(self.Type.Name))
error(`[Cmdr] Listable types must return a table from Parse {self.Type.Name}`)
end

for _, value in pairs(parsedValue) do
Expand Down
21 changes: 11 additions & 10 deletions Cmdr/Shared/Command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function Command:Parse(allowIncompleteArguments)
local required = (definition.Default == nil and definition.Optional ~= true)

if required and hadOptional then
error(("Command %q: Required arguments cannot occur after optional arguments."):format(self.Name))
error(("[Cmdr] Command %q: Required arguments cannot occur after optional arguments."):format(self.Name))
elseif not required then
hadOptional = true
end
Expand Down Expand Up @@ -130,7 +130,7 @@ end
-- Command:Validate() must be called before this is called or it will throw.
function Command:Run()
if self._Validated == nil then
error("Must validate a command before running.")
error("[Cmdr] Must validate a command before running.")
end

local beforeRunHook = self.Dispatcher:RunHooks("BeforeRun", self)
Expand Down Expand Up @@ -160,11 +160,10 @@ function Command:Run()
elseif IsServer then -- Uh oh, we're already on the server and there's no Run function.
if self.Object.ClientRun then
warn(
self.Name,
"command fell back to the server because ClientRun returned nil, but there is no server implementation! Either return a string from ClientRun, or create a server implementation for this command."
`[Cmdr] {self.Name} command fell back to the server because ClientRun returned nil, but there is no server implementation! Either return a string from ClientRun, or create a server implementation for this command.`
)
else
warn(self.Name, "command has no implementation!")
warn(`[Cmdr] {self.Name} command has no implementation!`)
end

self.Response = "No implementation."
Expand Down Expand Up @@ -204,22 +203,24 @@ end

-- Sends an event message to a player
function Command:SendEvent(player, event, ...)
assert(typeof(player) == "Instance", "Argument #1 must be a Player")
assert(player:IsA("Player"), "Argument #1 must be a Player")
assert(type(event) == "string", "Argument #2 must be a string")
assert(typeof(player) == "Instance" and player:IsA("Player"), "[Cmdr] Argument #1 must be a Player")
assert(type(event) == "string", "[Cmdr] Argument #2 must be a string")

if IsServer then
self.Dispatcher.Cmdr.RemoteEvent:FireClient(player, event, ...)
elseif self.Dispatcher.Cmdr.Events[event] then
assert(player == Players.LocalPlayer, "Event messages can only be sent to the local player on the client.")
assert(
player == Players.LocalPlayer,
"[Cmdr] Event messages can only be sent to the local player on the client."
)
self.Dispatcher.Cmdr.Events[event](...)
end
end

-- Sends an event message to all players
function Command:BroadcastEvent(...)
if not IsServer then
error("Can't broadcast event messages from the client.", 2)
error("[Cmdr] Can't broadcast event messages from the client.", 2)
end

self.Dispatcher.Cmdr.RemoteEvent:FireAllClients(...)
Expand Down
14 changes: 7 additions & 7 deletions Cmdr/Shared/Dispatcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ local Dispatcher = {
]=]
function Dispatcher:Evaluate(text: string, executor: Player, allowIncompleteArguments: boolean?, data: any?)
if RunService:IsClient() == true and executor ~= Players.LocalPlayer then
error("Can't evaluate a command that isn't sent by the local player.")
error("[Cmdr] Can't evaluate a command that isn't sent by the local player.")
end

local arguments = Util.SplitString(text)
Expand Down Expand Up @@ -125,7 +125,7 @@ function Dispatcher:EvaluateAndRun(
end)

if not ok then
warn(("Error occurred while evaluating command string %q\n%s"):format(text, tostring(out)))
warn((`[Cmdr] Error occurred while evaluating command string %q\n{tostring(out)}`):format(text))
end

return ok and out or "An error occurred while running this command. Check the console for more information."
Expand All @@ -143,7 +143,7 @@ end
]=]
function Dispatcher:Send(text: string, data: any?)
if RunService:IsClient() == false then
error("Dispatcher:Send can only be called from the client.")
error("[Cmdr] Dispatcher:Send can only be called from the client.")
end

return self.Cmdr.RemoteFunction:InvokeServer(text, {
Expand All @@ -163,7 +163,7 @@ end
]=]
function Dispatcher:Run(...): string
if not Players.LocalPlayer then
error("Dispatcher:Run can only be called from the client.")
error("[Cmdr] Dispatcher:Run can only be called from the client.")
end

local args = { ... }
Expand Down Expand Up @@ -235,7 +235,7 @@ end
]=]
function Dispatcher:RunHooks(hookName: string, commandContext, ...)
if not self.Registry.Hooks[hookName] then
error(("Invalid hook name: %q"):format(hookName), 2)
error(("[Cmdr] Invalid hook name: %q"):format(hookName), 2)
end

if
Expand Down Expand Up @@ -276,7 +276,7 @@ end
Inserts the string into the window history
]=]
function Dispatcher:PushHistory(text: string)
assert(RunService:IsClient(), "PushHistory may only be used from the client.")
assert(RunService:IsClient(), "[Cmdr] PushHistory may only be used from the client.")

local history = self:GetHistory()

Expand All @@ -297,7 +297,7 @@ end
Returns an array of the user's command history. Most recent commands are inserted at the end of the array.
]=]
function Dispatcher:GetHistory(): { string }
assert(RunService:IsClient(), "GetHistory may only be used from the client.")
assert(RunService:IsClient(), "[Cmdr] GetHistory may only be used from the client.")

return TeleportService:GetTeleportSetting(HISTORY_SETTING_NAME) or {}
end
Expand Down
32 changes: 12 additions & 20 deletions Cmdr/Shared/Registry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,23 @@ local Registry = {
]=]
function Registry:RegisterType(name: string, typeObject)
if not name or typeof(name) ~= "string" then
error("Invalid type name provided: nil")
error("[Cmdr] Invalid type name provided: nil")
end

if not name:find("^[%d%l]%w*$") then
error(
('Invalid type name provided: "%s", type names must be alphanumeric and start with a lower-case letter or a digit.'):format(
name
)
`[Cmdr] Invalid type name provided: "{name}", type names must be alphanumeric and start with a lower-case letter or a digit.`
)
end

for key in pairs(typeObject) do
if self.TypeMethods[key] == nil then
error('Unknown key/method in type "' .. name .. '": ' .. key)
error(`[Cmdr] Unknown key/method in type "{name}": {key}`)
end
end

if self.Types[name] ~= nil then
error(('Type "%s" has already been registered.'):format(name))
error(`[Cmdr] Type {name} has already been registered.`)
end

typeObject.Name = name
Expand Down Expand Up @@ -215,7 +213,7 @@ end
@within Registry
]=]
function Registry:RegisterTypeAlias(name: string, alias: string)
assert(self.TypeAliases[name] == nil, ("Type alias %s already exists!"):format(alias))
assert(self.TypeAliases[name] == nil, `[Cmdr] Type alias {alias} already exists!`)
self.TypeAliases[name] = alias
end

Expand Down Expand Up @@ -259,7 +257,7 @@ Registry.RegisterHooksIn = Registry.RegisterTypesIn
function Registry:RegisterCommandObject(commandObject)
for key in pairs(commandObject) do
if self.CommandMethods[key] == nil then
error("Unknown key/method in command " .. (commandObject.Name or "unknown command") .. ": " .. key)
error(`[Cmdr] Unknown key/method in command "{commandObject.Name or "unknown command"}": {key}`)
end
end

Expand All @@ -269,11 +267,7 @@ function Registry:RegisterCommandObject(commandObject)
for key in pairs(arg) do
if self.CommandArgProps[key] == nil then
error(
('Unknown property in command "%s" argument #%d: %s'):format(
commandObject.Name or "unknown",
i,
key
)
`[Cmdr] Unknown property in command "{commandObject.Name or "unknown"}" argument #{i}: {key}`
)
end
end
Expand Down Expand Up @@ -321,11 +315,11 @@ function Registry:RegisterCommand(
local commandObject = require(commandScript)
assert(
typeof(commandObject) == "table",
`Invalid return value from command script "{commandScript.Name}" (CommandDefinition expected, got {typeof(commandObject)})`
`[Cmdr] Invalid return value from command script "{commandScript.Name}" (CommandDefinition expected, got {typeof(commandObject)})`
)

if commandServerScript then
assert(RunService:IsServer(), "The commandServerScript parameter is not valid for client usage.")
assert(RunService:IsServer(), "[Cmdr] The commandServerScript parameter is not valid for client usage.")
commandObject.Run = require(commandServerScript)
end

Expand Down Expand Up @@ -373,9 +367,7 @@ function Registry:RegisterCommandsIn(container: Instance, filter: ((any) -> bool
for skippedScript in pairs(skippedServerScripts) do
if not usedServerScripts[skippedScript] then
warn(
"Command script "
.. skippedScript.Name
.. " was skipped because it has 'Server' in its name, and has no equivalent shared script."
`[Cmdr] Command script {skippedScript.Name} was skipped because it has 'Server' in its name, and has no equivalent shared script.`
)
end
end
Expand All @@ -395,7 +387,7 @@ end
@within Registry
]=]
function Registry:RegisterDefaultCommands(arrayOrFunc: { string } | (any) -> boolean | nil)
assert(RunService:IsServer(), "RegisterDefaultCommands cannot be called from the client.")
assert(RunService:IsServer(), "[Cmdr] RegisterDefaultCommands cannot be called from the client.")

local dictionary = if type(arrayOrFunc) == "table" then Util.MakeDictionary(arrayOrFunc) else nil

Expand Down Expand Up @@ -488,7 +480,7 @@ end
]=]
function Registry:RegisterHook(hookName: string, callback: (any) -> string?, priority: number)
if not self.Hooks[hookName] then
error(("Invalid hook name: %q"):format(hookName), 2)
error(("[Cmdr] Invalid hook name: %q"):format(hookName), 2)
end

table.insert(self.Hooks[hookName], { callback = callback, priority = priority or 0 })
Expand Down
6 changes: 3 additions & 3 deletions Cmdr/Shared/Util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ function Util.MakeFuzzyFinder(setOrContainer: any): (string, boolean?, boolean?)
elseif type(setOrContainer[1]) == "string" then
names = setOrContainer
elseif setOrContainer[1] ~= nil then
error("MakeFuzzyFinder only accepts tables of instances or strings.")
error("[Cmdr] MakeFuzzyFinder only accepts tables of instances or strings.")
else
names = {}
end
else
error("MakeFuzzyFinder only accepts a table, Enum, or Instance.")
error("[Cmdr] MakeFuzzyFinder only accepts a table, Enum, or Instance.")
end

-- Searches the set (checking exact matches first)
Expand Down Expand Up @@ -488,7 +488,7 @@ function Util.MakeSequenceType(options)

assert(
options.Parse ~= nil or options.Constructor ~= nil,
"MakeSequenceType: Must provide one of: Constructor, Parse"
"[Cmdr] MakeSequenceType: Must provide one of: Constructor, Parse"
)

options.TransformEach = options.TransformEach or function(...)
Expand Down
4 changes: 3 additions & 1 deletion Cmdr/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ local RunService = game:GetService("RunService")
local Util = require(script.Shared:WaitForChild("Util"))

if RunService:IsServer() == false then
error("Cmdr server module is somehow running on a client!")
error(
"[Cmdr] Client scripts cannot require the server library. Please require the client library from the client to use Cmdr in your own code."
)
end

--[=[
Expand Down

0 comments on commit 294ea68

Please sign in to comment.