Skip to content

Commit

Permalink
fix: add handling for errors in fetchUser (#25)
Browse files Browse the repository at this point in the history
* fix: add handling for errors in fetchUser

* Update src/utils/fetchUser.ts

Co-authored-by: pat <73502164+nyapat@users.noreply.github.com>

Co-authored-by: tOwOgarashi <togarashi@togarashi.owoowowowowowoowowowowo>
Co-authored-by: pat <73502164+nyapat@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 10, 2021
1 parent 4524fea commit ddb2bbb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/commands/general/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const command: Command = {
],
permissions: [],
async execute(interaction) {
const value = interaction.options.get("user")?.value;
const data = await fetchUser(value as string);
const value = interaction.options.getString("user", true);
const data = await fetchUser(value);
if(!data) {
interaction.reply({ content: PROFILE_NOT_FOUND, ephemeral: true });
}
Expand Down
7 changes: 3 additions & 4 deletions src/utils/fetchUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { BINARYSEARCH_API } from "#constants/urls";
import type { BSUser } from "#types/BSUser";

export async function fetchUser(username: string): Promise<null | BSUser> {
const url = BINARYSEARCH_API.replace("USERNAME", username);
const data = await fetch(url).then(res => res.json());
const url = BINARYSEARCH_API.replace("USERNAME", encodeURIComponent(username));
const data = await fetch(url).then(res => res.json()).catch(console.error);

if ("user" in data) return data.user;
return null;
return data?.user ?? null;
}

0 comments on commit ddb2bbb

Please sign in to comment.