Skip to content

Commit

Permalink
better errors
Browse files Browse the repository at this point in the history
InvalidUidFormatError extends RequestError instead of MihomoError
  • Loading branch information
yuko1101 committed Jun 1, 2023
1 parent 475e2ec commit 8736a76
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 2 additions & 4 deletions src/client/StarRail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ class StarRail {
const response = await fetchJSON(url, this, true);

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));
throw new InvalidUidFormatError(Number(uid), response.status, response.statusText);
default:
throw new MihomoError(`Unknown error occurred. Error: ${response.data["detail"]}`);
throw new RequestError(`Request failed with unknown status code ${response.status} - ${response.statusText}\nError Detail: ${response.data["detail"]}\nRequest url: ${url}`, response.status, response.statusText);
}
} else if (response.data["ErrCode"]) {
switch (response.data["ErrCode"]) {
Expand Down
12 changes: 7 additions & 5 deletions src/errors/InvalidUidFormatError.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import MihomoError from "./MihomoError";
import RequestError from "./RequestError";

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

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

this.name = "InvalidUidFormatError";
this.uid = uid;
Expand Down

1 comment on commit 8736a76

@vercel
Copy link

@vercel vercel bot commented on 8736a76 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-yuko1101.vercel.app
starrail.vercel.app
starrail-git-main-yuko1101.vercel.app

Please sign in to comment.