fix: correct json response for errors (#2813)

Fixes #2811

Signed-off-by: Roman Gershman <roman@dragonflydb.io>
This commit is contained in:
Roman Gershman 2024-04-01 22:56:26 +03:00 committed by GitHub
parent b2e2ad6e04
commit d3b90c8210
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 3 deletions

View file

@ -124,7 +124,7 @@ struct CaptureVisitor {
}
void operator()(CapturingReplyBuilder::Error err) {
str = absl::StrCat(R"({"error": ")", err.first);
str = absl::StrCat(R"({"error": ")", err.first, "\"");
}
void operator()(facade::OpStatus status) {
@ -132,7 +132,13 @@ struct CaptureVisitor {
}
void operator()(const CapturingReplyBuilder::StrArrPayload& sa) {
absl::StrAppend(&str, "not_implemented");
absl::StrAppend(&str, "[");
for (const auto& val : sa.arr) {
absl::StrAppend(&str, JsonEscape(val), ",");
}
if (sa.arr.size())
str.pop_back();
absl::StrAppend(&str, "]");
}
void operator()(unique_ptr<CapturingReplyBuilder::CollectionPayload> cp) {
@ -152,7 +158,18 @@ struct CaptureVisitor {
}
void operator()(facade::SinkReplyBuilder::MGetResponse resp) {
absl::StrAppend(&str, "not_implemented");
absl::StrAppend(&str, "[");
for (const auto& val : resp.resp_arr) {
if (val) {
absl::StrAppend(&str, JsonEscape(val->value), ",");
} else {
absl::StrAppend(&str, "null,");
}
}
if (resp.resp_arr.size())
str.pop_back();
absl::StrAppend(&str, "]");
}
void operator()(const CapturingReplyBuilder::ScoredArray& sarr) {

View file

@ -766,4 +766,11 @@ async def test_http(df_server: DflyInstance):
assert resp.status == 200
text = await resp.text()
assert text.strip() == '{"result":"МайяХилли"}'
body = '["foo", "bar"]'
async with session.post(f"http://localhost:{df_server.port}/api", data=body) as resp:
assert resp.status == 200
text = await resp.text()
assert text.strip() == '{"error": "unknown command `FOO`"}'
assert await client.ttl("foo") > 0