Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2f4a022

Browse files
Return proper JSON-RPC error responses from service protocol failures (#5889)
Fixes flutter/flutter#19571
1 parent d34eb1d commit 2f4a022

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

shell/common/shell.cc

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -773,19 +773,28 @@ Shell::GetServiceProtocolDescription() const {
773773
}
774774

775775
static void ServiceProtocolParameterError(rapidjson::Document& response,
776-
std::string parameter_name) {
776+
std::string error_details) {
777777
auto& allocator = response.GetAllocator();
778778
response.SetObject();
779779
const int64_t kInvalidParams = -32602;
780780
response.AddMember("code", kInvalidParams, allocator);
781781
response.AddMember("message", "Invalid params", allocator);
782782
{
783783
rapidjson::Value details(rapidjson::kObjectType);
784-
details.AddMember("details", parameter_name, allocator);
784+
details.AddMember("details", error_details, allocator);
785785
response.AddMember("data", details, allocator);
786786
}
787787
}
788788

789+
static void ServiceProtocolFailureError(rapidjson::Document& response,
790+
std::string message) {
791+
auto& allocator = response.GetAllocator();
792+
response.SetObject();
793+
const int64_t kJsonServerError = -32000;
794+
response.AddMember("code", kJsonServerError, allocator);
795+
response.AddMember("message", message, allocator);
796+
}
797+
789798
// Service protocol handler
790799
bool Shell::OnServiceProtocolScreenshot(
791800
const blink::ServiceProtocol::Handler::ServiceProtocolMap& params,
@@ -803,8 +812,7 @@ bool Shell::OnServiceProtocolScreenshot(
803812
response.AddMember("screenshot", image, allocator);
804813
return true;
805814
}
806-
ServiceProtocolParameterError(response,
807-
"Could not capture image screenshot.");
815+
ServiceProtocolFailureError(response, "Could not capture image screenshot.");
808816
return false;
809817
}
810818

@@ -825,7 +833,7 @@ bool Shell::OnServiceProtocolScreenshotSKP(
825833
response.AddMember("skp", skp, allocator);
826834
return true;
827835
}
828-
ServiceProtocolParameterError(response, "Could not capture SKP screenshot.");
836+
ServiceProtocolFailureError(response, "Could not capture SKP screenshot.");
829837
return false;
830838
}
831839

@@ -897,7 +905,8 @@ bool Shell::OnServiceProtocolRunInView(
897905
return true;
898906
} else {
899907
FML_DLOG(ERROR) << "Could not run configuration in engine.";
900-
response.AddMember("type", "Failure", allocator);
908+
ServiceProtocolFailureError(response,
909+
"Could not run configuration in engine.");
901910
return false;
902911
}
903912

@@ -951,7 +960,7 @@ bool Shell::OnServiceProtocolSetAssetBundlePath(
951960
return true;
952961
} else {
953962
FML_DLOG(ERROR) << "Could not update asset directory.";
954-
response.AddMember("type", "Failure", allocator);
963+
ServiceProtocolFailureError(response, "Could not update asset directory.");
955964
return false;
956965
}
957966

0 commit comments

Comments
 (0)