refactor: improve error handling for update feature

This commit is contained in:
4gray committed 2021-05-14 17:48:32 +02:00
1 parent 186d679d90
commit d0f41c2c9c
3 files changed
+43 -14

No files matched your search

+33 -12
View File
@@ -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'));
}
+1
View File
@@ -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';
+9 -2
View File
@@ -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),
},
];
/**