Skip to content

Commit

Permalink
shared: rpc now expects errors to be returned rather than throwed
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Oct 1, 2023
1 parent 2d097da commit 9c6576e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
22 changes: 12 additions & 10 deletions client/src/CV8Resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@ void CV8ResourceImpl::HandleServerRPC(alt::CScriptRPCEvent* ev)
{
auto handler = rpcHandlers.find(ev->GetName());

ev->WillAnswer();

if (handler == rpcHandlers.end())
{
ev->WillAnswer();

std::string errorMessage = "Rpc with that name was not registered";
auto returnValue = V8Helpers::V8ToMValue(v8::Undefined(isolate));
alt::ICore::Instance().TriggerServerRPCAnswer(ev->GetAnswerID(), returnValue, errorMessage);
Expand All @@ -353,7 +353,6 @@ void CV8ResourceImpl::HandleServerRPC(alt::CScriptRPCEvent* ev)
std::vector<v8::Local<v8::Value>> args;
V8Helpers::MValueArgsToV8(ev->GetArgs(), args);

v8::TryCatch tryCatch(isolate);
auto result = V8Helpers::CallFunctionWithTimeout(handler->second.Get(isolate), context, args);

v8::Local<v8::Value> returnValue;
Expand Down Expand Up @@ -384,15 +383,18 @@ void CV8ResourceImpl::HandleServerRPC(alt::CScriptRPCEvent* ev)
}
}

ev->WillAnswer();

// Retrieve returned error message when an error was returned
std::string errorMessage;
if (tryCatch.HasCaught())
{
errorMessage = "Unknown error";
if (returnValue->IsNativeError()) {
v8::Local<v8::Value> exception = returnValue.As<v8::Value>();

if (!tryCatch.Message().IsEmpty())
errorMessage = *v8::String::Utf8Value(isolate, tryCatch.Message()->Get());
v8::String::Utf8Value messageValue(isolate, exception->ToString(isolate->GetCurrentContext()).ToLocalChecked());
errorMessage = *messageValue;

// Strip exception prefix
if (size_t colonPos = errorMessage.find(':'); colonPos != std::string::npos) {
errorMessage = errorMessage.substr(colonPos + 2);
}
}

alt::ICore::Instance().TriggerServerRPCAnswer(ev->GetAnswerID(), V8Helpers::V8ToMValue(returnValue), errorMessage);
Expand Down
22 changes: 12 additions & 10 deletions server/src/CNodeResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ void CNodeResourceImpl::HandleClientRpcEvent(alt::CScriptRPCEvent* ev)
{
auto handler = rpcHandlers.find(ev->GetName());

ev->WillAnswer();

if (handler == rpcHandlers.end())
{
ev->WillAnswer();

std::string errorMessage = "Rpc with that name was not registered";
auto returnValue = V8Helpers::V8ToMValue(v8::Undefined(isolate));
alt::ICore::Instance().TriggerClientRPCAnswer(ev->GetTarget(), ev->GetAnswerID(), returnValue, errorMessage);
Expand All @@ -280,7 +280,6 @@ void CNodeResourceImpl::HandleClientRpcEvent(alt::CScriptRPCEvent* ev)
args.push_back(GetBaseObjectOrNull(ev->GetTarget()));
V8Helpers::MValueArgsToV8(ev->GetArgs(), args);

v8::TryCatch tryCatch(isolate);
auto result = V8Helpers::CallFunctionWithTimeout(handler->second.Get(isolate), context, args);

v8::Local<v8::Value> returnValue;
Expand Down Expand Up @@ -311,15 +310,18 @@ void CNodeResourceImpl::HandleClientRpcEvent(alt::CScriptRPCEvent* ev)
}
}

ev->WillAnswer();

// Retrieve returned error message when an error was returned
std::string errorMessage;
if (tryCatch.HasCaught())
{
errorMessage = "Unknown error";
if (returnValue->IsNativeError()) {
v8::Local<v8::Value> exception = returnValue.As<v8::Value>();

if (!tryCatch.Message().IsEmpty())
errorMessage = *v8::String::Utf8Value(isolate, tryCatch.Message()->Get());
v8::String::Utf8Value messageValue(isolate, exception->ToString(isolate->GetCurrentContext()).ToLocalChecked());
errorMessage = *messageValue;

// Strip exception prefix
if (size_t colonPos = errorMessage.find(':'); colonPos != std::string::npos) {
errorMessage = errorMessage.substr(colonPos + 2);
}
}

alt::ICore::Instance().TriggerClientRPCAnswer(ev->GetTarget(), ev->GetAnswerID(), V8Helpers::V8ToMValue(returnValue), errorMessage);
Expand Down

0 comments on commit 9c6576e

Please sign in to comment.