Skip to content

Commit

Permalink
Merge pull request #19 from StarryWisdom/nosetcomms-error
Browse files Browse the repository at this point in the history
Nosetcomms error
  • Loading branch information
Xansta committed Jan 25, 2023
2 parents c919769 + 8f0aee5 commit fe9a9f7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
63 changes: 58 additions & 5 deletions sandbox/errorHandling.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
-- but there is a stock lua package with that name (which isnt included in Empty Epsilon)
errorHandling = {}

-- note - setCommsScript is not wrapped unlike setCommsFunction
-- I am somewhat unsure if it is possible to wrap or not

-- please note - this file is likely to become either irrelevant or in need of update when the EE ECS rework is complete
-- it would be nice if the whole walking through the class hierarchy was moved out into its own file
-- however that walking is likely to be made obsolete semi soon with the ECS system
Expand All @@ -14,12 +17,24 @@ errorHandling = {}
-- the latter is better in my eyes
-- I am planning personally on waiting ECS changes have been implemented and I'm more sure on whats happening
-- PlayerShips created via the gm screen are ***NOT*** wrapped - this would be fairly easy to fix via wrapping on onNewPlayerShip
-- If there is a use case people want this for I will add that happily

function errorHandling:onError(err, replacementTraceback)
if self._onError then
if replacementTraceback then
self._onError(err,replacementTraceback)
else
-- it might be nice to suppress the first few lines of the traceback (the ones inside of errorHandling)
self._onError(err,traceback())
end
end
end

function errorHandling:callWithErrorHandling(fun,...)
assert(type(fun)=="function" or fun==nil)
if fun ~= nil then
return xpcall(fun, self.onError, ...)
return xpcall(fun, function(...)
self:onError(...)
end, ...)
end
end

Expand Down Expand Up @@ -47,7 +62,7 @@ function errorHandling:_autoWrapArgX(originalFunction, argToWrap)
end
end

function errorHandling:WormHole()
function errorHandling:_WormHole()
local create = WormHole
return function()
local worm = create()
Expand Down Expand Up @@ -198,8 +213,46 @@ function errorHandling:_AddMissileErrorHandling(missile)
return missile
end

-- this will call function fn
-- if during the call of fn setCommsMessage is not called then we will call onError if myTraceback has been sent
-- this is due to needing to call this during the setCommsFunction callback (where no setCommsMessage is sometimes useful)
-- and within addCommsReply (where it is not useful)
function errorHandling:_checkRelayMessageSent(fn,myTraceback)
return function ()
local _setCommsMessage = setCommsMessage
local _addCommsReply = addCommsReply
-- the reversed boolean is annoying, but I am unsure (and am not sure how to confirm)
-- if a local is set to nil will it be a local or a global (also does it change while its an upvalue?)
local messageUnset = true
setCommsMessage = function(msg)
messageUnset = nil
_setCommsMessage(msg)
end
addCommsReply = function (text, fn)
return _addCommsReply(text,self:_checkRelayMessageSent(fn,traceback()))
end
local ret = fn()
if messageUnset and myTraceback then
self:onError("A function set via addCommsReply has been called, but setCommsMessage wasn't called. This will result in relay seeing \"?\"\n Traceback of the function setting the callback is as follows \n\n",myTraceback)
end
setCommsMessage = _setCommsMessage
addCommsReply = _addCommsReply
return ret
end
end

function errorHandling:_wrapSetCommsFunction(object, _setCommsFunction)
return function (object,fn)
return _setCommsFunction(object,self:_checkRelayMessageSent(fn,nil))
end
end

function errorHandling:_AddSpaceObjectErrorHandling(object)
-- we deal with setCommsFunction in two parts
-- first we are trapping errors happening inside of the call
object.setCommsFunction = self:_autoWrapArgX(object.setCommsFunction,2)
-- secondly we are ensuring that setCommsMessage is called at some point
object.setCommsFunction = self:_wrapSetCommsFunction(object,object.setCommsFunction)
object.onDestroyed = self:_autoWrapArgX(object.onDestroyed,2)
return object
end
Expand Down Expand Up @@ -233,7 +286,7 @@ function errorHandling:_wrapAllFunctions()
update = self:wrapWithErrorHandling(update)
init = self:wrapWithErrorHandling(init)

WormHole = self:WormHole()
WormHole = self:_WormHole()
WarpJammer = self:_WarpJammer()
SupplyDrop = self:_SupplyDrop()
PlayerSpaceship = self:_PlayerSpaceship()
Expand Down Expand Up @@ -265,6 +318,6 @@ end
-- a natural location IMO is as the last line of the main script
function errorHandling:wrapAllFunctions(onError)
assert(type(onError) == "function")
self.onError = onError
self._onError = onError
self:callWithErrorHandling(self._wrapAllFunctions,self,onError)
end
4 changes: 2 additions & 2 deletions sandbox/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52842,8 +52842,8 @@ function updateCommerce(assets,region_station)
end --of zero item check branch
end --not nil commercial asset list check branch
end
function onError(error)
local err = "script error : - \n" .. error .. "\n\ntraceback :-\n" .. traceback()
function onError(error, errorTraceback)
local err = "script error : - \n" .. error .. "\n\ntraceback :-\n" .. errorTraceback
print(err)
addGMMessage(err)
end
Expand Down

0 comments on commit fe9a9f7

Please sign in to comment.