diff --git a/client/src/CV8Resource.cpp b/client/src/CV8Resource.cpp index b63661cc..288c69a2 100644 --- a/client/src/CV8Resource.cpp +++ b/client/src/CV8Resource.cpp @@ -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); @@ -353,7 +353,6 @@ void CV8ResourceImpl::HandleServerRPC(alt::CScriptRPCEvent* ev) std::vector> args; V8Helpers::MValueArgsToV8(ev->GetArgs(), args); - v8::TryCatch tryCatch(isolate); auto result = V8Helpers::CallFunctionWithTimeout(handler->second.Get(isolate), context, args); v8::Local returnValue; @@ -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 exception = returnValue.As(); - 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); diff --git a/server/src/CNodeResourceImpl.cpp b/server/src/CNodeResourceImpl.cpp index d9ac7e2a..32825b06 100644 --- a/server/src/CNodeResourceImpl.cpp +++ b/server/src/CNodeResourceImpl.cpp @@ -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); @@ -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 returnValue; @@ -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 exception = returnValue.As(); - 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);