diff --git a/sandbox/errorHandling.lua b/sandbox/errorHandling.lua index 6d19281..c0579ad 100644 --- a/sandbox/errorHandling.lua +++ b/sandbox/errorHandling.lua @@ -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 @@ -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 @@ -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() @@ -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 @@ -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() @@ -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 \ No newline at end of file diff --git a/sandbox/main.lua b/sandbox/main.lua index 25e80b6..6db1b93 100755 --- a/sandbox/main.lua +++ b/sandbox/main.lua @@ -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