feat: improved errors

This commit is contained in:
Jozef Steinhübl 2024-04-04 16:12:55 +02:00
parent c62cfa2438
commit b18eff081c
3 changed files with 53 additions and 7 deletions

View file

@ -0,0 +1,18 @@
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
type RecursiveError = Record<string, any>;
export function parseErrors(
obj: RecursiveError,
): { code: string; message: string }[] {
const errors = [];
for (const key in obj) {
if (key === "_errors") {
errors.push(obj[key]);
} else if (typeof obj[key] === "object") {
errors.push(...parseErrors(obj[key]));
}
}
return errors.flat();
}

View file

@ -34,6 +34,7 @@ export default async function (data: BasicData, ctx: Context, rawRole: string) {
new TextInputBuilder()
.setLabel("Label")
.setCustomId("label")
.setMaxLength(data.selecting === "buttons" ? 80 : 100)
.setPlaceholder("Ping")
.setStyle(TextInputStyle.Short)
.setRequired(true),
@ -75,6 +76,7 @@ export default async function (data: BasicData, ctx: Context, rawRole: string) {
new TextInputBuilder()
.setLabel("Description")
.setCustomId("description")
.setMaxLength(100)
.setPlaceholder("pingping pong pong")
.setStyle(TextInputStyle.Short)
.setRequired(false),

View file

@ -21,6 +21,7 @@ import splitArray from "./splitArray";
import { resolvePartialEmoji } from "./resolveEmoji";
import { BasicData } from "../types";
import resolveButtonStyle from "./resolveButtonStyle";
import { parseErrors } from "./errors";
type DataBot = BasicData & {
sendAs: "bot";
@ -66,7 +67,9 @@ export default async function (ctx: Context, data: Data) {
const components: APIActionRowComponent<APIMessageActionRowComponent>[] = [];
const array = splitArray(data.roleIds, data.selecting === "buttons" ? 5 : 24);
for (const items of array) {
const actionRow = new ActionRowBuilder();
const actionRow = new ActionRowBuilder<
ButtonBuilder | StringSelectMenuBuilder
>();
const selectMenuNaiveOption = new StringSelectMenuOptionBuilder()
.setLabel("Make a choice")
@ -113,8 +116,7 @@ export default async function (ctx: Context, data: Data) {
if (data.selecting === "dropdowns") actionRow.addComponents(selectMenu);
// @ts-expect-error i know i know
components.push(actionRow);
components.push(actionRow.toJSON());
}
payload.components = components;
@ -152,11 +154,23 @@ export default async function (ctx: Context, data: Data) {
}
if (!res.ok) {
const json: { message: string; code: string } = await res.json();
const json: {
message: string;
code: string;
errors: Record<string, unknown>;
} = await res.json();
const errors = parseErrors(json?.errors ?? {});
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: `Error: ${json.message} (${json.code})`,
content: `Error: ${json.message} (${json.code})${
errors.length > 0
? `\n${errors
.map((e) => ` - ${e.message} (${e.code})`)
.join("\n")}`
: ""
}`,
flags: MessageFlags.Ephemeral,
},
});
@ -200,11 +214,23 @@ export default async function (ctx: Context, data: Data) {
);
if (!res.ok) {
const json: { message: string; code: string } = await res.json();
const json: {
message: string;
code: string;
errors: Record<string, unknown>;
} = await res.json();
const errors = parseErrors(json?.errors ?? {});
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: `Error: ${json.message} (${json.code})`,
content: `Error: ${json.message} (${json.code})${
errors.length > 0
? `\n${errors
.map((e) => ` - ${e.message} (${e.code})`)
.join("\n")}`
: ""
}`,
flags: MessageFlags.Ephemeral,
},
});