error handling

This commit is contained in:
LenAnderson 2023-12-22 12:55:25 +00:00
parent e3c2d6771c
commit a088fb1746
2 changed files with 78 additions and 31 deletions

View File

@ -72,7 +72,9 @@ export class QuickReplyApi {
*/ */
toggleGlobalSet(name, isVisible = true) { toggleGlobalSet(name, isVisible = true) {
const set = this.getSetByName(name); const set = this.getSetByName(name);
if (!set) return; if (!set) {
throw new Error(`No quick reply set with name "${name}" found.`);
}
if (this.settings.config.hasSet(set)) { if (this.settings.config.hasSet(set)) {
this.settings.config.removeSet(set); this.settings.config.removeSet(set);
} else { } else {
@ -88,7 +90,9 @@ export class QuickReplyApi {
*/ */
addGlobalSet(name, isVisible = true) { addGlobalSet(name, isVisible = true) {
const set = this.getSetByName(name); const set = this.getSetByName(name);
if (!set) return; if (!set) {
throw new Error(`No quick reply set with name "${name}" found.`);
}
this.settings.config.addSet(set, isVisible); this.settings.config.addSet(set, isVisible);
} }
@ -99,7 +103,9 @@ export class QuickReplyApi {
*/ */
removeGlobalSet(name) { removeGlobalSet(name) {
const set = this.getSetByName(name); const set = this.getSetByName(name);
if (!set) return; if (!set) {
throw new Error(`No quick reply set with name "${name}" found.`);
}
this.settings.config.removeSet(set); this.settings.config.removeSet(set);
} }
@ -113,7 +119,9 @@ export class QuickReplyApi {
toggleChatSet(name, isVisible = true) { toggleChatSet(name, isVisible = true) {
if (!this.settings.chatConfig) return; if (!this.settings.chatConfig) return;
const set = this.getSetByName(name); const set = this.getSetByName(name);
if (!set) return; if (!set) {
throw new Error(`No quick reply set with name "${name}" found.`);
}
if (this.settings.chatConfig.hasSet(set)) { if (this.settings.chatConfig.hasSet(set)) {
this.settings.chatConfig.removeSet(set); this.settings.chatConfig.removeSet(set);
} else { } else {
@ -130,7 +138,9 @@ export class QuickReplyApi {
addChatSet(name, isVisible = true) { addChatSet(name, isVisible = true) {
if (!this.settings.chatConfig) return; if (!this.settings.chatConfig) return;
const set = this.getSetByName(name); const set = this.getSetByName(name);
if (!set) return; if (!set) {
throw new Error(`No quick reply set with name "${name}" found.`);
}
this.settings.chatConfig.addSet(set, isVisible); this.settings.chatConfig.addSet(set, isVisible);
} }
@ -142,7 +152,9 @@ export class QuickReplyApi {
removeChatSet(name) { removeChatSet(name) {
if (!this.settings.chatConfig) return; if (!this.settings.chatConfig) return;
const set = this.getSetByName(name); const set = this.getSetByName(name);
if (!set) return; if (!set) {
throw new Error(`No quick reply set with name "${name}" found.`);
}
this.settings.chatConfig.removeSet(set); this.settings.chatConfig.removeSet(set);
} }

View File

@ -85,24 +85,48 @@ export class SlashCommandHandler {
toggleGlobalSet(name, args = {}) { toggleGlobalSet(name, args = {}) {
this.api.toggleGlobalSet(name, JSON.parse(args.visible ?? 'true') === true); try {
this.api.toggleGlobalSet(name, JSON.parse(args.visible ?? 'true') === true);
} catch (ex) {
toastr.error(ex.message);
}
} }
addGlobalSet(name, args = {}) { addGlobalSet(name, args = {}) {
this.api.addGlobalSet(name, JSON.parse(args.visible ?? 'true') === true); try {
this.api.addGlobalSet(name, JSON.parse(args.visible ?? 'true') === true);
} catch (ex) {
toastr.error(ex.message);
}
} }
removeGlobalSet(name) { removeGlobalSet(name) {
this.api.removeGlobalSet(name); try {
this.api.removeGlobalSet(name);
} catch (ex) {
toastr.error(ex.message);
}
} }
toggleChatSet(name, args = {}) { toggleChatSet(name, args = {}) {
this.api.toggleChatSet(name, JSON.parse(args.visible ?? 'true') === true); try {
this.api.toggleChatSet(name, JSON.parse(args.visible ?? 'true') === true);
} catch (ex) {
toastr.error(ex.message);
}
} }
addChatSet(name, args = {}) { addChatSet(name, args = {}) {
this.api.addChatSet(name, JSON.parse(args.visible ?? 'true') === true); try {
this.api.addChatSet(name, JSON.parse(args.visible ?? 'true') === true);
} catch (ex) {
toastr.error(ex.message);
}
} }
removeChatSet(name) { removeChatSet(name) {
this.api.removeChatSet(name); try {
this.api.removeChatSet(name);
} catch (ex) {
toastr.error(ex.message);
}
} }
@ -174,30 +198,41 @@ export class SlashCommandHandler {
} }
} }
clearContextMenu(args, label) { clearContextMenu(args, label) {
const qr = this.getQrByLabel(args.set, args.label ?? label); try {
if (!qr) return; this.api.clearContextMenu(args.set, args.label ?? label);
qr.clearContextLinks(); } catch (ex) {
toastr.error(ex.message);
}
} }
createSet(name, args) { createSet(name, args) {
this.api.createSet( try {
args.name ?? name ?? '', this.api.createSet(
{ args.name ?? name ?? '',
disableSend: JSON.parse(args.nosend ?? 'false') === true, {
placeBeforeInput: JSON.parse(args.before ?? 'false') === true, disableSend: JSON.parse(args.nosend ?? 'false') === true,
injectInput: JSON.parse(args.inject ?? 'false') === true, placeBeforeInput: JSON.parse(args.before ?? 'false') === true,
}, injectInput: JSON.parse(args.inject ?? 'false') === true,
); },
);
} catch (ex) {
toastr.error(ex.message);
}
} }
updateSet(name, args) { updateSet(name, args) {
this.api.updateSet( try {
args.name ?? name ?? '', this.api.updateSet(
{ args.name ?? name ?? '',
disableSend: args.nosend !== undefined ? JSON.parse(args.nosend ?? 'false') === true : undefined, {
placeBeforeInput: args.before !== undefined ? JSON.parse(args.before ?? 'false') === true : undefined, disableSend: args.nosend !== undefined ? JSON.parse(args.nosend ?? 'false') === true : undefined,
injectInput: args.inject !== undefined ? JSON.parse(args.inject ?? 'false') === true : undefined, placeBeforeInput: args.before !== undefined ? JSON.parse(args.before ?? 'false') === true : undefined,
}, injectInput: args.inject !== undefined ? JSON.parse(args.inject ?? 'false') === true : undefined,
); },
);
} catch (ex) {
toastr.error(ex.message);
}
}
} }
} }