Skip to content

Commit 84e59cf

Browse files
authored
Merge pull request #37 from ynhhoJ/features/35
🚧Throw errors instead of just return errors
2 parents ba6d97e + a87ec52 commit 84e59cf

7 files changed

+24
-18
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flibusta",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"author": "ynhhoJ",
55
"description": "Unofficial Flibusta API based on website search engine. If you like to read books - buy",
66
"license": "MIT",

src/api/getCoverByBookId.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,26 @@ class GetCoverByBookId {
1919
private async fetchImageByTypeUrl(url: string): Promise<Nullable<File>> {
2020
return this.axiosInstance.get<File>(url, {})
2121
.then((response) => response.data)
22-
.catch((error) => error);
22+
.catch((error) => {
23+
throw error;
24+
});
2325
}
2426

2527
private async fetchCoverByUrl(id: number): Promise<Nullable<File>> {
2628
const idAsString = id.toString();
2729
const yAsString = idAsString.slice(4);
2830
const y = Number.parseInt(yAsString, 10);
2931
const jpgImageUrl = GetCoverByBookId.generateCoverUrl(id, y, 'jpg');
30-
const jpgImage = await this.fetchImageByTypeUrl(jpgImageUrl);
32+
// eslint-disable-next-line unicorn/no-useless-undefined
33+
const jpgImage = await this.fetchImageByTypeUrl(jpgImageUrl).catch(() => undefined);
3134

3235
if (!isNil(jpgImage)) {
3336
return jpgImage;
3437
}
3538

3639
const pngImageUrl = GetCoverByBookId.generateCoverUrl(id, y, 'png');
37-
const pngImage = await this.fetchImageByTypeUrl(pngImageUrl);
40+
// eslint-disable-next-line unicorn/no-useless-undefined
41+
const pngImage = await this.fetchImageByTypeUrl(pngImageUrl).catch(() => undefined);
3842

3943
if (!isNil(pngImage)) {
4044
return pngImage;

src/flibustaApiHelper.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ abstract class FlibustaAPIHelper extends FlibustaOpdsApiHelper {
6565
public async getFlibustaHTMLPage(url: string): Promise<HTMLElement | null> {
6666
return this.axiosInstance.get<string>(url)
6767
.then((response) => parse(response.data).querySelector('#main'))
68-
.catch((error) => error);
68+
.catch((error) => {
69+
throw error;
70+
});
6971
}
7072

7173
public getInformationOfBookOrAuthor(node: HTMLElement): Author;

src/flibustaOpdsApiHelper.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ class FlibustaOpdsApiHelper {
141141
const parser = new XMLParser(parsingOptions);
142142

143143
return parser.parse(response.data);
144-
}).catch((error) => error);
144+
}).catch((error) => {
145+
throw error;
146+
});
145147
}
146148
}
147149

tests/api/getCoverByBookId.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ describe('GetCoverBookById', () => {
2828
expect(coverByBookId).to.satisfy((cover: File) => !isNil(cover));
2929
});
3030

31-
it('should return axios error', async () => {
32-
const coverByBookId = await getCoverByBookIdApi.getCoverByBookId(Number.POSITIVE_INFINITY);
31+
it('should return undefined error', async () => {
32+
const coverByBookId = await getCoverByBookIdApi
33+
.getCoverByBookId(Number.POSITIVE_INFINITY);
3334

34-
expect(axios.isAxiosError(coverByBookId)).to.be.equal(true);
35+
expect(isNil(coverByBookId)).to.be.equal(true);
3536
});
3637
});
3738
});

tests/flibustaApiHelper.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ describe('FlibustaAPIHelper', () => {
5959

6060
it('should return axios error when flibusta html page is wrong', async () => {
6161
const url = 'booksed';
62-
const flibustaHTMLPage = await flibustaApiHelper.getFlibustaHTMLPage(url);
63-
64-
if (isNil(flibustaHTMLPage)) {
65-
return;
66-
}
6762

68-
expect(axios.isAxiosError(flibustaHTMLPage)).to.be.equal(true);
63+
await flibustaApiHelper.getFlibustaHTMLPage(url).catch((error) => {
64+
expect(axios.isAxiosError(error)).to.be.equal(true);
65+
});
6966
});
7067
});
7168

tests/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,9 @@ describe('FlibustaAPI', () => {
394394
});
395395

396396
it('should return axios error', async () => {
397-
const coverByBookId = await flibustaApi.getCoverByBookId(Number.POSITIVE_INFINITY);
398-
399-
expect(axios.isAxiosError(coverByBookId)).to.be.equal(true);
397+
await flibustaApi.getCoverByBookId(Number.POSITIVE_INFINITY).catch((error) => {
398+
expect(axios.isAxiosError(error)).to.be.equal(true);
399+
});
400400
});
401401
});
402402
});

0 commit comments

Comments
 (0)