add basic error catching to axios calls

This commit is contained in:
emerson 2022-01-24 10:53:42 -05:00
parent 53dda5d518
commit ddbdcc9981
3 changed files with 81 additions and 15 deletions

View file

@ -1,10 +1,8 @@
import { Server } from "./Server.js"; import { Server } from "./Server.js";
import { MatrixUser } from "./MatrixUser.js"; import { MatrixUser } from "./MatrixUser.js";
import { IRCUser } from "./IRCUser.js"; import { IRCUser } from "./IRCUser.js";
import { IRCMessage } from "./Message.js";
import { Client } from "./Client.js"; import { Client } from "./Client.js";
import numerics from "./numerics.js"; import numerics from "./numerics.js";
import axios from "axios";
export class Channel { export class Channel {
public name: string public name: string

View file

@ -248,6 +248,15 @@ export class Client {
this.user.nextBatch = data.next_batch; 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.sendMessage(this.server.name, 'NOTICE', [this.user.nick, 'You are now synced to the network!'], message.tags);
this.user.addClient(this, 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);
}
}) })
} }
} }

View file

@ -38,7 +38,11 @@ export class IRCUser {
} }
getVerification() { 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 { 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<string, string> = new Map()) { inviteMatrixUser(client: Client, channel: Channel, target: MatrixUser, reason: string, passedTags: Map<string, string> = new Map()) {
@ -84,9 +98,15 @@ export class IRCUser {
"reason": reason, "reason": reason,
"user_id": target.mxid "user_id": target.mxid
} }
axios.post(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/invite?access_token=${this.accessToken}`, data).then(response => { axios.post(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/invite?access_token=${this.accessToken}`, data).catch(function (error) {
if (response.status !== 200) if (error.response) {
client.sendMessage(this.server.name, "NOTICE", [this.nick, JSON.stringify(response.data)], passedTags); 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, "reason": reason,
"user_id": target "user_id": target
} }
axios.post(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/kick?access_token=${this.accessToken}`, data).then(response => { axios.post(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/kick?access_token=${this.accessToken}`, data).catch(function (error) {
if (response.status !== 200) if (error.response) {
client.sendMessage(this.server.name, "NOTICE", [this.nick, JSON.stringify(response.data)], passedTags); 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 { else {
client.sendMessage(this.server.name, "NOTICE", [this.nick, JSON.stringify(response.data)], passedTags); 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<string, string> = new Map()) { changeRoomTopic(client: Client, channel: Channel, topic: string, passedTags: Map<string, string> = 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 => { 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 (response.status !== 200) if (error.response) {
client.sendMessage(this.server.name, "NOTICE", [this.nick, JSON.stringify(response.data)], passedTags); 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(); const newTxnid = randomUUID();
this.txnIdStore.set(newTxnid, client); 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) { sendTagToMatrix(message: IRCMessage, client: Client) {
@ -169,7 +219,16 @@ export class IRCUser {
} }
const newTxnid = randomUUID(); const newTxnid = randomUUID();
this.txnIdStore.set(newTxnid, client); 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);
}
});
} }
} }