From ddbdcc9981164dd6325274735d9db18f922aabe2 Mon Sep 17 00:00:00 2001 From: emerson Date: Mon, 24 Jan 2022 10:53:42 -0500 Subject: [PATCH] add basic error catching to axios calls --- src/Channel.ts | 2 -- src/Client.ts | 9 ++++++ src/IRCUser.ts | 85 ++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 81 insertions(+), 15 deletions(-) diff --git a/src/Channel.ts b/src/Channel.ts index dec325b..b6febb9 100644 --- a/src/Channel.ts +++ b/src/Channel.ts @@ -1,10 +1,8 @@ import { Server } from "./Server.js"; import { MatrixUser } from "./MatrixUser.js"; import { IRCUser } from "./IRCUser.js"; -import { IRCMessage } from "./Message.js"; import { Client } from "./Client.js"; import numerics from "./numerics.js"; -import axios from "axios"; export class Channel { public name: string diff --git a/src/Client.ts b/src/Client.ts index 1dfc479..d6da89f 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -248,6 +248,15 @@ export class Client { this.user.nextBatch = data.next_batch; this.sendMessage(this.server.name, 'NOTICE', [this.user.nick, 'You are now synced to the network!'], message.tags); this.user.addClient(this, message.tags); + }).catch(function (error) { + if (error.response) { + console.log(error.response.data); + } else if (error.request) { + console.log(error.request); + } else { + console.log('Error', error.message); + console.log(error.config); + } }) } } diff --git a/src/IRCUser.ts b/src/IRCUser.ts index 2fd075e..09f590c 100644 --- a/src/IRCUser.ts +++ b/src/IRCUser.ts @@ -38,7 +38,11 @@ export class IRCUser { } getVerification() { - return axios.get(`https://${this.homeserver}/_matrix/client/v3/account/whoami?access_token=${this.accessToken}`); + return axios.get(`https://${this.homeserver}/_matrix/client/v3/account/whoami?access_token=${this.accessToken}`, { + validateStatus: function (status) { + return status < 500; + } + }); } getMask(): string { @@ -76,7 +80,17 @@ export class IRCUser { }); } } - }) + }).catch(function (error) { + if (error.response) { + console.log(error.response.data); + console.log(error.response.status); + } else if (error.request) { + console.log(error.request); + } else { + console.log('Error', error.message); + console.log(error.config); + } + }); } inviteMatrixUser(client: Client, channel: Channel, target: MatrixUser, reason: string, passedTags: Map = new Map()) { @@ -84,9 +98,15 @@ export class IRCUser { "reason": reason, "user_id": target.mxid } - axios.post(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/invite?access_token=${this.accessToken}`, data).then(response => { - if (response.status !== 200) - client.sendMessage(this.server.name, "NOTICE", [this.nick, JSON.stringify(response.data)], passedTags); + axios.post(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/invite?access_token=${this.accessToken}`, data).catch(function (error) { + if (error.response) { + console.log(error.response.data); + } else if (error.request) { + console.log(error.request); + } else { + console.log('Error', error.message); + console.log(error.config); + } }) } @@ -95,9 +115,15 @@ export class IRCUser { "reason": reason, "user_id": target } - axios.post(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/kick?access_token=${this.accessToken}`, data).then(response => { - if (response.status !== 200) - client.sendMessage(this.server.name, "NOTICE", [this.nick, JSON.stringify(response.data)], passedTags); + axios.post(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/kick?access_token=${this.accessToken}`, data).catch(function (error) { + if (error.response) { + console.log(error.response.data); + } else if (error.request) { + console.log(error.request); + } else { + console.log('Error', error.message); + console.log(error.config); + } }) } @@ -112,13 +138,28 @@ export class IRCUser { else { client.sendMessage(this.server.name, "NOTICE", [this.nick, JSON.stringify(response.data)], passedTags); } + }).catch(function (error) { + if (error.response) { + console.log(error.response.data); + } else if (error.request) { + console.log(error.request); + } else { + console.log('Error', error.message); + console.log(error.config); + } }) } changeRoomTopic(client: Client, channel: Channel, topic: string, passedTags: Map = new Map()) { - axios.put(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/state/m.room.topic?access_token=${this.accessToken}`, {"topic": topic}).then(response => { - if (response.status !== 200) - client.sendMessage(this.server.name, "NOTICE", [this.nick, JSON.stringify(response.data)], passedTags); + axios.put(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/state/m.room.topic?access_token=${this.accessToken}`, {"topic": topic}).catch(function (error) { + if (error.response) { + console.log(error.response.data); + } else if (error.request) { + console.log(error.request); + } else { + console.log('Error', error.message); + console.log(error.config); + } }) } @@ -150,7 +191,16 @@ export class IRCUser { } const newTxnid = randomUUID(); this.txnIdStore.set(newTxnid, client); - axios.put(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/send/m.room.message/${newTxnid}?access_token=${this.accessToken}`, content); + axios.put(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/send/m.room.message/${newTxnid}?access_token=${this.accessToken}`, content).catch(function (error) { + if (error.response) { + console.log(error.response.data); + } else if (error.request) { + console.log(error.request); + } else { + console.log('Error', error.message); + console.log(error.config); + } + }); } sendTagToMatrix(message: IRCMessage, client: Client) { @@ -169,7 +219,16 @@ export class IRCUser { } const newTxnid = randomUUID(); this.txnIdStore.set(newTxnid, client); - axios.put(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/send/m.reaction/${newTxnid}?access_token=${this.accessToken}`, content); + axios.put(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/send/m.reaction/${newTxnid}?access_token=${this.accessToken}`, content).catch(function (error) { + if (error.response) { + console.log(error.response.data); + } else if (error.request) { + console.log(error.request); + } else { + console.log('Error', error.message); + console.log(error.config); + } + }); } }