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

Commit

Permalink
Better Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Aug 26, 2024
1 parent cc1766c commit 3592c0f
Show file tree
Hide file tree
Showing 24 changed files with 154 additions and 107 deletions.
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 { RequestOptions } from '../Private/Requests';
import Guild from '../structures/Guild/Guild';
import Error from '../Private/ErrorHandler';
import isGuildID from '../utils/isGuildID';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
Expand All @@ -16,17 +17,19 @@ class getGuild extends Endpoint {
query: string,
options?: RequestOptions
): Promise<Guild | null> {
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.requests.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.requests.toUUID(query);
const res = await this.client.requests.request(`/guild?${searchParameter}=${encodeURI(query)}`, options);
if (res.options.raw) return res.data;
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,4 +1,5 @@
import { RequestOptions } from '../Private/Requests';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import Client from '../Client';
Expand All @@ -11,7 +12,7 @@ class getHouse extends Endpoint {
}

async execute(query: string, options?: RequestOptions): Promise<House> {
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.requests.request(`/housing/house?house=${query}`, options);
if (res.options.raw) return res.data;
return new House(res.data);
Expand Down
7 changes: 5 additions & 2 deletions src/API/getLeaderboards.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RequestOptions } from '../Private/Requests';
import Leaderboard from '../structures/Leaderboard';
import Constants from '../utils/Constants';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import Constants from '../utils/Constants';
import Client from '../Client';

class getLeaderboards extends Endpoint {
Expand All @@ -15,7 +16,9 @@ class getLeaderboards extends Endpoint {
const res = await this.client.requests.request('/leaderboards', options);
if (res.options.raw) return res.data;
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 lbnames = Object.create(Constants.leaderboardNames);
for (const name in lbnames) {
Expand Down
5 changes: 3 additions & 2 deletions src/API/getPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PlayerRequestOptions } from './API';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import Player from '../structures/Player';
import Client from '../Client';
Expand All @@ -11,11 +12,11 @@ class getPlayer extends Endpoint {
}

async execute(query: string, options?: PlayerRequestOptions): Promise<Player> {
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.requests.toUUID(query);
const res = await this.client.requests.request(`/player?uuid=${query}`, options);
if (res.options.raw) return res.data;
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, {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
Expand Down
3 changes: 2 additions & 1 deletion src/API/getPlayerHouses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RequestOptions } from '../Private/Requests';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import Client from '../Client';
Expand All @@ -11,7 +12,7 @@ class getPlayerHouses extends Endpoint {
}

async execute(query: string, options?: RequestOptions): Promise<House[]> {
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.requests.toUUID(query);
const res = await this.client.requests.request(`/housing/houses?player=${query}`, options);
if (res.options.raw) return res.data;
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 { RequestOptions } from '../Private/Requests';
import RecentGame from '../structures/RecentGame';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

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

async execute(query: string, options?: RequestOptions): Promise<RecentGame[]> {
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.requests.toUUID(query);
const res = await this.client.requests.request(`/recentgames?uuid=${query}`, options);
if (res.options.raw) return res.data;
Expand Down
9 changes: 6 additions & 3 deletions src/API/getSkyblockAuction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Auction from '../structures/SkyBlock/Auctions/Auction';
import { AuctionRequestOptions } from './API';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

Expand All @@ -24,12 +25,14 @@ 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.requests.request(`/skyblock/auction?${filter}=${query}`, options);
if (res.options.raw) return res.data;
return res.data.auctions.map((a: any) => new Auction(a, options?.includeItemBytes ?? false));
return res.data.auctions.map(
(a: Record<string, string | number | object>) => 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
@@ -1,6 +1,7 @@
import AuctionInfo from '../structures/SkyBlock/Auctions/AuctionInfo';
import Auction from '../structures/SkyBlock/Auctions/Auction';
import { AuctionRequestOptions } from './API';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

Expand All @@ -16,9 +17,13 @@ class getSkyblockAuctions extends Endpoint {
query: number | '*',
options?: AuctionRequestOptions
): Promise<{ info: AuctionInfo; auctions: Auction[] }> {
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,5 +1,6 @@
import Auction from '../structures/SkyBlock/Auctions/Auction';
import { AuctionRequestOptions } from './API';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

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

async execute(query: string, options?: AuctionRequestOptions): Promise<Auction[]> {
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.requests.toUUID(query);
const res = await this.client.requests.request(`/skyblock/auction?player=${query}`);
if (res.options.raw) return res.data;
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 SkyblockGarden from '../structures/SkyBlock/SkyblockGarden';
import { RequestOptions } from '../Private/Requests';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

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

async execute(profileId: string, options?: RequestOptions): Promise<SkyblockGarden> {
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.requests.request(`/skyblock/garden?profile=${profileId}`, options);
if (res.options.raw) return res.data;
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 SkyblockMember from '../structures/SkyBlock/SkyblockMember';
import { SkyblockRequestOptions } from './API';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

Expand All @@ -11,11 +12,13 @@ class getSkyblockMember extends Endpoint {
}

async execute(query: string, options?: SkyblockRequestOptions): Promise<Map<string, SkyblockMember>> {
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.requests.toUUID(query);
const res = await this.client.requests.request(`/skyblock/profiles?uuid=${query}`, options);
if (res.options.raw) return res.data;
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 SkyblockMuseum from '../structures/SkyBlock/SkyblockMuseum';
import { RequestOptions } from '../Private/Requests';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

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

async execute(query: string, profileId: string, options?: RequestOptions): Promise<SkyblockMuseum> {
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.requests.toUUID(query);
const res = await this.client.requests.request(`/skyblock/museum?uuid=${query}&profile=${profileId}`, options);
if (res.options.raw) return res.data;
Expand Down
7 changes: 5 additions & 2 deletions src/API/getSkyblockProfiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SkyblockProfile from '../structures/SkyBlock/SkyblockProfile';
import { SkyblockRequestOptions } from './API';
import Error from '../Private/ErrorHandler';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

Expand All @@ -11,11 +12,13 @@ class getSkyblockProfiles extends Endpoint {
}

async execute(query: string, options?: SkyblockRequestOptions): Promise<SkyblockProfile[]> {
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.requests.toUUID(query);
const res = await this.client.requests.request(`/skyblock/profiles?uuid=${query}`, options);
if (res.options.raw) return res.data;
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 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
Expand Up @@ -4,7 +4,7 @@ import { ClientOptions } from './typings/Client';
import Requests from './Private/Requests';
import Updater from './Private/Updater';
import Client from './Client';
import Errors from './Errors';
import Errors from './Private/ErrorHandler';
const errors = new Errors();

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

View workflow job for this annotation

GitHub Actions / build

Expected 2-3 arguments, but got 0.

test('Client (No Key)', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Error, { Errors } from './Private/ErrorHandler';
import CacheHandler from './Private/CacheHandler';
import { ClientOptions } from './typings/Client';
import RateLimit from './Private/RateLimit';
import Requests from './Private/Requests';
import Updater from './Private/Updater';
import Errors from './Errors';
import API from './API';

const clients: Client[] = [];

Check warning on line 9 in src/Client.ts

View workflow job for this annotation

GitHub Actions / check linting (eslint)

'Client' was used before it was defined
Expand All @@ -20,7 +20,7 @@ class Client {
constructor(key: string, options?: ClientOptions) {
this.key = key;
this.errors = new Errors();
if (!this.key.length) throw new Error(this.errors.NO_API_KEY);
if (!this.key.length) throw new Error(this.errors.NO_API_KEY, 'Initializing Client');
this.options = this.parasOptions(options);
this.requests = new Requests(this);
this.cacheHandler = new CacheHandler(this);
Expand Down
42 changes: 0 additions & 42 deletions src/Errors.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/Private/Endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Error from '../Private/ErrorHandler';
import Client from '../Client';

class Endpoint {
Expand All @@ -7,7 +8,7 @@ class Endpoint {
}

execute(...args: any[]): Promise<any> | any {
throw new Error(this.client.errors.NOT_IMPLEMENTED);
throw new Error(this.client.errors.NOT_IMPLEMENTED, 'Unknown endpoint');
}
}

Expand Down
Loading

0 comments on commit 3592c0f

Please sign in to comment.