diff --git a/api.ts b/api.ts index 9ca9bf383..096bc05e3 100644 --- a/api.ts +++ b/api.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/no-misused-promises */ import { app, BrowserWindow, ipcMain } from 'electron'; import { parse } from 'iptv-playlist-parser'; import axios from 'axios'; @@ -11,10 +10,12 @@ import { EPG_FETCH_DONE, EPG_GET_PROGRAM, EPG_GET_PROGRAM_DONE, + ERROR, PLAYLIST_SAVE_DETAILS, PLAYLIST_PARSE, PLAYLIST_PARSE_RESPONSE, PLAYLIST_UPDATE, + PLAYLIST_UPDATE_RESPONSE, } from './ipc-commands'; const fs = require('fs'); @@ -53,7 +54,7 @@ export class Api { }); }); } catch (err) { - event.sender.send('error', { + event.sender.send(ERROR, { message: err.response.statusText, status: err.response.status, }); @@ -301,6 +302,10 @@ export class Api { console.error('Error: Playlist details were not updated'); } + event.sender.send(PLAYLIST_UPDATE_RESPONSE, { + message: `Success! The playlist was successfully updated (${playlist.items.length} channels)`, + }); + // send all playlists back to the renderer process this.sendAllPlaylists(event); } @@ -323,8 +328,8 @@ export class Api { this.handlePlaylistRefresh(id, result.data, event) ); } catch (err) { - event.sender.send('error', { - message: err.response.statusText, + event.sender.send(ERROR, { + message: `${err.response.statusText}. Please check the entered playlist URL again.`, status: err.response.status, }); } @@ -341,26 +346,42 @@ export class Api { path: string, event: Electron.IpcMainEvent ): void { - const errorMessage = 'PLAYLIST UPDATE: something went wrong'; try { fs.readFile(path, 'utf-8', async (err, data) => { if (err) { - console.error(errorMessage, err); + this.handleFileNotFoundError(err, event); return; } this.handlePlaylistRefresh(id, data, event); }); } catch (err) { - console.error(errorMessage, err); - event.sender.send('error', { - message: 'Something went wrong', - status: 0, - }); + this.handleFileNotFoundError(err, event); } } - convertFileStringToPlaylist(m3uString: string): any[] { + /** + * Sends an error message to the renderer process + * @param error + * @param event + */ + handleFileNotFoundError( + error: { + errno: string; + code: string; + syscall: string; + path: string; + }, + event: Electron.IpcMainEvent + ) { + console.error(error); + event.sender.send(ERROR, { + message: `Sorry, playlist was not found (${error.path})`, + status: 'ENOENT', + }); + } + + convertFileStringToPlaylist(m3uString: string): any { return this.parsePlaylist(m3uString.split('\n')); } diff --git a/ipc-commands.ts b/ipc-commands.ts index 59b0c197d..f510a1c36 100644 --- a/ipc-commands.ts +++ b/ipc-commands.ts @@ -14,3 +14,4 @@ export const PLAYLIST_UPDATE_RESPONSE = 'PLAYLIST:UPDATE_RESPONSE'; // General export const SHOW_WHATS_NEW = 'SHOW_WHATS_NEW'; +export const ERROR = 'error'; diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index a15c1f9f6..953b8232e 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -6,9 +6,11 @@ import { MatSnackBar } from '@angular/material/snack-bar'; import { Playlist } from './playlist.interface'; import { ElectronService } from '../services/electron.service'; import { + ERROR, PLAYLIST_PARSE, PLAYLIST_PARSE_RESPONSE, PLAYLIST_UPDATE, + PLAYLIST_UPDATE_RESPONSE, } from './../../../ipc-commands'; /** Type to describe meta data of a playlist */ @@ -58,14 +60,19 @@ export class HomeComponent { }, }, { - id: 'error', + id: ERROR, execute: (response: { message: string; status: number }) => { this.isLoading = false; this.showNotification( - `Error: ${response.status} ${response.message}. Please check the entered playlist URL again.` + `Error: ${response.status} ${response.message}.` ); }, }, + { + id: PLAYLIST_UPDATE_RESPONSE, + execute: (response: { message: string }) => + this.showNotification(response.message), + }, ]; /**