Skip to content

Commit

Permalink
fix: return search card and render messages on adapters errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdonado committed Oct 19, 2023
1 parent f3de376 commit a81a745
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/adapters/apple-music.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import axios from 'axios';

import * as config from '~/config/default';

import { logger } from '~/utils/logger';
import { getCheerioDoc } from '~/utils/scraper';
import { responseMatchesQuery } from '~/utils/compare';
import { getQueryFromMetadata } from '~/utils/query';
Expand Down Expand Up @@ -72,6 +74,8 @@ export async function getAppleMusicLink(
isVerified,
};
} catch (err) {
throw new Error(`[Apple Music] (${query}) ${err}`);
logger.error(`[Apple Music] (${query}) ${err}`);

return undefined;
}
}
3 changes: 2 additions & 1 deletion src/adapters/deezer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export async function getDeezerLink(
isVerified: true,
};
} catch (error) {
throw new Error(`[Deezer] (${query}) ${error}`);
logger.error(`[Deezer] (${query}) ${error}`);
return undefined;
}
}
3 changes: 2 additions & 1 deletion src/adapters/youtube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export async function getYouTubeLink(
isVerified: true,
};
} catch (error) {
throw new Error(`[YouTube] (${query}) ${error}`);
logger.error(`[YouTube] (${query}) ${error}`);
return undefined;
}
}
4 changes: 3 additions & 1 deletion src/routes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import SearchCard from '~/views/components/search-card';
import ErrorMessage from '~/views/components/error-message';

export const pageRouter = new Elysia()
.onError(({ error }) => {
.onError(({ error, set }) => {
logger.error(error);

set.status = 200;

return <ErrorMessage />;
})
.get('/', async () => {
Expand Down
70 changes: 59 additions & 11 deletions tests/integration/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ describe('Api router', () => {
]);
});

it('should return 200 when adapter returns error - YouTube', async () => {
it.only('should return 200 when adapter returns error - YouTube', async () => {
const spotifyLink = 'https://open.spotify.com/track/2KvHC9z14GSl4YpkNMX384';
const query = 'Do%20Not%20Disturb%20Drake';

Expand Down Expand Up @@ -576,13 +576,37 @@ describe('Api router', () => {
const response = await app.handle(request).then(res => res.json());

expect(response).toEqual({
code: 'UNKNOWN',
message:
'[YouTube] (Do%20Not%20Disturb%20Drake) Error: Request failed with status code 400',
id: '2KvHC9z14GSl4YpkNMX384',
type: 'music.song',
title: 'Do Not Disturb',
description: 'Drake · Song · 2017',
image: 'https://i.scdn.co/image/ab67616d0000b2734f0fd9dad63977146e685700',
audio: 'https://p.scdn.co/mp3-preview/df989a31c8233f46b6a997c59025f9c8021784aa',
source: 'https://open.spotify.com/track/2KvHC9z14GSl4YpkNMX384',
links: [
{
type: 'appleMusic',
url: 'https://music.apple.com/us/album/do-not-disturb/1440890708?i=1440892237',
isVerified: true,
},
{
type: 'deezer',
url: 'https://www.deezer.com/track/144572248',
isVerified: true,
},
{
type: 'soundCloud',
url: 'https://soundcloud.com/search/sounds?q=Do%20Not%20Disturb%20Drake',
},
{
type: 'tidal',
url: 'https://listen.tidal.com/search?q=Do%20Not%20Disturb%20Drake',
},
],
});

expect(redisGetMock).toHaveBeenCalledTimes(1);
expect(redisSetMock).toHaveBeenCalledTimes(0);
expect(redisGetMock).toHaveBeenCalledTimes(2);
expect(redisSetMock).toHaveBeenCalledTimes(2);
});

it('should return 200 when adapter returns error - Deezer', async () => {
Expand All @@ -606,13 +630,37 @@ describe('Api router', () => {
const response = await app.handle(request).then(res => res.json());

expect(response).toEqual({
code: 'UNKNOWN',
message:
'[Deezer] (Do%20Not%20Disturb%20Drake) Error: Request failed with status code 500',
id: '2KvHC9z14GSl4YpkNMX384',
type: 'music.song',
title: 'Do Not Disturb',
description: 'Drake · Song · 2017',
image: 'https://i.scdn.co/image/ab67616d0000b2734f0fd9dad63977146e685700',
audio: 'https://p.scdn.co/mp3-preview/df989a31c8233f46b6a997c59025f9c8021784aa',
source: 'https://open.spotify.com/track/2KvHC9z14GSl4YpkNMX384',
links: [
{
type: 'appleMusic',
url: 'https://music.apple.com/us/album/do-not-disturb/1440890708?i=1440892237',
isVerified: true,
},
{
type: 'youTube',
url: 'https://www.youtube.com/watch?v=zhY_0DoQCQs',
isVerified: true,
},
{
type: 'soundCloud',
url: 'https://soundcloud.com/search/sounds?q=Do%20Not%20Disturb%20Drake',
},
{
type: 'tidal',
url: 'https://listen.tidal.com/search?q=Do%20Not%20Disturb%20Drake',
},
],
});

expect(redisGetMock).toHaveBeenCalledTimes(1);
expect(redisSetMock).toHaveBeenCalledTimes(0);
expect(redisGetMock).toHaveBeenCalledTimes(2);
expect(redisSetMock).toHaveBeenCalledTimes(2);
});

it('should return unknown error - could not parse Spotify metadata', async () => {
Expand Down
4 changes: 3 additions & 1 deletion www/bin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { logger } from '~/utils/logger';

import { app } from '~/index';

app.listen(Bun.env.PORT ?? 3000);
console.log(`Server is running at ${app.server?.hostname}:${app.server?.port}`);
logger.info(`Server is running at ${app.server?.hostname}:${app.server?.port}`);

0 comments on commit a81a745

Please sign in to comment.