Skip to content

Commit

Permalink
Merge pull request #37 from ynhhoJ/features/35
Browse files Browse the repository at this point in the history
🚧Throw errors instead of just return errors
  • Loading branch information
ynhhoJ authored Sep 15, 2022
2 parents ba6d97e + a87ec52 commit 84e59cf
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flibusta",
"version": "0.4.0",
"version": "0.4.1",
"author": "ynhhoJ",
"description": "Unofficial Flibusta API based on website search engine. If you like to read books - buy",
"license": "MIT",
Expand Down
10 changes: 7 additions & 3 deletions src/api/getCoverByBookId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,26 @@ class GetCoverByBookId {
private async fetchImageByTypeUrl(url: string): Promise<Nullable<File>> {
return this.axiosInstance.get<File>(url, {})
.then((response) => response.data)
.catch((error) => error);
.catch((error) => {
throw error;
});
}

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

if (!isNil(jpgImage)) {
return jpgImage;
}

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

if (!isNil(pngImage)) {
return pngImage;
Expand Down
4 changes: 3 additions & 1 deletion src/flibustaApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ abstract class FlibustaAPIHelper extends FlibustaOpdsApiHelper {
public async getFlibustaHTMLPage(url: string): Promise<HTMLElement | null> {
return this.axiosInstance.get<string>(url)
.then((response) => parse(response.data).querySelector('#main'))
.catch((error) => error);
.catch((error) => {
throw error;
});
}

public getInformationOfBookOrAuthor(node: HTMLElement): Author;
Expand Down
4 changes: 3 additions & 1 deletion src/flibustaOpdsApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ class FlibustaOpdsApiHelper {
const parser = new XMLParser(parsingOptions);

return parser.parse(response.data);
}).catch((error) => error);
}).catch((error) => {
throw error;
});
}
}

Expand Down
7 changes: 4 additions & 3 deletions tests/api/getCoverByBookId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ describe('GetCoverBookById', () => {
expect(coverByBookId).to.satisfy((cover: File) => !isNil(cover));
});

it('should return axios error', async () => {
const coverByBookId = await getCoverByBookIdApi.getCoverByBookId(Number.POSITIVE_INFINITY);
it('should return undefined error', async () => {
const coverByBookId = await getCoverByBookIdApi
.getCoverByBookId(Number.POSITIVE_INFINITY);

expect(axios.isAxiosError(coverByBookId)).to.be.equal(true);
expect(isNil(coverByBookId)).to.be.equal(true);
});
});
});
9 changes: 3 additions & 6 deletions tests/flibustaApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ describe('FlibustaAPIHelper', () => {

it('should return axios error when flibusta html page is wrong', async () => {
const url = 'booksed';
const flibustaHTMLPage = await flibustaApiHelper.getFlibustaHTMLPage(url);

if (isNil(flibustaHTMLPage)) {
return;
}

expect(axios.isAxiosError(flibustaHTMLPage)).to.be.equal(true);
await flibustaApiHelper.getFlibustaHTMLPage(url).catch((error) => {
expect(axios.isAxiosError(error)).to.be.equal(true);
});
});
});

Expand Down
6 changes: 3 additions & 3 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ describe('FlibustaAPI', () => {
});

it('should return axios error', async () => {
const coverByBookId = await flibustaApi.getCoverByBookId(Number.POSITIVE_INFINITY);

expect(axios.isAxiosError(coverByBookId)).to.be.equal(true);
await flibustaApi.getCoverByBookId(Number.POSITIVE_INFINITY).catch((error) => {
expect(axios.isAxiosError(error)).to.be.equal(true);
});
});
});
});
Expand Down

0 comments on commit 84e59cf

Please sign in to comment.