Skip to content

Add "Assert" Global Function #2206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions garrysmod/lua/includes/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,39 @@ function Either( iff, aa, bb )
return bb
end

--[[---------------------------------------------------------
Custom assert with more features
-----------------------------------------------------------]]
local ProtectedCall = ProtectedCall
local Format = Format
local error = error

function Assert( expression, errorMessage, errorLevel, noHalt, ... )

if ( errorMessage != nil ) then

if ( ... != nil ) then
errorMessage = Format( errorMessage, ... )
end

else
errorMessage = "assertion failed!"
end

if ( expression ) then
return expression, errorMessage, ...
end

errorLevel = errorLevel or 1

if noHalt then
ProtectedCall( error, errorMessage, errorLevel + 2 )
else
error( errorMessage, errorLevel + 1 )
end

end

--
-- You can use this function to add your own CLASS_ var.
-- Adding in this way will ensure your CLASS_ doesn't collide with another
Expand Down