1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-29 22:36:38 +02:00

relay/api: use cjson lib to return errors

This commit is contained in:
Sébastien Helleu
2025-01-07 07:23:55 +01:00
parent d10af1037b
commit 9d3388b09e
2 changed files with 33 additions and 39 deletions
+21 -27
View File
@@ -248,7 +248,7 @@ relay_api_msg_send_error_json (struct t_relay_client *client,
{
cJSON *json;
int num_bytes;
char *error_msg, *str_json;
char *str_json;
if (!client || !message || !format)
return -1;
@@ -259,43 +259,37 @@ relay_api_msg_send_error_json (struct t_relay_client *client,
num_bytes = -1;
json = cJSON_CreateObject ();
if (!json)
return -1;
cJSON_AddItemToObject (json, "error", cJSON_CreateString (vbuffer));
if (client->websocket == RELAY_CLIENT_WEBSOCKET_READY)
{
/*
* with established websocket, we return JSON string instead of
* an HTTP response
*/
json = cJSON_CreateObject ();
if (json)
{
cJSON_AddItemToObject (json, "error", cJSON_CreateString (vbuffer));
num_bytes = relay_api_msg_send_json_internal (
client,
return_code,
message,
NULL, /* event_name */
-1, /* event_buffer_id */
headers,
NULL, /* body_type */
json);
cJSON_Delete (json);
}
num_bytes = relay_api_msg_send_json_internal (
client,
return_code,
message,
NULL, /* event_name */
-1, /* event_buffer_id */
headers,
NULL, /* body_type */
json);
}
else
{
error_msg = weechat_string_replace (vbuffer, "\"", "\\\"");
if (error_msg)
{
if (weechat_asprintf (&str_json, "{\"error\": \"%s\"}", error_msg) >= 0)
{
num_bytes = relay_http_send_json (client, return_code, message,
headers, str_json);
free (str_json);
}
free (error_msg);
}
str_json = cJSON_PrintUnformatted (json);
num_bytes = relay_http_send_json (client, return_code, message,
headers, str_json);
free (str_json);
}
cJSON_Delete (json);
free (vbuffer);
return num_bytes;
}
@@ -379,9 +379,9 @@ TEST(RelayApiProtocolWithClient, CbBuffers)
STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Content-Type: application/json; charset=utf-8\r\n"
"Content-Length: 41\r\n"
"Content-Length: 40\r\n"
"\r\n"
"{\"error\": \"Buffer \\\"invalid\\\" not found\"}",
"{\"error\":\"Buffer \\\"invalid\\\" not found\"}",
data_sent[0]);
/* error: invalid buffer id */
@@ -389,9 +389,9 @@ TEST(RelayApiProtocolWithClient, CbBuffers)
STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Content-Type: application/json; charset=utf-8\r\n"
"Content-Length: 37\r\n"
"Content-Length: 36\r\n"
"\r\n"
"{\"error\": \"Buffer \\\"123\\\" not found\"}",
"{\"error\":\"Buffer \\\"123\\\" not found\"}",
data_sent[0]);
/* error: invalid sub-resource */
@@ -399,9 +399,9 @@ TEST(RelayApiProtocolWithClient, CbBuffers)
STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Content-Type: application/json; charset=utf-8\r\n"
"Content-Length: 59\r\n"
"Content-Length: 58\r\n"
"\r\n"
"{\"error\": \"Sub-resource of buffers not found: \\\"invalid\\\"\"}",
"{\"error\":\"Sub-resource of buffers not found: \\\"invalid\\\"\"}",
data_sent[0]);
/* error: too many parameters in path */
@@ -626,9 +626,9 @@ TEST(RelayApiProtocolWithClient, CbCompletion)
STRCMP_EQUAL("HTTP/1.1 400 Bad Request\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Content-Type: application/json; charset=utf-8\r\n"
"Content-Length: 41\r\n"
"Content-Length: 40\r\n"
"\r\n"
"{\"error\": \"Buffer \\\"invalid\\\" not found\"}",
"{\"error\":\"Buffer \\\"invalid\\\" not found\"}",
data_sent[0]);
/* on core buffer, with buffer name. examples from relay protocol examples:
@@ -705,9 +705,9 @@ TEST(RelayApiProtocolWithClient, CbInput)
STRCMP_EQUAL("HTTP/1.1 400 Bad Request\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Content-Type: application/json; charset=utf-8\r\n"
"Content-Length: 41\r\n"
"Content-Length: 40\r\n"
"\r\n"
"{\"error\": \"Buffer \\\"invalid\\\" not found\"}",
"{\"error\":\"Buffer \\\"invalid\\\" not found\"}",
data_sent[0]);
/* on core buffer, without buffer name */
@@ -786,9 +786,9 @@ TEST(RelayApiProtocolWithClient, CbSync)
STRCMP_EQUAL("HTTP/1.1 403 Forbidden\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Content-Type: application/json; charset=utf-8\r\n"
"Content-Length: 72\r\n"
"Content-Length: 71\r\n"
"\r\n"
"{\"error\": \"Sync resource is available only with "
"{\"error\":\"Sync resource is available only with "
"a websocket connection\"}",
data_sent[0]);
}