diff --git a/api.ts b/api.ts index 4c7cbec75..cc0fdf5c0 100644 --- a/api.ts +++ b/api.ts @@ -16,20 +16,27 @@ const db = new Nedb({ export class Api { constructor() { - ipcMain.on('parse-playlist-by-url', (event, args) => { - axios.get(args.url).then((result) => { - const array = result.data.split('\n'); - const parsedPlaylist = this.parsePlaylist(array); - const playlistObject = this.createPlaylistObject( - args.title, - parsedPlaylist, - args.url - ); - this.insertToDb(playlistObject); - event.sender.send('parse-response', { - payload: playlistObject, + ipcMain.on('parse-playlist-by-url', async (event, args) => { + try { + await axios.get(args.url).then((result) => { + const array = result.data.split('\n'); + const parsedPlaylist = this.parsePlaylist(array); + const playlistObject = this.createPlaylistObject( + args.title, + parsedPlaylist, + args.url + ); + this.insertToDb(playlistObject); + event.sender.send('parse-response', { + payload: playlistObject, + }); }); - }); + } catch (err) { + event.sender.send('error', { + message: err.response.statusText, + status: err.response.status, + }); + } }); ipcMain.on('parse-playlist', (event, args) => { diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index 708c33e8e..87506fda6 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -42,6 +42,13 @@ export class HomeComponent { this.electronService.ipcRenderer.send('playlists-all'); }, }, + { + id: 'error', + execute: (response: { message: string; status: number }) => + this.showNotification( + `Error: ${response.status} ${response.message}. Please check the entered playlist URL again.` + ), + }, ]; /** @@ -84,12 +91,8 @@ export class HomeComponent { * @param fileName name of the uploaded file */ rejectFile(fileName: string): void { - this.snackBar.open( - `File was rejected, unsupported file format (${fileName}).`, - null, - { - duration: 2000, - } + this.showNotification( + `File was rejected, unsupported file format (${fileName}).` ); this.isLoading = false; } @@ -176,4 +179,15 @@ export class HomeComponent { return ''; } } + + /** + * Shows snack bar notification with a given message + * @param message message to show + * @param duration visibility duration of the snackbar + */ + showNotification(message: string, duration = 2000): void { + this.snackBar.open(message, null, { + duration, + }); + } }