Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Better Errors #58

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/API/getGuild.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import Guild from '../structures/Guild/Guild.js';
import isGuildID from '../utils/isGuildID.js';
import { GuildFetchOptions } from './API.js';
Expand All @@ -17,17 +18,19 @@ class getGuild extends Endpoint {
query: string,
options?: RequestOptions
): Promise<Guild | null | RequestData> {
if (!query) throw new Error(this.client.errors.NO_GUILD_QUERY);
if ('id' === searchParameter && !isGuildID(query)) throw new Error(this.client.errors.INVALID_GUILD_ID);
const isPlayerQuery = 'player' === searchParameter;
if (isPlayerQuery) query = await this.client.requestHandler.toUUID(query);
if (!['id', 'name', 'player'].includes(searchParameter)) {
throw new Error(this.client.errors.INVALID_GUILD_SEARCH_PARAMETER);
throw new Error(this.client.errors.INVALID_GUILD_SEARCH_PARAMETER, 'Fetching Guild Stats');
}
if (!query) throw new Error(this.client.errors.NO_GUILD_QUERY, 'Fetching Guild Stats');
if ('id' === searchParameter && !isGuildID(query)) {
throw new Error(this.client.errors.INVALID_GUILD_ID, 'Fetching Guild Stats');
}
const isPlayerQuery = 'player' === searchParameter;
if (isPlayerQuery) query = await this.client.requestHandler.toUUID(query);
const res = await this.client.requestHandler.request(`/guild?${searchParameter}=${encodeURI(query)}`, options);
if (res.options.raw) return res;
if (!res.data.guild && 'player' !== searchParameter) {
throw new Error(this.client.errors.GUILD_DOES_NOT_EXIST);
throw new Error(this.client.errors.GUILD_DOES_NOT_EXIST, 'Fetching Guild Stats');
}
return res.data.guild ? new Guild(res.data.guild, isPlayerQuery ? query : undefined) : null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/API/getHouse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import House from '../structures/House.js';
import { RequestData, RequestOptions } from '../Private/RequestHandler.js';

Expand All @@ -11,7 +12,7 @@ class getHouse extends Endpoint {
}

async execute(query: string, options?: RequestOptions): Promise<House | RequestData> {
if (!query) throw new Error(this.client.errors.NO_UUID);
if (!query) throw new Error(this.client.errors.NO_UUID, 'Fetching a House');
const res = await this.client.requestHandler.request(`/housing/house?house=${query}`, options);
if (res.options.raw) return res;
return new House(res.data);
Expand Down
5 changes: 4 additions & 1 deletion src/API/getLeaderboards.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import Leaderboard from '../structures/Leaderboard.js';
import { RequestData, RequestOptions } from '../Private/RequestHandler.js';

Expand All @@ -14,7 +15,9 @@ class getLeaderboards extends Endpoint {
const res = await this.client.requestHandler.request('/leaderboards', options);
if (res.options.raw) return res;
if (!res.data.leaderboards) {
throw new Error(this.client.errors.SOMETHING_WENT_WRONG.replace(/{cause}/, 'Try again.'));
throw new Error(this.client.errors.SOMETHING_WENT_WRONG, 'Fetching Leaderboards', {
cause: 'Data is missing. Try again.'
});
}
const leaderboards: Record<string, Leaderboard[]> = {};
Object.keys(res.data.leaderboards).forEach((key) => {
Expand Down
5 changes: 3 additions & 2 deletions src/API/getPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import Guild from '../structures/Guild/Guild.js';
import House from '../structures/House.js';
import Player from '../structures/Player/Player.js';
Expand All @@ -15,11 +16,11 @@ class getPlayer extends Endpoint {
}

async execute(query: string, options?: PlayerRequestOptions): Promise<Player | RequestData> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID, 'Fetching Player');
query = await this.client.requestHandler.toUUID(query);
const res = await this.client.requestHandler.request(`/player?uuid=${query}`, options);
if (res.options.raw) return res;
if (query && !res.data.player) throw new Error(this.client.errors.PLAYER_HAS_NEVER_LOGGED);
if (query && !res.data.player) throw new Error(this.client.errors.PLAYER_HAS_NEVER_LOGGED, 'Fetching Player');
return new Player(res.data.player, {
guild: options?.guild ? ((await this.client.getGuild('player', query)) as Guild) : null,
houses: options?.houses ? ((await this.client.getPlayerHouses(query)) as House[]) : null,
Expand Down
3 changes: 2 additions & 1 deletion src/API/getPlayerHouses.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import House from '../structures/House.js';
import { RequestData, RequestOptions } from '../Private/RequestHandler.js';

Expand All @@ -11,7 +12,7 @@ class getPlayerHouses extends Endpoint {
}

async execute(query: string, options?: RequestOptions): Promise<House[] | RequestData> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID, 'Fetching Player Houses');
query = await this.client.requestHandler.toUUID(query);
const res = await this.client.requestHandler.request(`/housing/houses?player=${query}`, options);
if (res.options.raw) return res;
Expand Down
3 changes: 2 additions & 1 deletion src/API/getRecentGames.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import RecentGame from '../structures/RecentGame.js';
import { RequestData, RequestOptions } from '../Private/RequestHandler.js';

Expand All @@ -11,7 +12,7 @@ class getRecentGames extends Endpoint {
}

async execute(query: string, options?: RequestOptions): Promise<RecentGame[] | RequestData> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID, 'Fetching Recent Games');
query = await this.client.requestHandler.toUUID(query);
const res = await this.client.requestHandler.request(`/recentgames?uuid=${query}`, options);
if (res.options.raw) return res;
Expand Down
5 changes: 3 additions & 2 deletions src/API/getSkyblockAuction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Auction from '../structures/SkyBlock/Auctions/Auction.js';
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import { AuctionFetchOptions, AuctionRequestOptions } from './API.js';
import { RequestData } from '../Private/RequestHandler.js';

Expand All @@ -25,9 +26,9 @@ class getSkyblockAction extends Endpoint {
} else if ('auction' === type) {
filter = 'uuid';
} else {
throw new Error(this.client.errors.BAD_AUCTION_FILTER);
throw new Error(this.client.errors.BAD_AUCTION_FILTER, 'Fetching Skyblock Auction');
}
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID, 'Fetching Skyblock Auction');
const res = await this.client.requestHandler.request(`/skyblock/auction?${filter}=${query}`, options);
if (res.options.raw) return res;
return res.data.auctions.map((a: any) => new Auction(a, options?.includeItemBytes ?? false));
Expand Down
11 changes: 8 additions & 3 deletions src/API/getSkyblockAuctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Auction from '../structures/SkyBlock/Auctions/Auction.js';
import AuctionInfo from '../structures/SkyBlock/Auctions/AuctionInfo.js';
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import { AuctionRequestOptions, SkyblockAuctionsResult } from './API.js';

class getSkyblockAuctions extends Endpoint {
Expand All @@ -13,9 +14,13 @@ class getSkyblockAuctions extends Endpoint {
}

async execute(query: number | '*', options?: AuctionRequestOptions): Promise<SkyblockAuctionsResult> {
if (!query) throw new Error(this.client.errors.INVALID_OPTION_VALUE);
if ('number' === typeof query && 0 >= query) throw new Error(this.client.errors.INVALID_OPTION_VALUE);
if ('number' !== typeof query && '*' !== query) throw new Error(this.client.errors.INVALID_OPTION_VALUE);
if (!query) throw new Error(this.client.errors.INVALID_OPTION_VALUE, 'Fetching Skyblock Auctions');
if ('number' === typeof query && 0 >= query) {
throw new Error(this.client.errors.INVALID_OPTION_VALUE, 'Fetching Skyblock Auctions');
}
if ('number' !== typeof query && '*' !== query) {
throw new Error(this.client.errors.INVALID_OPTION_VALUE, 'Fetching Skyblock Auctions');
}
this.options = this.parseOptions(options);
if ('*' === query) return await this.getAllPages();
return await this.getPage(query);
Expand Down
3 changes: 2 additions & 1 deletion src/API/getSkyblockAuctionsByPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Auction from '../structures/SkyBlock/Auctions/Auction.js';
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import { AuctionRequestOptions } from './API.js';
import { RequestData } from '../Private/RequestHandler.js';

Expand All @@ -12,7 +13,7 @@ class getSkyblockActionsByPlayer extends Endpoint {
}

async execute(query: string, options?: AuctionRequestOptions): Promise<Auction[] | RequestData> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID, 'Fetching Skyb,oock Auctions By Player');
query = await this.client.requestHandler.toUUID(query);
const res = await this.client.requestHandler.request(`/skyblock/auction?player=${query}`, options);
if (res.options.raw) return res;
Expand Down
3 changes: 2 additions & 1 deletion src/API/getSkyblockGarden.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import SkyblockGarden from '../structures/SkyBlock/SkyblockGarden.js';
import { RequestData, RequestOptions } from '../Private/RequestHandler.js';

Expand All @@ -11,7 +12,7 @@ class getSkyblockGarden extends Endpoint {
}

async execute(profileId: string, options?: RequestOptions): Promise<SkyblockGarden | RequestData> {
if (!profileId) throw new Error(this.client.errors.NO_UUID);
if (!profileId) throw new Error(this.client.errors.NO_UUID, 'Fetching Skyblock Garden');
const res = await this.client.requestHandler.request(`/skyblock/garden?profile=${profileId}`, options);
if (res.options.raw) return res;
return new SkyblockGarden(res.data);
Expand Down
7 changes: 5 additions & 2 deletions src/API/getSkyblockMember.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import SkyblockMember from '../structures/SkyBlock/SkyblockMember.js';
import { RequestData } from '../Private/RequestHandler.js';
import { SkyblockRequestOptions } from './API.js';
Expand All @@ -12,11 +13,13 @@ class getSkyblockMember extends Endpoint {
}

async execute(query: string, options?: SkyblockRequestOptions): Promise<Map<string, SkyblockMember> | RequestData> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID, 'Fetching Skyblock Member');
query = await this.client.requestHandler.toUUID(query);
const res = await this.client.requestHandler.request(`/skyblock/profiles?uuid=${query}`, options);
if (res.options.raw) return res;
if (!res.data.profiles || !res.data.profiles.length) throw new Error(this.client.errors.NO_SKYBLOCK_PROFILES);
if (!res.data.profiles || !res.data.profiles.length) {
throw new Error(this.client.errors.NO_SKYBLOCK_PROFILES, 'Fetching Skyblock Member');
}
const memberByProfileName = new Map();
for (const profile of res.data.profiles) {
memberByProfileName.set(
Expand Down
3 changes: 2 additions & 1 deletion src/API/getSkyblockMuseum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import SkyblockMuseum from '../structures/SkyBlock/SkyblockMuseum.js';
import { RequestData, RequestOptions } from '../Private/RequestHandler.js';

Expand All @@ -11,7 +12,7 @@ class getSkyblockMuseum extends Endpoint {
}

async execute(query: string, profileId: string, options?: RequestOptions): Promise<SkyblockMuseum | RequestData> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID, 'Fetching Skyblock Museum');
query = await this.client.requestHandler.toUUID(query);
const res = await this.client.requestHandler.request(
`/skyblock/museum?uuid=${query}&profile=${profileId}`,
Expand Down
9 changes: 6 additions & 3 deletions src/API/getSkyblockProfiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Client from '../Client.js';
import Endpoint from '../Private/Endpoint.js';
import Error from '../Private/ErrorHandler.js';
import SkyblockProfile from '../structures/SkyBlock/SkyblockProfile.js';
import { RequestData } from '../Private/RequestHandler.js';
import { SkyblockRequestOptions } from './API.js';
Expand All @@ -12,11 +13,13 @@ class getSkyblockProfiles extends Endpoint {
}

async execute(query: string, options?: SkyblockRequestOptions): Promise<SkyblockProfile[] | RequestData> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID, 'Fetching Skyblock Profiles');
query = await this.client.requestHandler.toUUID(query);
const res = await this.client.requestHandler.request(`/skyblock/profiles?uuid=${query}`, options);
if (res.options.raw) return res;
if (!res.data.profiles || !res.data.profiles.length) throw new Error(this.client.errors.NO_SKYBLOCK_PROFILES);
if (res.options.raw) return res.data;
if (!res.data.profiles || !res.data.profiles.length) {
throw new Error(this.client.errors.NO_SKYBLOCK_PROFILES, 'Fetching Skyblock Profiles');
}
const profiles = [];
for (let i = 0; i < res.data.profiles.length; i++) {
profiles.push({
Expand Down
2 changes: 1 addition & 1 deletion src/Client.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import CacheHandler from './Private/CacheHandler.js';
import Client, { ClientOptions } from './Client.js';
import Errors from './Errors.js';
import RequestHandler from './Private/RequestHandler.js';
import Updater from './Private/Updater.js';
import { Errors } from './Private/ErrorHandler.js';
import { expect, expectTypeOf, test } from 'vitest';
const errors = new Errors();

test('Client (No Key)', () => {
expect(() => new Client('')).toThrowError(errors.NO_API_KEY);

Check failure on line 10 in src/Client.test.ts

View workflow job for this annotation

GitHub Actions / tests

src/Client.test.ts > Client (No Key)

AssertionError: expected [Function] to throw error including 'No API Key specified! For help join o…' but got 'No API Key specified! For help join o…' Expected: "No API Key specified! For help join our Discord Server {discordInvite}" Received: "No API Key specified! For help join our Discord Server https://discord.gg/NSEBNMM" ❯ src/Client.test.ts:10:32
});

test('Client (No Options)', () => {
Expand Down
Loading
Loading