~emersion/public-inbox

gamja: Add error reporting on connect and main page v1 SUPERSEDED

bbworld1: 1
 Add error reporting on connect and main page

 4 files changed, 31 insertions(+), 21 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/public-inbox/patches/11692/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH gamja] Add error reporting on connect and main page Export this patch

---
 components/app.js     | 17 +++++++++++------
 components/connect.js |  1 +
 lib/client.js         | 24 +++++++++---------------
 style.css             | 10 ++++++++++
 4 files changed, 31 insertions(+), 21 deletions(-)

diff --git a/components/app.js b/components/app.js
index e255b70..596c893 100644
--- a/components/app.js
+++ b/components/app.js
@@ -84,6 +84,7 @@ export default class App extends Component {
		status: Status.DISCONNECTED,
		buffers: new Map(),
		activeBuffer: null,
    error: null
	};
	pendingHistory = Promise.resolve(null);
	endOfHistory = new Map();
@@ -323,6 +324,9 @@ export default class App extends Component {
			username: params.username,
			realname: params.realname,
			saslPlain: params.saslPlain,
      onError: (error) => {
        this.setState({ error });
      }
		});

		this.client.addEventListener("close", () => {
@@ -440,7 +444,7 @@ export default class App extends Component {
				var after = receipt;
				var before = { time: msg.tags.time || irc.formatDate(new Date()) };
				this.fetchHistoryBetween(channel, after, before, CHATHISTORY_MAX_SIZE).catch((err) => {
					console.error("Failed to fetch history:", err);
          this.setState({ error: "Failed to fetch history:" + err });
					this.receipts.delete(channel);
					this.saveReceipts();
				});
@@ -584,20 +588,20 @@ export default class App extends Component {

		var cmd = commands[name];
		if (!cmd) {
			console.error("Unknwon command '" + name + "'");
      this.setState({ error: "Unknown command '" + name + "'" });
			return;
		}

		try {
			cmd(this, args);
		} catch (err) {
			console.error(err);
		} catch (error) {
      this.setState({ error });
		}
	}

	privmsg(target, text) {
		if (target == SERVER_BUFFER) {
			console.error("Cannot send message in server buffer");
      this.setState({ error: "Cannot send message in server buffer" });
			return;
		}

@@ -764,7 +768,7 @@ export default class App extends Component {
		if (this.state.status != Status.REGISTERED) {
			return html`
				<section id="connect">
					<${Connect} params=${this.state.connectParams} disabled=${this.state.status != Status.DISCONNECTED} onSubmit=${this.handleConnectSubmit}/>
					<${Connect} error=${this.state.error} params=${this.state.connectParams} disabled=${this.state.status != Status.DISCONNECTED} onSubmit=${this.handleConnectSubmit}/>
				</section>
			`;
		}
@@ -810,6 +814,7 @@ export default class App extends Component {
			</>
			${memberList}
			<${Composer} ref=${this.composer} readOnly=${this.state.activeBuffer == SERVER_BUFFER} onSubmit=${this.handleComposerSubmit} autocomplete=${this.autocomplete}/>
      <p id="error-msg">Error: unknown error</p>
		`;
	}
}
diff --git a/components/connect.js b/components/connect.js
index 97589e0..f124f7c 100644
--- a/components/connect.js
+++ b/components/connect.js
@@ -140,6 +140,7 @@ export default class Connect extends Component {
				</details>

				<br/>
        <p style=${{color: "red"}}>${this.props.error || ""}</p>

				<button disabled=${this.props.disabled}>Connect</button>
			</form>
diff --git a/lib/client.js b/lib/client.js
index 01fd67b..84b9c95 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -22,6 +22,7 @@ export default class Client extends EventTarget {
		nick: null,
		pass: null,
		saslPlain: null,
    onError: null
	};
	registered = false;
	availableCaps = {};
@@ -33,24 +34,17 @@ export default class Client extends EventTarget {

		this.params = Object.assign(this.params, params);

		try {
			this.ws = new WebSocket(params.url);
		} catch (err) {
			console.error("Failed to create connection:", err);
			setTimeout(() => this.dispatchEvent(new CustomEvent("close")), 0);
			return;
		}

		this.ws = new WebSocket(params.url);
		this.ws.addEventListener("open", this.handleOpen.bind(this));
		this.ws.addEventListener("message", this.handleMessage.bind(this));

		this.ws.addEventListener("close", () => {
		this.ws.addEventListener("close", (e) => {
			console.log("Connection closed");
			this.dispatchEvent(new CustomEvent("close"));
		});

		this.ws.addEventListener("error", () => {
			console.error("Connection error");
		this.ws.addEventListener("error", (err) => {
      this.params.onError("Connection error: could not open WebSocket connection");
		});
	}

@@ -86,7 +80,7 @@ export default class Client extends EventTarget {
		switch (msg.command) {
		case irc.RPL_WELCOME:
			if (this.params.saslPlain && this.availableCaps["sasl"] === undefined) {
				console.error("Server doesn't support SASL PLAIN");
				this.params.onError("Server doesn't support SASL PLAIN");
				this.close();
				return;
			}
@@ -95,7 +89,7 @@ export default class Client extends EventTarget {
			this.registered = true;
			break;
		case irc.ERR_PASSWDMISMATCH:
			console.error("Password mismatch");
			this.params.onError("Password mismatch");
			this.close();
			break;
		case "CAP":
@@ -121,7 +115,7 @@ export default class Client extends EventTarget {
		case irc.ERR_SASLTOOLONG:
		case irc.ERR_SASLABORTED:
		case irc.ERR_SASLALREADY:
			console.error("SASL error:", msg);
			this.params.onError("SASL error:", msg);
			this.close();
			break;
		case "PING":
@@ -258,7 +252,7 @@ export default class Client extends EventTarget {

		// For now only PLAIN is supported
		if (challengeStr != "+") {
			console.error("Expected an empty challenge, got:", challengeStr);
			this.params.onError("Expected an empty challenge, got:", challengeStr);
			this.send({ command: "AUTHENTICATE", params: ["*"] });
			return;
		}
diff --git a/style.css b/style.css
index 3d1166f..b9276d2 100644
--- a/style.css
+++ b/style.css
@@ -234,3 +234,13 @@ details summary {
#buffer .nick-16 {
	color: #ec273e;
}

#error-msg {
  color: white;
  background-color: red;
  position: fixed;
  bottom: 2rem;
  right: 0;
  padding: 0.5rem;
  margin: 0.5rem;
}
-- 
2.27.0
Thanks for sending a new version, but it seems like this is the exact
same patch as the first one?

(Tip: you can give the -v2 flag to git-send-email to label a patch as
version 2.)