forked from ground/cground
Update error handling inside extlibs
This commit is contained in:
@@ -50,21 +50,11 @@ size_t write_callback(void* ptr, size_t size, size_t nmemb, void* userdata) {
|
||||
|
||||
// GET request
|
||||
GroundValue get_request(GroundScope* scope, List args) {
|
||||
if (args.size < 1) {
|
||||
printf("Requires a URL argument\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
if (args.values[0].type != STRING) {
|
||||
printf("Arg 1 needs to be a string (URL)\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
|
||||
curl_setup();
|
||||
CURL* handle = curl_easy_init();
|
||||
|
||||
if (!handle) {
|
||||
printf("Curl failed to init\n");
|
||||
return groundCreateValue(NONE);
|
||||
ERROR("CURL failed to init", "GenericRequestError");
|
||||
}
|
||||
|
||||
ResponseBuffer buffer = {0};
|
||||
@@ -81,10 +71,9 @@ GroundValue get_request(GroundScope* scope, List args) {
|
||||
CURLcode result = curl_easy_perform(handle);
|
||||
|
||||
if (result != CURLE_OK) {
|
||||
printf("Curl request failed: %s\n", curl_easy_strerror(result));
|
||||
free(buffer.data);
|
||||
curl_easy_cleanup(handle);
|
||||
return groundCreateValue(NONE);
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf) - 1, "Curl request failed: %s\n", curl_easy_strerror(result));
|
||||
ERROR(buf, "ActionRequestError");
|
||||
}
|
||||
|
||||
curl_easy_cleanup(handle);
|
||||
@@ -95,21 +84,11 @@ GroundValue get_request(GroundScope* scope, List args) {
|
||||
|
||||
// POST request
|
||||
GroundValue post_request(GroundScope* scope, List args) {
|
||||
if (args.size < 2) {
|
||||
printf("Requires URL and POST data arguments\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
if (args.values[0].type != STRING || args.values[1].type != STRING) {
|
||||
printf("Both arguments need to be strings (URL, POST data)\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
|
||||
curl_setup();
|
||||
CURL* handle = curl_easy_init();
|
||||
|
||||
if (!handle) {
|
||||
printf("Curl failed to init\n");
|
||||
return groundCreateValue(NONE);
|
||||
ERROR("CURL failed to init", "GenericRequestError");
|
||||
}
|
||||
|
||||
ResponseBuffer buffer = {0};
|
||||
@@ -127,10 +106,11 @@ GroundValue post_request(GroundScope* scope, List args) {
|
||||
CURLcode result = curl_easy_perform(handle);
|
||||
|
||||
if (result != CURLE_OK) {
|
||||
printf("Curl POST request failed: %s\n", curl_easy_strerror(result));
|
||||
free(buffer.data);
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf) - 1, "Curl request failed: %s\n", curl_easy_strerror(result));
|
||||
curl_easy_cleanup(handle);
|
||||
return groundCreateValue(NONE);
|
||||
ERROR(buf, "ActionRequestError");
|
||||
}
|
||||
|
||||
curl_easy_cleanup(handle);
|
||||
@@ -153,27 +133,16 @@ int find_ws_slot() {
|
||||
|
||||
// WebSocket Connect - returns connection ID as INT
|
||||
GroundValue ws_connect(GroundScope* scope, List args) {
|
||||
if (args.size < 1) {
|
||||
printf("Requires a WebSocket URL argument (ws:// or wss://)\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
if (args.values[0].type != STRING) {
|
||||
printf("Arg 1 needs to be a string (WebSocket URL)\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
|
||||
curl_setup();
|
||||
|
||||
int slot = find_ws_slot();
|
||||
if (slot == -1) {
|
||||
printf("Maximum WebSocket connections reached\n");
|
||||
return groundCreateValue(NONE);
|
||||
ERROR("Maximum WebSocket connections reached", "OverflowError");
|
||||
}
|
||||
|
||||
CURL* handle = curl_easy_init();
|
||||
if (!handle) {
|
||||
printf("Curl failed to init\n");
|
||||
return groundCreateValue(NONE);
|
||||
ERROR("CURL failed to init", "GenericWSError");
|
||||
}
|
||||
|
||||
curl_easy_setopt(handle, CURLOPT_URL, args.values[0].data.stringVal);
|
||||
@@ -183,9 +152,10 @@ GroundValue ws_connect(GroundScope* scope, List args) {
|
||||
CURLcode result = curl_easy_perform(handle);
|
||||
|
||||
if (result != CURLE_OK) {
|
||||
printf("WebSocket connection failed: %s\n", curl_easy_strerror(result));
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf) - 1, "Curl WebSocket failed: %s\n", curl_easy_strerror(result));
|
||||
curl_easy_cleanup(handle);
|
||||
return groundCreateValue(NONE);
|
||||
ERROR(buf, "ActionWSError");
|
||||
}
|
||||
|
||||
ws_connections[slot].handle = handle;
|
||||
@@ -197,19 +167,9 @@ GroundValue ws_connect(GroundScope* scope, List args) {
|
||||
|
||||
// WebSocket Send - sends a text message, returns bytes sent as INT
|
||||
GroundValue ws_send(GroundScope* scope, List args) {
|
||||
if (args.size < 2) {
|
||||
printf("Requires connection ID and message\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
if (args.values[0].type != INT || args.values[1].type != STRING) {
|
||||
printf("Args need to be (INT connection ID, STRING message)\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
|
||||
int conn_id = (int)args.values[0].data.intVal;
|
||||
if (conn_id < 0 || conn_id >= MAX_WS_CONNECTIONS || !ws_connections[conn_id].connected) {
|
||||
printf("Invalid or closed WebSocket connection\n");
|
||||
return groundCreateValue(NONE);
|
||||
ERROR("Invalid WebSocket connection ID", "GenericWSError");
|
||||
}
|
||||
|
||||
CURL* handle = ws_connections[conn_id].handle;
|
||||
@@ -219,8 +179,9 @@ GroundValue ws_send(GroundScope* scope, List args) {
|
||||
CURLcode result = curl_ws_send(handle, message, strlen(message), &sent, 0, CURLWS_TEXT);
|
||||
|
||||
if (result != CURLE_OK) {
|
||||
printf("WebSocket send failed: %s\n", curl_easy_strerror(result));
|
||||
return groundCreateValue(NONE);
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf) - 1, "WebSocket send failed: %s\n", curl_easy_strerror(result));
|
||||
ERROR(buf, "ActionWSError");
|
||||
}
|
||||
|
||||
return groundCreateValue(INT, (int64_t)sent);
|
||||
@@ -228,19 +189,9 @@ GroundValue ws_send(GroundScope* scope, List args) {
|
||||
|
||||
// WebSocket Receive - receives a message (blocking)
|
||||
GroundValue ws_receive(GroundScope* scope, List args) {
|
||||
if (args.size < 1) {
|
||||
printf("Requires connection ID\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
if (args.values[0].type != INT) {
|
||||
printf("Arg 1 needs to be an INT (connection ID)\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
|
||||
int conn_id = (int)args.values[0].data.intVal;
|
||||
if (conn_id < 0 || conn_id >= MAX_WS_CONNECTIONS || !ws_connections[conn_id].connected) {
|
||||
printf("Invalid or closed WebSocket connection\n");
|
||||
return groundCreateValue(NONE);
|
||||
ERROR("Invalid WebSocket connection ID", "GenericWSError");
|
||||
}
|
||||
|
||||
CURL* handle = ws_connections[conn_id].handle;
|
||||
@@ -255,8 +206,9 @@ GroundValue ws_receive(GroundScope* scope, List args) {
|
||||
// No data available right now
|
||||
return groundCreateValue(STRING, "");
|
||||
}
|
||||
printf("WebSocket receive failed: %s\n", curl_easy_strerror(result));
|
||||
return groundCreateValue(NONE);
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf) - 1, "WebSocket receive failed: %s\n", curl_easy_strerror(result));
|
||||
ERROR(buf, "ActionWSError");
|
||||
}
|
||||
|
||||
buffer[received] = '\0';
|
||||
@@ -283,21 +235,11 @@ GroundValue ws_receive(GroundScope* scope, List args) {
|
||||
|
||||
// WebSocket Receive with timeout (non-blocking version)
|
||||
GroundValue ws_receive_timeout(GroundScope* scope, List args) {
|
||||
if (args.size < 2) {
|
||||
printf("Requires connection ID and timeout in milliseconds\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
if (args.values[0].type != INT || args.values[1].type != INT) {
|
||||
printf("Both arguments need to be INT (connection ID, timeout_ms)\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
|
||||
int conn_id = (int)args.values[0].data.intVal;
|
||||
long timeout_ms = (long)args.values[1].data.intVal;
|
||||
|
||||
if (conn_id < 0 || conn_id >= MAX_WS_CONNECTIONS || !ws_connections[conn_id].connected) {
|
||||
printf("Invalid or closed WebSocket connection\n");
|
||||
return groundCreateValue(NONE);
|
||||
ERROR("Invalid WebSocket connection ID", "GenericWSError");
|
||||
}
|
||||
|
||||
CURL* handle = ws_connections[conn_id].handle;
|
||||
@@ -316,8 +258,9 @@ GroundValue ws_receive_timeout(GroundScope* scope, List args) {
|
||||
}
|
||||
|
||||
if (result != CURLE_OK) {
|
||||
printf("WebSocket receive failed: %s\n", curl_easy_strerror(result));
|
||||
return groundCreateValue(NONE);
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf) - 1, "WebSocket receive failed: %s\n", curl_easy_strerror(result));
|
||||
ERROR(buf, "ActionWSError");
|
||||
}
|
||||
|
||||
buffer[received] = '\0';
|
||||
@@ -341,19 +284,9 @@ GroundValue ws_receive_timeout(GroundScope* scope, List args) {
|
||||
|
||||
// WebSocket Close - properly close a connection, returns BOOL success
|
||||
GroundValue ws_close(GroundScope* scope, List args) {
|
||||
if (args.size < 1) {
|
||||
printf("Requires connection ID\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
if (args.values[0].type != INT) {
|
||||
printf("Arg 1 needs to be an INT (connection ID)\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
|
||||
int conn_id = (int)args.values[0].data.intVal;
|
||||
if (conn_id < 0 || conn_id >= MAX_WS_CONNECTIONS || !ws_connections[conn_id].connected) {
|
||||
printf("Invalid or already closed WebSocket connection\n");
|
||||
return groundCreateValue(BOOL, false);
|
||||
ERROR("Invalid or already connected websocket", "GenericWSError");
|
||||
}
|
||||
|
||||
CURL* handle = ws_connections[conn_id].handle;
|
||||
@@ -374,19 +307,9 @@ GroundValue ws_close(GroundScope* scope, List args) {
|
||||
|
||||
// WebSocket Send Binary data, returns bytes sent as INT
|
||||
GroundValue ws_send_binary(GroundScope* scope, List args) {
|
||||
if (args.size < 2) {
|
||||
printf("Requires connection ID and binary data\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
if (args.values[0].type != INT || args.values[1].type != STRING) {
|
||||
printf("Args need to be (INT connection ID, STRING binary data)\n");
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
|
||||
int conn_id = (int)args.values[0].data.intVal;
|
||||
if (conn_id < 0 || conn_id >= MAX_WS_CONNECTIONS || !ws_connections[conn_id].connected) {
|
||||
printf("Invalid or closed WebSocket connection\n");
|
||||
return groundCreateValue(NONE);
|
||||
ERROR("Invalid WebSocket connection ID", "GenericWSError");
|
||||
}
|
||||
|
||||
CURL* handle = ws_connections[conn_id].handle;
|
||||
@@ -396,8 +319,9 @@ GroundValue ws_send_binary(GroundScope* scope, List args) {
|
||||
CURLcode result = curl_ws_send(handle, data, strlen(data), &sent, 0, CURLWS_BINARY);
|
||||
|
||||
if (result != CURLE_OK) {
|
||||
printf("WebSocket binary send failed: %s\n", curl_easy_strerror(result));
|
||||
return groundCreateValue(NONE);
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf) - 1, "WebSocket binary send failed: %s\n", curl_easy_strerror(result));
|
||||
ERROR(buf, "ActionWSError");
|
||||
}
|
||||
|
||||
return groundCreateValue(INT, (int64_t)sent);
|
||||
|
||||
Reference in New Issue
Block a user