Skip to content

Commit e3b4199

Browse files
Stricter error handling
1 parent a14c1bc commit e3b4199

File tree

9 files changed

+66
-33
lines changed

9 files changed

+66
-33
lines changed

ferrum-addon/addon.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export interface CountObject {
2323
/** Returns the deleted track lists, including folder children */
2424
export declare function delete_track_list(id: string): void
2525

26-
export declare function delete_tracks_with_item_ids(itemIds: Array<ItemId>): void
26+
export declare function delete_tracks_with_item_ids(itemIds: Array<ItemId>): FerrumStatus
27+
28+
export type FerrumStatus =
29+
| { type: 'Ok' }
30+
| { type: 'FileDeletionError', field0: string }
2731

2832
export interface Folder {
2933
id: TrackListID
@@ -133,7 +137,7 @@ export declare function save_view_options(viewOptions: ViewOptions): void
133137

134138
export declare function set_image(index: number, pathStr: string): void
135139

136-
export declare function set_image_data(index: number, bytes: Uint8Array): void
140+
export declare function set_image_data(index: number, bytes: ArrayBuffer): void
137141

138142
export interface Special {
139143
id: TrackListID

src-native/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ mod sort;
2020
mod tracks;
2121
mod view_options;
2222

23+
#[napi]
24+
pub enum FerrumStatus {
25+
Ok,
26+
FileDeletionError(String),
27+
}
28+
impl FerrumStatus {
29+
pub fn is_err(&self) -> bool {
30+
match self {
31+
FerrumStatus::Ok => false,
32+
FerrumStatus::FileDeletionError(_) => true,
33+
}
34+
}
35+
}
36+
2337
fn get_now_timestamp() -> i64 {
2438
let timestamp = match SystemTime::now().duration_since(UNIX_EPOCH) {
2539
Ok(n) => n.as_millis() as i64,

src-native/library.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ pub fn get_track_field_type(field: &str) -> Result<TrackField> {
149149

150150
#[napi(js_name = "get_genres")]
151151
#[allow(dead_code)]
152-
pub fn get_genres(env: Env) -> Result<Vec<String>> {
152+
pub fn get_genres(env: Env) -> Vec<String> {
153153
let data: &mut Data = get_data(&env);
154154
let genres = data.library.get_genres();
155-
Ok(genres.clone())
155+
genres.clone()
156156
}
157157

158158
#[napi(js_name = "get_artists")]
159159
#[allow(dead_code)]
160-
pub fn get_artists(env: Env) -> Result<Vec<String>> {
160+
pub fn get_artists(env: Env) -> Vec<String> {
161161
let data: &mut Data = get_data(&env);
162162
let genres = data.library.get_artists();
163-
Ok(genres.clone())
163+
genres.clone()
164164
}

src-native/library_types.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(non_snake_case)]
22

3-
use crate::get_now_timestamp;
43
use crate::playlists::{delete_file, remove_from_all_playlists};
4+
use crate::{FerrumStatus, get_now_timestamp};
55
use anyhow::{Context, Result, bail};
66
use linked_hash_map::{Entry, LinkedHashMap};
77
use nanoid::nanoid;
@@ -138,13 +138,20 @@ impl Library {
138138
track_id_map.push(id.clone());
139139
self.track_item_ids.insert(id, item_id);
140140
}
141-
pub fn delete_track_and_file(&mut self, id: &TrackID, tracks_dir: &PathBuf) -> Result<()> {
141+
pub fn delete_track_and_file(
142+
&mut self,
143+
id: &TrackID,
144+
tracks_dir: &PathBuf,
145+
) -> Result<FerrumStatus> {
142146
let file_path = {
143147
let track = self.get_track(id)?;
144148
tracks_dir.join(&track.file)
145149
};
146150
if !file_path.exists() {
147-
bail!("File does not exist: {}", file_path.to_string_lossy());
151+
return Ok(FerrumStatus::FileDeletionError(format!(
152+
"File does not exist: {}",
153+
file_path.to_string_lossy()
154+
)));
148155
}
149156

150157
remove_from_all_playlists(self, id);
@@ -154,8 +161,10 @@ impl Library {
154161
self.track_item_ids
155162
.remove(id)
156163
.expect("Track ID not found when deleting (2)");
157-
delete_file(&file_path)?;
158-
Ok(())
164+
Ok(match delete_file(&file_path) {
165+
Ok(_) => FerrumStatus::Ok,
166+
Err(err) => FerrumStatus::FileDeletionError(err.to_string()),
167+
})
159168
}
160169
pub fn generate_id(&self) -> String {
161170
let alphabet: [char; 32] = [

src-native/playlists.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::library_types::{
44
ItemId, Library, SpecialTrackListName, TRACK_ID_MAP, TrackID, TrackList,
55
get_track_ids_from_item_ids, new_item_ids_from_track_ids,
66
};
7-
use crate::str_to_option;
7+
use crate::{FerrumStatus, str_to_option};
88
use anyhow::{Context, Result, bail};
99
use linked_hash_map::LinkedHashMap;
1010
use napi::{Env, Unknown};
@@ -25,10 +25,9 @@ pub struct TrackListDetails {
2525

2626
#[napi(js_name = "get_track_lists_details")]
2727
#[allow(dead_code)]
28-
pub fn get_track_lists_details(env: Env) -> Result<HashMap<String, TrackListDetails>> {
28+
pub fn get_track_lists_details(env: Env) -> HashMap<String, TrackListDetails> {
2929
let data: &Data = get_data(&env);
30-
Ok(data
31-
.library
30+
data.library
3231
.trackLists
3332
.iter()
3433
.map(|(id, tracklist)| {
@@ -54,7 +53,7 @@ pub fn get_track_lists_details(env: Env) -> Result<HashMap<String, TrackListDeta
5453
},
5554
);
5655
})
57-
.collect())
56+
.collect()
5857
}
5958

6059
#[napi(js_name = "get_track_list", ts_return_type = "TrackList")]
@@ -217,14 +216,17 @@ pub fn delete_file(path: &PathBuf) -> Result<()> {
217216

218217
#[napi(js_name = "delete_tracks_with_item_ids")]
219218
#[allow(dead_code)]
220-
pub fn delete_tracks_with_item_ids(item_ids: Vec<ItemId>, env: Env) -> Result<()> {
219+
pub fn delete_tracks_with_item_ids(item_ids: Vec<ItemId>, env: Env) -> Result<FerrumStatus> {
221220
let data: &mut Data = get_data(&env);
222221
let library = &mut data.library;
223222
let track_ids = get_track_ids_from_item_ids(&item_ids);
224223
for track_id in &track_ids {
225-
library.delete_track_and_file(track_id, &data.paths.tracks_dir)?;
224+
let status = library.delete_track_and_file(track_id, &data.paths.tracks_dir)?;
225+
if status.is_err() {
226+
return Ok(status);
227+
}
226228
}
227-
return Ok(());
229+
return Ok(FerrumStatus::Ok);
228230
}
229231

230232
#[napi(js_name = "new_playlist")]

src-native/tracks/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,12 @@ pub fn set_image(index: u32, path_str: String, env: Env) -> Result<()> {
218218
#[napi(js_name = "set_image_data")]
219219
#[allow(dead_code)]
220220
pub fn set_image_data(index: u32, bytes: ArrayBuffer, env: Env) -> Result<()> {
221-
let bytes: Vec<u8> = bytes.to_vec();
222221
let data: &mut Data = get_data(&env);
223222
let tag = match &mut data.current_tag {
224223
Some(tag) => tag,
225224
None => bail!("No tag loaded"),
226225
};
227-
tag.set_image(index as usize, bytes)?;
226+
tag.set_image(index as usize, bytes.to_vec())?;
228227
Ok(())
229228
}
230229

src/lib/data.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
import { queue } from './queue'
1414
import { current_playlist_id } from '@/components/TrackList.svelte'
1515
import { navigate } from './router'
16-
import { call, get_error_message, strict_call } from './error'
16+
import { call, error_popup, get_error_message, strict_call } from './error'
1717

1818
export const is_dev = window.is_dev
1919
export const local_data_path = window.local_data_path
@@ -23,21 +23,21 @@ export const is_windws = window.is_windows
2323
const inner_addon = window.addon
2424
export const ItunesImport = inner_addon.ItunesImport
2525

26-
call((addon) => addon.load_data(is_dev, local_data_path, library_path))
26+
strict_call((addon) => addon.load_data(is_dev, local_data_path, library_path))
2727

28-
export const paths = call((addon) => addon.get_paths())
28+
export const paths = strict_call((addon) => addon.get_paths())
2929
export function join_paths(...args: string[]) {
3030
return args.join(paths.pathSeparator)
3131
}
3232

3333
export const track_lists_details_map = (() => {
34-
const initial = call((addon) => addon.get_track_lists_details())
34+
const initial = strict_call((addon) => addon.get_track_lists_details())
3535

3636
const { subscribe, set } = writable(initial)
3737
return {
3838
subscribe,
3939
refresh() {
40-
set(call((addon) => addon.get_track_lists_details()))
40+
set(strict_call((addon) => addon.get_track_lists_details()))
4141
},
4242
}
4343
})()
@@ -47,7 +47,9 @@ export async function add_tracks_to_playlist(
4747
check_duplicates = true,
4848
) {
4949
if (check_duplicates) {
50-
const filtered_ids = call((addon) => addon.playlist_filter_duplicates(playlist_id, track_ids))
50+
const filtered_ids = strict_call((addon) =>
51+
addon.playlist_filter_duplicates(playlist_id, track_ids),
52+
)
5153
const duplicates = track_ids.length - filtered_ids.length
5254
if (duplicates > 0) {
5355
const result = await ipc_renderer.invoke('showMessageBox', false, {
@@ -68,18 +70,21 @@ export async function add_tracks_to_playlist(
6870
}
6971
}
7072
if (track_ids.length >= 1) {
71-
call((addon) => addon.add_tracks_to_playlist(playlist_id, track_ids))
73+
strict_call((addon) => addon.add_tracks_to_playlist(playlist_id, track_ids))
7274
tracklist_updated.emit()
7375
save()
7476
}
7577
}
7678
export function remove_from_playlist(playlist_id: TrackListID, item_ids: ItemId[]) {
77-
call((addon) => addon.remove_from_playlist(playlist_id, item_ids))
79+
strict_call((addon) => addon.remove_from_playlist(playlist_id, item_ids))
7880
tracklist_updated.emit()
7981
save()
8082
}
8183
export function delete_tracks_with_item_ids(item_ids: ItemId[]) {
82-
call((addon) => addon.delete_tracks_with_item_ids(item_ids))
84+
const result = strict_call((addon) => addon.delete_tracks_with_item_ids(item_ids))
85+
if (result.type === 'FileDeletionError') {
86+
error_popup(result.field0)
87+
}
8388
tracklist_updated.emit()
8489
queue.removeDeleted()
8590
save()

src/lib/error.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export function get_error_message(err: unknown): string {
88
} else if (obj.code) {
99
return 'Code: ' + String(obj.message)
1010
}
11+
} else if (typeof err === 'string') {
12+
return err
1113
}
1214
return 'No reason or code provided'
1315
}
@@ -20,7 +22,7 @@ function get_error_stack(err: unknown): string {
2022
}
2123
return ''
2224
}
23-
function error_popup(err: unknown, crash = false) {
25+
export function error_popup(err: unknown, crash = false) {
2426
ipc_renderer.invoke(
2527
'showMessageBox',
2628
false,

src/lib/selection.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,9 @@ class Selection<T> {
196196
} else if (this.items.size === 0) {
197197
this.add_index_unchecked(0)
198198
} else if (this.last_added !== null) {
199-
console.log('go_forward', { ...this })
200199
const next_index = this.last_added.index + 1
201200
this.clear()
202201
this.add_index_unchecked(Math.min(next_index, this.all.length - 1))
203-
console.log('.', { ...this })
204202
}
205203
}
206204
/** Expand or shrink selection backwards (shift+up) */

0 commit comments

Comments
 (0)