mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
tweak(callback): error handling
Errors during callbacks will never respond, so we'll use a protected call and format the error message with a normal print to keep the thread alive.
This commit is contained in:
@@ -47,10 +47,10 @@ local function triggerServerCallback(_, event, delay, cb, ...)
|
||||
events[key] = nil
|
||||
|
||||
if promise then
|
||||
return promise:resolve(response)
|
||||
return promise:resolve(response or {})
|
||||
end
|
||||
|
||||
return cb and cb(table.unpack(response))
|
||||
return cb and cb(table.unpack(response or {}))
|
||||
end
|
||||
|
||||
if promise then
|
||||
@@ -70,12 +70,26 @@ function callback.await(event, delay, ...)
|
||||
return triggerServerCallback(_, event, delay, false, ...)
|
||||
end
|
||||
|
||||
local function callbackResponse(success, result, ...)
|
||||
if not success then
|
||||
if result then
|
||||
return print(('^1SCRIPT ERROR: %s^0\n%s'):format(result , Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString()) or ''))
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
return { result, ... }
|
||||
end
|
||||
|
||||
local pcall = pcall
|
||||
|
||||
---@param name string
|
||||
---@param cb function
|
||||
--- Registers an event handler and callback function to respond to server requests.
|
||||
function callback.register(name, cb)
|
||||
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
||||
TriggerServerEvent(cbEvent:format(resource), key, { cb(...) })
|
||||
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user