Skip to content

Commit 060a336

Browse files
weekliesessential-randomness
authored andcommitted
Throw InvalidIDError
1 parent 8aecc67 commit 060a336

File tree

7 files changed

+64
-28
lines changed

7 files changed

+64
-28
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ us help you through the process.
5151

5252
## How to Develop for AO3.js
5353

54-
The first step is alway the same: run `npm install` and install all the
54+
The first step is always the same: run `npm install` and install all the
5555
dependencies for the package.
5656

5757
### How to Run Tests

src/series/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { loadSeriesPage } from "src/page-loaders";
2-
import type { Series } from "types/entities";
1+
import { InvalidIDError, isValidArchiveId, parseArchiveId } from "src/utils";
32
import {
43
getSeriesAuthors,
54
getSeriesBookmarkCount,
@@ -13,15 +12,17 @@ import {
1312
getSeriesWorkCount,
1413
getSeriesWorks,
1514
} from "./getters";
16-
import { isValidArchiveId, parseArchiveId } from "src/utils";
15+
16+
import type { Series } from "types/entities";
17+
import { loadSeriesPage } from "src/page-loaders";
1718

1819
export const getSeries = async ({
1920
seriesId,
2021
}: {
2122
seriesId: string | number;
2223
}): Promise<Series> => {
2324
if (!isValidArchiveId(seriesId)) {
24-
throw new Error(`${seriesId} is not a valid series id`);
25+
throw new InvalidIDError(seriesId, "series");
2526
}
2627

2728
const seriesPage = await loadSeriesPage(seriesId);

src/urls.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
InvalidIDError,
23
isValidArchiveId,
34
isValidArchiveIdOrNullish,
45
parseArchiveId,
@@ -38,7 +39,7 @@ export const getWorkUrl = ({
3839
let workPath = "";
3940

4041
if (!isValidArchiveId(workId)) {
41-
throw new Error(`${workId} is not a valid work id`);
42+
throw new InvalidIDError(workId, "work");
4243
}
4344

4445
if (collectionName) {
@@ -50,7 +51,7 @@ export const getWorkUrl = ({
5051

5152
if (chapterId) {
5253
if (!isValidArchiveId(chapterId)) {
53-
throw new Error(`${workId} is not a valid chapter id`);
54+
throw new InvalidIDError(workId, "chapter");
5455
}
5556

5657
workPath += `/chapters/${chapterId}`;
@@ -61,14 +62,14 @@ export const getWorkUrl = ({
6162

6263
export const getWorkIndexUrl = ({ workId }: { workId: string | number }) => {
6364
if (!isValidArchiveId(workId)) {
64-
throw new Error(`${workId} is not a valid work id`);
65+
throw new InvalidIDError(workId, "work");
6566
}
6667
return new URL(`works/${workId}/navigate`, getArchiveBaseUrl()).href;
6768
};
6869

6970
export const getSeriesUrl = ({ seriesId }: { seriesId: string | number }) => {
7071
if (!isValidArchiveId(seriesId)) {
71-
throw new Error(`${seriesId} is not a valid series id`);
72+
throw new InvalidIDError(seriesId, "series");
7273
}
7374

7475
return new URL(`series/${seriesId}`, getArchiveBaseUrl()).href;
@@ -182,11 +183,11 @@ export const getWorkDetailsFromUrl = ({
182183
const matchedWorkId = workUrlMatch[1];
183184
const matchedChapterId = url.match(/chapters\/(\d+)/)?.[1];
184185
if (!isValidArchiveId(matchedWorkId)) {
185-
throw new Error(`${matchedWorkId} is not a valid work id`);
186+
throw new InvalidIDError(matchedWorkId, "work");
186187
}
187188

188189
if (!isValidArchiveIdOrNullish(matchedChapterId)) {
189-
throw new Error(`${matchedChapterId} is not a valid chapter id`);
190+
throw new InvalidIDError(matchedChapterId, "chapter");
190191
}
191192

192193
return {

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ export const isValidArchiveIdOrNullish = <
1212
id: T
1313
): id is Exclude<T, string> =>
1414
typeof id === "undefined" || id === null || parseArchiveId(id) == id;
15+
16+
export class InvalidIDError extends Error {
17+
message: string;
18+
19+
constructor(id: string | number, type: "work" | "chapter" | "series") {
20+
super();
21+
this.message = `${id} is not a valid ${type} id`;
22+
}
23+
}

src/works/index.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type {
22
Author,
33
Chapter,
4-
WorkContent,
54
LockedWorkSummary,
5+
WorkContent,
66
WorkSummary,
77
} from "types/entities";
88
import {
9-
getChaptersList,
10-
getWorkAuthors as getWorkAuthorsFromChaptersIndex,
11-
getWorkTitle as getWorkTitleFromChaptersIndex,
12-
} from "./chapter-getters";
9+
InvalidIDError,
10+
isValidArchiveId,
11+
isValidArchiveIdOrNullish,
12+
parseArchiveId,
13+
} from "src/utils";
1314
import {
1415
getChapterIndex,
1516
getChapterName,
@@ -41,12 +42,12 @@ import {
4142
getWorkWarnings,
4243
getWorkWordCount,
4344
} from "./work-getters";
44-
import { loadChaptersIndexPage, loadWorkPage } from "src/page-loaders";
4545
import {
46-
isValidArchiveId,
47-
parseArchiveId,
48-
isValidArchiveIdOrNullish,
49-
} from "src/utils";
46+
getChaptersList,
47+
getWorkAuthors as getWorkAuthorsFromChaptersIndex,
48+
getWorkTitle as getWorkTitleFromChaptersIndex,
49+
} from "./chapter-getters";
50+
import { loadChaptersIndexPage, loadWorkPage } from "src/page-loaders";
5051

5152
export const getWork = async ({
5253
workId,
@@ -56,10 +57,10 @@ export const getWork = async ({
5657
chapterId?: string | number;
5758
}): Promise<WorkSummary | LockedWorkSummary> => {
5859
if (!isValidArchiveId(workId)) {
59-
throw new Error(`${workId} is not a valid work id`);
60+
throw new InvalidIDError(workId, "work");
6061
}
6162
if (!isValidArchiveIdOrNullish(chapterId)) {
62-
throw new Error(`${workId} is not a valid chapter id`);
63+
throw new InvalidIDError(workId, "chapter");
6364
}
6465

6566
const workPage = await loadWorkPage({ workId, chapterId });
@@ -134,7 +135,7 @@ export const getWorkWithChapters = async ({
134135
chapters: Chapter[];
135136
}> => {
136137
if (!isValidArchiveId(workId)) {
137-
throw new Error(`${workId} is not a valid work id`);
138+
throw new InvalidIDError(workId, "work");
138139
}
139140
const page = await loadChaptersIndexPage({ workId });
140141

@@ -154,7 +155,7 @@ export const getWorkContent = async ({
154155
chapterId?: string | number | null;
155156
}): Promise<WorkContent> => {
156157
if (!isValidArchiveId(workId) || !isValidArchiveIdOrNullish(chapterId)) {
157-
throw new Error(`${workId} is not a valid work id`);
158+
throw new InvalidIDError(workId, "work");
158159
}
159160
const workPage = await loadWorkPage({
160161
workId,

tests/series.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { InvalidIDError } from "src/utils";
14
import { getSeries } from "src/index";
2-
import { describe, it, expect } from 'vitest';
5+
36
// TODO: Add more tests
47

58
describe("Series/data", () => {
9+
it("should throw InvalidIDError for invalid series ID", async () => {
10+
const invalidSeries = getSeries({ seriesId: "invalid-id" });
11+
12+
await expect(invalidSeries).rejects.toThrow(InvalidIDError);
13+
await expect(invalidSeries).rejects.toThrow(
14+
"invalid-id is not a valid series id"
15+
);
16+
});
17+
618
it("should fetch series information and check top level fields", async () => {
719
const series = await getSeries({ seriesId: "2270465" });
820

tests/works.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { describe, expect, it } from "vitest";
12
import { getWorkDetailsFromUrl, getWorkUrl } from "src/urls";
2-
import { getWork } from "src/index";
3-
import { describe, it, expect } from "vitest";
3+
4+
import { InvalidIDError } from "src/utils";
45
import type { WorkSummary } from "types/entities";
6+
import { getWork } from "src/index";
7+
58
// TODO: this file is too long and should be split into multiple tests
69

710
describe("Works/parse", () => {
@@ -73,6 +76,15 @@ describe("Works/url", () => {
7376
});
7477

7578
describe("Works/data", () => {
79+
it("should throw InvalidIDError for invalid work ID", async () => {
80+
const invalidWork = getWork({ workId: "invalid-id" });
81+
82+
await expect(invalidWork).rejects.toThrow(InvalidIDError);
83+
await expect(invalidWork).rejects.toThrow(
84+
"invalid-id is not a valid work id"
85+
);
86+
});
87+
7688
it("should fetch work information in its entirety", async () => {
7789
const work = await getWork({
7890
workId: 29046888,

0 commit comments

Comments
 (0)