MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

improve error handling of fetch

+36 -6
+8
include/modules/http.h
··· 13 13 struct ant_http_header_s *next; 14 14 } ant_http_header_t; 15 15 16 + typedef enum { 17 + ANT_HTTP_RESULT_OK = 0, 18 + ANT_HTTP_RESULT_NETWORK_ERROR, 19 + ANT_HTTP_RESULT_ABORTED, 20 + ANT_HTTP_RESULT_PROTOCOL_ERROR, 21 + } ant_http_result_t; 22 + 16 23 typedef struct { 17 24 const char *method; 18 25 const char *url; ··· 43 50 44 51 typedef void (*ant_http_complete_cb)( 45 52 ant_http_request_t *req, 53 + ant_http_result_t result, 46 54 int error_code, 47 55 const char *error_message, 48 56 void *user_data
+19 -5
src/modules/fetch.c
··· 40 40 int refs; 41 41 bool settled; 42 42 bool aborted; 43 + bool response_started; 43 44 } fetch_request_t; 44 45 45 46 static UT_array *pending_requests = NULL; ··· 118 119 static void fetch_resolve(fetch_request_t *req, ant_value_t response_obj) { 119 120 if (!req || req->settled) return; 120 121 req->settled = true; 122 + req->response_started = true; 121 123 req->response_obj = response_obj; 122 124 js_resolve_promise(req->js, req->promise, response_obj); 123 125 } ··· 344 346 } 345 347 } 346 348 347 - static void fetch_http_on_complete(ant_http_request_t *http_req, int error_code, const char *error_message, void *user_data) { 349 + static ant_value_t fetch_transport_reason(fetch_request_t *req, ant_http_result_t result, const char *error_message) { 350 + if (result == ANT_HTTP_RESULT_ABORTED && req->aborted) { 351 + ant_value_t signal = request_get_signal(req->request_obj); 352 + return abort_signal_get_reason(signal); 353 + } 354 + 355 + return fetch_type_error(req->js, error_message ? error_message : "fetch failed"); 356 + } 357 + 358 + static void fetch_http_on_complete( 359 + ant_http_request_t *http_req, 360 + ant_http_result_t result, 361 + int error_code, const char *error_message, void *user_data 362 + ) { 348 363 fetch_request_t *req = (fetch_request_t *)user_data; 364 + 349 365 ant_t *js = req->js; 350 366 ant_value_t stream = 0; 351 367 ant_value_t controller = 0; 352 368 ant_value_t reason = 0; 353 - 354 - (void)http_req; 355 369 req->http_req = NULL; 356 370 357 - if (error_code != 0) { 358 - reason = fetch_type_error(js, error_message ? error_message : "fetch failed"); 371 + if (result != ANT_HTTP_RESULT_OK || error_code != 0) { 372 + reason = fetch_transport_reason(req, result, error_message); 359 373 if (is_object_type(req->response_obj)) fetch_error_response_body(req, reason); 360 374 else fetch_reject(req, reason); 361 375 fetch_request_release(req);
+9 -1
src/modules/http.c
··· 20 20 char *error_message; 21 21 brotli_stream_state_t *brotli_decoder; 22 22 23 + ant_http_result_t result; 23 24 int error_code; 25 + 24 26 bool completed; 27 + bool canceled; 25 28 bool decode_brotli; 26 29 }; 27 30 ··· 141 144 if (!req) return; 142 145 143 146 if (req->on_complete) req->on_complete( 144 - req, req->error_code, 147 + req, req->result, req->error_code, 145 148 req->error_message, 146 149 req->user_data 147 150 ); ··· 153 156 if (!req || req->completed) return; 154 157 req->completed = 1; 155 158 req->error_code = error_code; 159 + 160 + if (error_code == 0) req->result = ANT_HTTP_RESULT_OK; 161 + else if (req->canceled) req->result = ANT_HTTP_RESULT_ABORTED; 162 + else req->result = ANT_HTTP_RESULT_NETWORK_ERROR; 156 163 157 164 free(req->error_message); 158 165 req->error_message = error_message ? strdup(error_message) : NULL; ··· 237 244 238 245 int ant_http_request_cancel(ant_http_request_t *req) { 239 246 if (!req || !req->req || req->completed) return 0; 247 + req->canceled = true; 240 248 return tlsuv_http_req_cancel(&req->client, req->req); 241 249 } 242 250