From e211e8bf8c48c099ad7750df3c0f75d48e73aee2 Mon Sep 17 00:00:00 2001 From: ading2210 Date: Thu, 7 Mar 2024 17:18:27 -0500 Subject: [PATCH] expose function to get error strings --- README.md | 28 ++++++++++++++++++++++++++++ client/javascript/main.js | 1 + client/javascript/websocket.js | 1 + client/javascript/ws_polyfill.js | 1 - client/package.json | 2 +- 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 53c0e90..de0f0bc 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,26 @@ This is an experimental port of [libcurl](https://curl.se/libcurl/) to WebAssembly for use in the browser. It provides an interface compatible with the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), allowing you to proxy HTTPS requests from the browser with full TLS encryption. Unlike previous implementations, the proxy server cannot read the contents of your requests. +## Table of Contents: +- [Features](#features) +- [Building](#building) +- [Javascript API](#javascript-api) + * [Importing the Library](#importing-the-library) + * [Making HTTP Requests](#making-http-requests) + * [Creating WebSocket Connections](#creating-websocket-connections) + * [Using TLS Sockets](#using-tls-sockets) + * [Changing the Network Transport](#changing-the-network-transport) + * [Changing the Websocket Proxy URL](#changing-the-websocket-proxy-url) + * [Getting Libcurl's Output](#getting-libcurl-s-output) + * [Getting Error Strings](#getting-error-strings) + * [Getting Version Info](#getting-version-info) + * [Getting the CA Certificates Bundle](#getting-the-ca-certificates-bundle) +- [Proxy Server](#proxy-server) +- [Copyright](#copyright) + * [Copyright Notice](#copyright-notice) + +Table of contents generated with markdown-toc + ## Features: - Fetch compatible API - End to end encryption between the browser and the destination server @@ -176,6 +196,14 @@ Libcurl.js will also output some error messages to the browser console. You can This may be useful if you are running libcurl.js inside a web worker and do not have access to the regular console API. +### Getting Error Strings: +Libcurl.js reports errors based on the [error codes](https://curl.se/libcurl/c/libcurl-errors.html) defined by the libcurl C library. The `libcurl.get_error_string` function can be used to get an error string from an error code. + +```js +console.log(libcurl.get_error_string(56)); +//"Failure when receiving data from the peer" +``` + ### Getting Version Info: You can get version information from the `libcurl.version` object. This object will also contain the versions of all the C libraries that libcurl.js uses. `libcurl.version.lib` returns the version of libcurl.js itself. diff --git a/client/javascript/main.js b/client/javascript/main.js index 449df13..046fab6 100644 --- a/client/javascript/main.js +++ b/client/javascript/main.js @@ -244,6 +244,7 @@ api = { CurlWebSocket: CurlWebSocket, TLSSocket: TLSSocket, get_cacert: get_cacert, + get_error_string: get_error_str, wisp_connections: _wisp_connections, WispConnection: WispConnection, diff --git a/client/javascript/websocket.js b/client/javascript/websocket.js index 1117cfc..0e2da59 100644 --- a/client/javascript/websocket.js +++ b/client/javascript/websocket.js @@ -106,6 +106,7 @@ class CurlWebSocket { this.connected = false; if (error) { + error_msg(`Websocket "${this.url}" encountered error code ${error}: ${get_error_str(error)}`); this.onerror(error); } else { diff --git a/client/javascript/ws_polyfill.js b/client/javascript/ws_polyfill.js index 54074bb..1adc046 100644 --- a/client/javascript/ws_polyfill.js +++ b/client/javascript/ws_polyfill.js @@ -44,7 +44,6 @@ class FakeWebSocket extends EventTarget { this.socket.onerror = (error) => { this.status = this.CLOSED; - error_msg(`websocket ${this.url} encountered an error (${error})`); let error_event = new Event("error"); this.dispatchEvent(error_event); this.onerror(error_event); diff --git a/client/package.json b/client/package.json index 0d26f2f..eb49ee1 100644 --- a/client/package.json +++ b/client/package.json @@ -1,6 +1,6 @@ { "name": "libcurl.js", - "version": "0.4.1", + "version": "0.4.2", "description": "An experimental port of libcurl to WebAssembly for use in the browser.", "main": "libcurl.mjs", "scripts": {