refactor: improve error handling

This commit is contained in:
4gray committed 2020-12-26 23:35:01 +01:00
1 parent 68ccabbcb1
commit e0708189da
2 files changed
+40 -19

No files matched your search

+20 -13
View File
@@ -16,20 +16,27 @@ const db = new Nedb<Playlist>({
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) => {
+20 -6
View File
@@ -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,
});
}
}