Skip to content

Commit

Permalink
Added better Error handling and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ThymonA committed Jun 13, 2020
1 parent 8cf9bd6 commit 6eddc79
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fxcore_error.log
93 changes: 90 additions & 3 deletions lib/error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,108 @@

Error = class()

--
-- Log a error in file `fxcore_error.log`
-- @msg string Error Message
-- @resource string Resource name
-- @module string Module name
--
function Error:Log(msg, resource, module)
if (resource ~= nil and resource ~= '' and tostring(resource) ~= '') then
resource = (' [%s]'):format(tostring(resource))
elseif (CurrentFrameworkResource ~= nil and CurrentFrameworkResource ~= '' and tostring(CurrentFrameworkResource) ~= '') then
resource = (' [%s]'):format(tostring(CurrentFrameworkResource))
else
resource = ''
end

if (module ~= nil and module ~= '' and tostring(module) ~= '') then
module = (' (%s)'):format(tostring(module))
elseif (CurrentFrameworkModule ~= nil and CurrentFrameworkModule ~= '' and tostring(CurrentFrameworkModule) ~= '') then
module = (' (%s)'):format(tostring(CurrentFrameworkModule))
else
module = ''
end

local currentFile = LoadResourceFile(GetCurrentResourceName(), 'fxcore_error.log') or ''

if (currentFile) then
else
currentFile = ''
end

local date_table = os.date("*t")
local hour, minute, second = date_table.hour, date_table.min, date_table.sec
local year, month, day = date_table.year, date_table.month, date_table.day
local timestring = tostring(year)

if (month < 10) then
timestring = ('%s-0%s'):format(timestring, month)
else
timestring = ('%s-%s'):format(timestring, month)
end

if (day < 10) then
timestring = ('%s-0%s'):format(timestring, day)
else
timestring = ('%s-%s'):format(timestring, day)
end

if (hour < 10) then
timestring = ('%s 0%s'):format(timestring, hour)
else
timestring = ('%s %s'):format(timestring, hour)
end

if (minute < 10) then
timestring = ('%s:0%s'):format(timestring, minute)
else
timestring = ('%s:%s'):format(timestring, minute)
end

if (second < 10) then
timestring = ('%s:0%s'):format(timestring, second)
else
timestring = ('%s:%s'):format(timestring, second)
end

local newData = ('%s%s ERROR%s%s %s\n'):format(currentFile, timestring, resource, module, msg)

SaveResourceFile(GetCurrentResourceName(), 'fxcore_error.log', newData)
end

--
-- Create a error message
-- @msg string Error Message
-- @resource string Resource name
-- @module string Module name
--
function Error:Print(msg, resource, module)
Error:Log(msg, resource, module)

if (resource ~= nil and resource ~= '' and tostring(resource) ~= '') then
resource = ('[ %s ] '):format(tostring(resource))
resource = ('[+] RESOURCE: %s\n'):format(tostring(resource))
elseif (CurrentFrameworkResource ~= nil and CurrentFrameworkResource ~= '' and tostring(CurrentFrameworkResource) ~= '') then
resource = ('[+] RESOURCE: %s\n'):format(tostring(CurrentFrameworkResource))
else
resource = ''
end

if (module ~= nil and module ~= '' and tostring(module) ~= '') then
module = ('[ %s ] '):format(tostring(module))
module = ('[+] MODULE: %s\n'):format(tostring(module))
elseif (CurrentFrameworkModule ~= nil and CurrentFrameworkModule ~= '' and tostring(CurrentFrameworkModule) ~= '') then
module = ('[+] MODULE: %s\n'):format(tostring(CurrentFrameworkModule))
else
module = ''
end

print(('[FXCore] [ERROR] %s%s> %s'):format(resource, module, msg))
local errorType = 'MODULE'

if (module == '' and resource ~= '') then
errorType = 'RESOURCE'
elseif (module == '' and resource == '') then
errorType = 'FRAMEWORK'
end

print(('[+] ---------------------------------------------------------------------------\n%s%s\n[+] ERROR TYPE: %s\n[+] ERROR: %s\n[+] ---------------------------------------------------------------------------'):format(resource, module, errorType, msg))
end
35 changes: 35 additions & 0 deletions lib/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,35 @@ function Module:Create(name, func)
return false
end

function _object:Get()
return self.value or nil
end

function _object:Execute()
if (func ~= nil and self:CanStart()) then
if (#self.params <= 0) then
self.value = self.func()
else
local _params = {}

for _, param in pairs(self.params or {}) do
local key = string.lower(tostring(param))
local module = (((Module.Modules or nil)[key] or nil)) or nil

if (module ~= nil) then
table.insert(_params, module:Get())
else
table.insert(_params, nil)
end
end

self.value = self.func(table.unpack(_params))
end
else
error(("Can't start module '%s'"):format(self.name or 'unknown'))
end
end

return _object
end

Expand All @@ -122,6 +151,8 @@ end
-- @func function Function to load module
--
function Module:Load(name, func, override)
_ENV.CurrentFrameworkModule = name

if (name == nil or tostring(name) == '' or name == '') then
error('Module name is required', 3)
return
Expand All @@ -135,6 +166,10 @@ function Module:Load(name, func, override)
end

local module = Module:Create(name, func)

if (module:CanStart()) then
module:Execute()
end
end

-- FiveM manipulation
Expand Down
3 changes: 3 additions & 0 deletions lib/resource.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,7 @@ function Resource:ExecuteResources()
end
end
end

_ENV.CurrentFrameworkResource = nil
_ENV.CurrentFrameworkModule = nil
end

0 comments on commit 6eddc79

Please sign in to comment.