Skip to content

Commit

Permalink
Merge pull request #85 from seditionist/main
Browse files Browse the repository at this point in the history
Add Custom Errors
  • Loading branch information
maxswa authored Nov 27, 2023
2 parents 99ea3fb + 331a448 commit 55312ba
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 98 deletions.
12 changes: 9 additions & 3 deletions __tests__/hiscores.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getSkillPageURL,
getStatsURL,
BOSSES,
INVALID_FORMAT_ERROR,
InvalidFormatError,
BH_MODES,
parseJsonStats,
HiscoresResponse
Expand Down Expand Up @@ -276,12 +276,12 @@ test('Parse CSV with unknown activity', () => {
const statsWithUnknownActivity = `${lynxTitanStats}
-1,-1`;
expect(() => parseStats(statsWithUnknownActivity)).toThrow(
INVALID_FORMAT_ERROR
InvalidFormatError
);
});

test('Parse invalid CSV', () => {
expect(() => parseStats('invalid')).toThrow(INVALID_FORMAT_ERROR);
expect(() => parseStats('invalid')).toThrow(InvalidFormatError);
});

describe('Get name format', () => {
Expand Down Expand Up @@ -580,6 +580,12 @@ describe('Get stats options', () => {
)
).toBeFalsy();
});
it('omits excluded gamemodes', async () => {
const response = await getStats(rsn, {
otherGamemodes: ['ironman', 'ultimate']
});
expect(response.hardcore).toBeUndefined();
});
});

test('CSV and JSON parsing outputs identical object', async () => {
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@
],
"ignorePatterns": [
"**/@types/*"
]
],
"rules": {
"max-classes-per-file": "off"
}
},
"prettier": {
"trailingComma": "none",
Expand Down
167 changes: 77 additions & 90 deletions src/hiscores.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
AxiosRequestConfig,
AxiosResponse,
InternalAxiosRequestConfig
} from 'axios';
import axios, { AxiosRequestConfig } from 'axios';
import { BinaryData, JSDOM } from 'jsdom';
import {
Player,
Expand Down Expand Up @@ -35,9 +31,10 @@ import {
getActivityPageURL,
httpGet,
BOSSES,
INVALID_FORMAT_ERROR,
InvalidFormatError,
PlayerNotFoundError,
HiScoresError,
validateRSN,
PLAYER_NOT_FOUND_ERROR,
FORMATTED_SKILL_NAMES,
FORMATTED_BH_NAMES,
FORMATTED_CLUE_NAMES,
Expand Down Expand Up @@ -68,8 +65,12 @@ export async function getOfficialStats(
try {
const response = await httpGet<HiscoresResponse>(url, config);
return response.data;
} catch {
throw Error(PLAYER_NOT_FOUND_ERROR);
} catch (err) {
if (!axios.isAxiosError(err)) throw err;

if (err.response?.status === 404) throw new PlayerNotFoundError();

throw new HiScoresError();
}
}

Expand Down Expand Up @@ -99,9 +100,9 @@ export async function getRSNFormat(
if (anchor) {
return rsnFromElement(anchor);
}
throw Error(PLAYER_NOT_FOUND_ERROR);
throw new PlayerNotFoundError();
} catch {
throw Error(PLAYER_NOT_FOUND_ERROR);
throw new HiScoresError();
}
}

Expand Down Expand Up @@ -191,7 +192,7 @@ export function parseStats(csv: string): Stats {
splitCSV.length !==
SKILLS.length + BH_MODES.length + CLUES.length + BOSSES.length + 5
) {
throw Error(INVALID_FORMAT_ERROR);
throw new InvalidFormatError();
}

const skillObjects: Skill[] = splitCSV
Expand Down Expand Up @@ -284,91 +285,77 @@ export async function getStats(
];
const shouldGetFormattedRsn = options?.shouldGetFormattedRsn ?? true;

const mainRes = await httpGet<HiscoresResponse>(
getStatsURL('main', rsn, true),
options?.axiosConfigs?.main
);
if (mainRes.status === 200) {
const emptyResponse: AxiosResponse<HiscoresResponse> = {
status: 404,
data: { skills: [], activities: [] },
statusText: '',
headers: {},
config: {} as InternalAxiosRequestConfig
};
const getModeStats = async (
mode: Extract<Gamemode, 'ironman' | 'hardcore' | 'ultimate'>
): Promise<AxiosResponse<HiscoresResponse>> =>
otherGamemodes.includes(mode)
? httpGet<HiscoresResponse>(
getStatsURL(mode, rsn, true),
options?.axiosConfigs?.[mode]
).catch((err) => err)
: emptyResponse;
const formattedName = shouldGetFormattedRsn
? await getRSNFormat(rsn, options?.axiosConfigs?.rsn).catch(
() => undefined
)
: undefined;
const main = await getOfficialStats(rsn, 'main', options?.axiosConfigs?.main);

const player: Player = {
name: formattedName ?? rsn,
mode: 'main',
dead: false,
deulted: false,
deironed: false
};
player.main = parseJsonStats(mainRes.data);

const ironRes = await getModeStats('ironman');
if (ironRes.status === 200) {
player.ironman = parseJsonStats(ironRes.data);
const hcRes = await getModeStats('hardcore');
const ultRes = await getModeStats('ultimate');
if (hcRes.status === 200) {
player.mode = 'hardcore';
player.hardcore = parseJsonStats(hcRes.data);
if (
player.ironman.skills.overall.xp !== player.hardcore.skills.overall.xp
) {
player.dead = true;
player.mode = 'ironman';
}
if (
player.main.skills.overall.xp !== player.ironman.skills.overall.xp
) {
player.deironed = true;
player.mode = 'main';
}
} else if (ultRes.status === 200) {
player.mode = 'ultimate';
player.ultimate = parseJsonStats(ultRes.data);
if (
player.ironman.skills.overall.xp !== player.ultimate.skills.overall.xp
) {
player.deulted = true;
player.mode = 'ironman';
}
if (
player.main.skills.overall.xp !== player.ironman.skills.overall.xp
) {
player.deironed = true;
player.mode = 'main';
}
} else {
const getModeStats = async (
mode: Extract<Gamemode, 'ironman' | 'hardcore' | 'ultimate'>
): Promise<HiscoresResponse | undefined> =>
otherGamemodes.includes(mode)
? getOfficialStats(rsn, mode, options?.axiosConfigs?.[mode])
.catch(() => undefined)
: undefined;
const formattedName = shouldGetFormattedRsn
? await getRSNFormat(rsn, options?.axiosConfigs?.rsn).catch(
() => undefined
)
: undefined;

const player: Player = {
name: formattedName ?? rsn,
mode: 'main',
dead: false,
deulted: false,
deironed: false
};
player.main = parseJsonStats(main);

const iron = await getModeStats('ironman');
if (iron) {
player.ironman = parseJsonStats(iron);
const hc = await getModeStats('hardcore');
const ult = await getModeStats('ultimate');
if (hc) {
player.mode = 'hardcore';
player.hardcore = parseJsonStats(hc);
if (
player.ironman.skills.overall.xp !== player.hardcore.skills.overall.xp
) {
player.dead = true;
player.mode = 'ironman';
if (
}
if (
player.main.skills.overall.xp !== player.ironman.skills.overall.xp
) {
player.deironed = true;
player.mode = 'main';
}
} else if (ult) {
player.mode = 'ultimate';
player.ultimate = parseJsonStats(ult);
if (
player.ironman.skills.overall.xp !== player.ultimate.skills.overall.xp
) {
player.deulted = true;
player.mode = 'ironman';
}
if (
player.main.skills.overall.xp !== player.ironman.skills.overall.xp
) {
player.deironed = true;
player.mode = 'main';
}
} else {
player.mode = 'ironman';
if (
player.main.skills.overall.xp !== player.ironman.skills.overall.xp
) {
player.deironed = true;
player.mode = 'main';
}
player.deironed = true;
player.mode = 'main';
}
}

return player;
}
throw Error(PLAYER_NOT_FOUND_ERROR);

return player;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,40 @@ export const FORMATTED_RIFTS_CLOSED = 'Rifts closed';

export const INVALID_FORMAT_ERROR = 'Invalid hiscores format';
export const PLAYER_NOT_FOUND_ERROR = 'Player not found';
export const HISCORES_ERROR = 'HiScores not responding';

export class InvalidFormatError extends Error {
__proto__ = Error;

constructor() {
super(INVALID_FORMAT_ERROR);
Object.setPrototypeOf(this, InvalidFormatError.prototype);
}
}

export class InvalidRSNError extends Error {
__proto__ = Error;

constructor(message: string) {
super(message);
Object.setPrototypeOf(this, InvalidRSNError.prototype);
}
}

export class PlayerNotFoundError extends Error {
__proto__ = Error;

constructor() {
super(PLAYER_NOT_FOUND_ERROR);
Object.setPrototypeOf(this, PlayerNotFoundError.prototype);
}
}

export class HiScoresError extends Error {
__proto__ = Error;

constructor() {
super(HISCORES_ERROR);
Object.setPrototypeOf(this, HiScoresError.prototype);
}
}
9 changes: 5 additions & 4 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
SCORES_URL,
SKILLS,
ACTIVITIES,
JSON_STATS_URL
JSON_STATS_URL,
InvalidRSNError
} from './constants';

/**
Expand Down Expand Up @@ -119,10 +120,10 @@ export const httpGet = <Response>(
*/
export const validateRSN = (rsn: string) => {
if (typeof rsn !== 'string') {
throw Error('RSN must be a string');
throw new InvalidRSNError('RSN must be a string');
} else if (!/^[a-zA-Z0-9 _-]+$/.test(rsn)) {
throw Error('RSN contains invalid character');
throw new InvalidRSNError('RSN contains invalid character');
} else if (rsn.length > 12 || rsn.length < 1) {
throw Error('RSN must be between 1 and 12 characters');
throw new InvalidRSNError('RSN must be between 1 and 12 characters');
}
};

0 comments on commit 55312ba

Please sign in to comment.