From 2a1815c4601015a3a5a0a21e670451752f36873b Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Thu, 25 Aug 2022 18:49:07 +1000 Subject: [PATCH] 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. --- imports/callback/client.lua | 20 +++++++++++++++++--- imports/callback/server.lua | 20 +++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/imports/callback/client.lua b/imports/callback/client.lua index c25479e..7cc2ee4 100644 --- a/imports/callback/client.lua +++ b/imports/callback/client.lua @@ -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 diff --git a/imports/callback/server.lua b/imports/callback/server.lua index a02d624..c02d85f 100644 --- a/imports/callback/server.lua +++ b/imports/callback/server.lua @@ -28,10 +28,10 @@ local function triggerClientCallback(_, event, playerId, 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 @@ -51,12 +51,26 @@ function callback.await(event, playerId, ...) return triggerClientCallback(_, event, playerId, 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 client requests. function callback.register(name, cb) 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