Last active
December 11, 2015 03:18
-
-
Save Neopallium/4536604 to your computer and use it in GitHub Desktop.
Assertion function with formatting support.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Release as Public Domain. | |
local function fmt_error(fmt, ...) | |
local f_type = type(fmt) | |
if f_type == 'string' then | |
if select('#', ...) > 0 then | |
return string.format(fmt, ...) | |
else | |
-- only a single string, no formating needed. | |
return fmt | |
end | |
elseif f_type == 'function' then | |
-- fmt should be a callable function which returns the message to log | |
return fmt(...) | |
end | |
-- fmt is not a string and not a function, just call tostring() on it. | |
return tostring(fmt) | |
end | |
function assert_fmt(v, fmt, ...) | |
if v then return v, fmt, ... end | |
local good, msg = pcall(fmt_error, fmt, ...) | |
if not good then | |
msg = "Failed to format error message: " .. msg | |
end | |
error(msg, 2) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment