Skip to content

Commit

Permalink
Throws MihomoError in StarRail#fetchUser
Browse files Browse the repository at this point in the history
  • Loading branch information
yuko1101 committed Jun 1, 2023
1 parent 91e9e43 commit 11fdf24
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 4 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cSpell.words": [
"autodrain",
"Mihomo",
"starfaring",
"starrail",
"Trailblaze",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 0.3.1
- Added CharacterData#eidolons and Eidolon class.
- Added CharacterData#skillTreeNodes.
- Throws MihomoError if an error occurs when requesting with StarRail#fetchUser.
# 0.3.0
- Added StarRail#fetchUser.
- Renamed RelicSubStat to RelicSubStatData.
Expand Down
18 changes: 18 additions & 0 deletions src/client/StarRail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import RelicData from "../models/relic/RelicData";
import { fetchJSON } from "../utils/axios_utils";
import RequestError from "../errors/RequestError";
import User from "../models/User";
import InvalidUidFormatError from "../errors/InvalidUidFormatError";
import UserNotFoundError from "../errors/UserNotFoundError";
import MihomoError from "../errors/MihomoError";

const defaultImageBaseUrls: ImageBaseUrl[] = [];

Expand Down Expand Up @@ -49,6 +52,7 @@ class StarRail {

/**
* @param uid
* @throws {MihomoError}
*/
async fetchUser(uid: number | string): Promise<User> {
if (isNaN(Number(uid))) throw new Error("Parameter `uid` must be a number or a string number.");
Expand All @@ -60,6 +64,20 @@ class StarRail {

if (response.status !== 200) {
throw new RequestError(`Request failed with unknown status code ${response.status} - ${response.statusText}\nRequest url: ${url}`, response.status, response.statusText);
} else if (response.data["detail"]) {
switch (response.data["detail"]) {
case "Invalid uid":
throw new InvalidUidFormatError(Number(uid));
default:
throw new MihomoError(`Unknown error occurred. Error: ${response.data["detail"]}`);
}
} else if (response.data["ErrCode"]) {
switch (response.data["ErrCode"]) {
case 3612:
throw new UserNotFoundError(Number(uid));
default:
throw new MihomoError(`Unknown error occurred. ErrorCode: ${response.data["ErrCode"]}`);
}
}

return new User({ ...response.data }, this);
Expand Down
22 changes: 22 additions & 0 deletions src/errors/InvalidUidFormatError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import MihomoError from "./MihomoError";

/**
* @en InvalidUidFormatError
* @extends {MihomoError}
*/
class InvalidUidFormatError extends MihomoError {
/** */
readonly uid: number;

/**
* @param uid
*/
constructor(uid: number) {
super(`Invalid uid format. (${uid} provided.)`);

this.name = "InvalidUidFormatError";
this.uid = uid;
}
}

export default InvalidUidFormatError;
14 changes: 14 additions & 0 deletions src/errors/MihomoError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @en MihomoError
* @extends {Error}
*/
class MihomoError extends Error {
/**
* @param message
*/
constructor(message: string) {
super(message);
}
}

export default MihomoError;
6 changes: 4 additions & 2 deletions src/errors/RequestError.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import MihomoError from "./MihomoError";

/**
* @en RequestError
* @extends {Error}
* @extends {MihomoError}
*/
class RequestError extends Error {
class RequestError extends MihomoError {
/** HTTP response status code */
readonly statusCode: number;
/** The message of the status code */
Expand Down
22 changes: 22 additions & 0 deletions src/errors/UserNotFoundError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import MihomoError from "./MihomoError";

/**
* @en UserNotFoundError
* @extends {MihomoError}
*/
class UserNotFoundError extends MihomoError {
/** */
readonly uid: number;

/**
* @param uid
*/
constructor(uid: number) {
super(`The user with uid ${uid} was not found.`);

this.name = "UserNotFoundError";
this.uid = uid;
}
}

export default UserNotFoundError;
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import CachedAssetsManager from "./client/CachedAssetsManager";
import ObjectKeysManager from "./client/ObjectKeysManager";
import StarRail from "./client/StarRail";
import AssetsNotFoundError from "./errors/AssetsNotFoundError";
import InvalidUidFormatError from "./errors/InvalidUidFormatError";
import MihomoError from "./errors/MihomoError";
import RequestError from "./errors/RequestError";
import UserNotFoundError from "./errors/UserNotFoundError";
import ImageAssets from "./models/assets/ImageAssets";
import TextAssets from "./models/assets/TextAssets";
import LeveledSkill from "./models/character/skill/LeveledSkill";
import Skill from "./models/character/skill/Skill";
import Character from "./models/character/Character";
import CharacterData from "./models/character/CharacterData";
import Eidolon from "./models/character/Eidolon";
import LightCone from "./models/light_cone/LightCone";
import LightConeData from "./models/light_cone/LightConeData";
import LightConeExpType from "./models/light_cone/LightConeExpType";
Expand All @@ -31,13 +35,17 @@ export {
ObjectKeysManager,
StarRail,
AssetsNotFoundError,
InvalidUidFormatError,
MihomoError,
RequestError,
UserNotFoundError,
ImageAssets,
TextAssets,
LeveledSkill,
Skill,
Character,
CharacterData,
Eidolon,
LightCone,
LightConeData,
LightConeExpType,
Expand Down Expand Up @@ -72,4 +80,5 @@ export { StatPropertyType } from "./models/StatProperty";
export { Birthday } from "./models/User";

// functions
export { fetchJSON } from "./utils/axios_utils";
export { fetchJSON } from "./utils/axios_utils";
export { getStableHash } from "./utils/hash_utils";
1 change: 0 additions & 1 deletion src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class User {

this._data = data;

// TODO: error handling (e.g. user does not exist)
const json = new JsonReader(this._data);
const playerDetailInfo = json.get("PlayerDetailInfo");

Expand Down

1 comment on commit 11fdf24

@vercel
Copy link

@vercel vercel bot commented on 11fdf24 Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

starrail – ./

starrail-git-main-yuko1101.vercel.app
starrail.vercel.app
starrail-yuko1101.vercel.app

Please sign in to comment.