mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 07:56:02 +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
|
events[key] = nil
|
||||||
|
|
||||||
if promise then
|
if promise then
|
||||||
return promise:resolve(response)
|
return promise:resolve(response or {})
|
||||||
end
|
end
|
||||||
|
|
||||||
return cb and cb(table.unpack(response))
|
return cb and cb(table.unpack(response or {}))
|
||||||
end
|
end
|
||||||
|
|
||||||
if promise then
|
if promise then
|
||||||
@@ -70,12 +70,26 @@ function callback.await(event, delay, ...)
|
|||||||
return triggerServerCallback(_, event, delay, false, ...)
|
return triggerServerCallback(_, event, delay, false, ...)
|
||||||
end
|
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 name string
|
||||||
---@param cb function
|
---@param cb function
|
||||||
--- Registers an event handler and callback function to respond to server requests.
|
--- Registers an event handler and callback function to respond to server requests.
|
||||||
function callback.register(name, cb)
|
function callback.register(name, cb)
|
||||||
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
||||||
TriggerServerEvent(cbEvent:format(resource), key, { cb(...) })
|
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -28,10 +28,10 @@ local function triggerClientCallback(_, event, playerId, cb, ...)
|
|||||||
events[key] = nil
|
events[key] = nil
|
||||||
|
|
||||||
if promise then
|
if promise then
|
||||||
return promise:resolve(response)
|
return promise:resolve(response or {})
|
||||||
end
|
end
|
||||||
|
|
||||||
return cb and cb(table.unpack(response))
|
return cb and cb(table.unpack(response or {}))
|
||||||
end
|
end
|
||||||
|
|
||||||
if promise then
|
if promise then
|
||||||
@@ -51,12 +51,26 @@ function callback.await(event, playerId, ...)
|
|||||||
return triggerClientCallback(_, event, playerId, false, ...)
|
return triggerClientCallback(_, event, playerId, false, ...)
|
||||||
end
|
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 name string
|
||||||
---@param cb function
|
---@param cb function
|
||||||
--- Registers an event handler and callback function to respond to client requests.
|
--- Registers an event handler and callback function to respond to client requests.
|
||||||
function callback.register(name, cb)
|
function callback.register(name, cb)
|
||||||
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
||||||
TriggerClientEvent(cbEvent:format(resource), source, key, { cb(source, ...) })
|
TriggerClientEvent(cbEvent:format(resource), source, key, callbackResponse(pcall(cb, source, ...)))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user