Skip to content

Commit a58d9db

Browse files
committed
feat(error): adds error handling to views data fetch
1 parent f68f9db commit a58d9db

8 files changed

+41
-5
lines changed

src/services/notification.service.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
import type { useMessage, useNotification } from 'naive-ui';
1+
import type { NotificationOptions, useMessage, useNotification } from 'naive-ui';
2+
import type { Mutable } from '~/utils/typescript.utils';
3+
4+
type NotificationApi = ReturnType<typeof useNotification>;
5+
type MessageApi = ReturnType<typeof useMessage>;
26

37
export class NotificationService {
4-
static notification: ReturnType<typeof useNotification>;
5-
static message: ReturnType<typeof useMessage>;
8+
static notification: NotificationApi;
9+
static message: MessageApi;
10+
11+
static error(title: string, error: Error | Response | unknown, duration = 5000) {
12+
const option: Mutable<NotificationOptions> = {
13+
title,
14+
duration,
15+
};
16+
17+
if (error instanceof Response) {
18+
option.description = error.status?.toString();
19+
option.content = error.statusText;
20+
} else if (error instanceof Error) {
21+
option.description = error.name;
22+
option.content = error.message;
23+
}
24+
25+
this.notification.error(option);
26+
}
627
}

src/stores/data/activity.store.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ref, watch } from 'vue';
44

55
import type { TraktSyncActivities } from '~/models/trakt/trakt-sync.model';
66

7+
import { NotificationService } from '~/services/notification.service';
78
import { TraktService } from '~/services/trakt.service';
89
import { storage } from '~/utils/browser/browser-storage.utils';
910
import { compareDateObject, toDateObject } from '~/utils/date.utils';
@@ -31,6 +32,7 @@ export const useActivityStore = defineStore('data.activity', () => {
3132
await saveState();
3233
} catch (error) {
3334
console.error('Failed to fetch activity', error);
35+
NotificationService.error('Failed to fetch activity', error);
3436
throw error;
3537
} finally {
3638
loading.value = false;

src/stores/data/calendar.store.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { TraktCalendarMovie, TraktCalendarShow } from '~/models/trakt/trakt
55

66
import { type ListScrollItem, type ListScrollItemTag, ListScrollItemType } from '~/models/list-scroll.model';
77

8+
import { NotificationService } from '~/services/notification.service';
89
import { TraktService } from '~/services/trakt.service';
910
import { storage } from '~/utils/browser/browser-storage.utils';
1011
import { DateUtils } from '~/utils/date.utils';
@@ -181,7 +182,8 @@ export const useCalendarStore = defineStore('data.calendar', () => {
181182
calendar.value = [...calendar.value.filter(c => c.type !== ListScrollItemType.loading), ...spacedData];
182183
}
183184
} catch (e) {
184-
console.error('Failed to fetch history');
185+
console.error('Failed to fetch calendar');
186+
NotificationService.error('Failed to fetch calendar', e);
185187
calendar.value = calendar.value.filter(c => c.type !== ListScrollItemType.loading);
186188
throw e;
187189
} finally {

src/stores/data/history.store.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ref, watch } from 'vue';
44
import type { TraktClientPagination } from '~/models/trakt/trakt-client.model';
55
import type { TraktHistory } from '~/models/trakt/trakt-history.model';
66

7+
import { NotificationService } from '~/services/notification.service';
78
import { TraktService } from '~/services/trakt.service';
89
import { storage } from '~/utils/browser/browser-storage.utils';
910
import { debounceLoading, useBelowThreshold, useLoadingPlaceholder, useSearchFilter } from '~/utils/store.utils';
@@ -81,6 +82,7 @@ export const useHistoryStore = defineStore('data.history', () => {
8182
history.value = page ? [...history.value.filter(h => h.id >= 0), ...response.data] : response.data;
8283
} catch (e) {
8384
console.error('Failed to fetch history');
85+
NotificationService.error('Failed to fetch history', e);
8486
history.value = history.value.filter(h => h.id >= 0);
8587
throw e;
8688
} finally {

src/stores/data/list.store.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { TraktListItem } from '~/models/trakt/trakt-list.model';
1010
import type { TraktWatchlist } from '~/models/trakt/trakt-watchlist.model';
1111

1212
import { ListScrollItemType } from '~/models/list-scroll.model';
13+
import { NotificationService } from '~/services/notification.service';
1314
import { TraktService } from '~/services/trakt.service';
1415
import { useUserSettingsStoreRefs } from '~/stores/settings/user.store';
1516
import { storage } from '~/utils/browser/browser-storage.utils';
@@ -110,6 +111,7 @@ export const useListsStore = defineStore('data.lists', () => {
110111
}
111112
} catch (e) {
112113
console.error('Failed to fetch lists');
114+
NotificationService.error('Failed to fetch lists', e);
113115
throw e;
114116
} finally {
115117
loading.value = false;
@@ -207,6 +209,7 @@ export const useListStore = defineStore('data.list', () => {
207209
listItems.value = page ? [...listItems.value.filter(l => l.type !== ListScrollItemType.loading), ...newData] : newData;
208210
} catch (e) {
209211
console.error('Failed to fetch list');
212+
NotificationService.error(`Failed to fetch list '${list}'`, e);
210213
listItems.value = listItems.value.filter(l => l.type !== ListScrollItemType.loading);
211214
throw e;
212215
} finally {

src/stores/data/progress.store.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { TraktShow } from '~/models/trakt/trakt-show.model';
77
import { getContent, getTags, getTitle } from '~/components/common/list/use-list-scroll';
88
import { type ListScrollItem, type ListScrollSourceItem } from '~/models/list-scroll.model';
99
import { type ProgressItem } from '~/models/progress.model';
10+
import { NotificationService } from '~/services/notification.service';
1011
import { TraktService } from '~/services/trakt.service';
1112
import { debounceLoading, useLoadingPlaceholder } from '~/utils/store.utils';
1213

@@ -96,6 +97,7 @@ export const useProgressStore = defineStore('data.progress', () => {
9697
}
9798

9899
console.error(error);
100+
NotificationService.error('Failed to fetch progress', error);
99101
throw error;
100102
} finally {
101103
clearTimeout(timeout);

src/stores/data/search.store.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { TraktSearchResult, TraktSearchType } from '~/models/trakt/trakt-se
77

88
import { type ListScrollItem, ListScrollItemType } from '~/models/list-scroll.model';
99

10+
import { NotificationService } from '~/services/notification.service';
1011
import { TraktService } from '~/services/trakt.service';
1112
import { storage } from '~/utils/browser/browser-storage.utils';
1213
import { debounceLoading, useLoadingPlaceholder } from '~/utils/store.utils';
@@ -91,7 +92,8 @@ export const useSearchStore = defineStore('data.search', () => {
9192
pagination.value = response.pagination;
9293
searchResults.value = page ? [...searchResults.value.filter(s => s.type !== ListScrollItemType.loading), ...data] : data;
9394
} catch (e) {
94-
console.error('Failed to fetch history');
95+
console.error('Failed to fetch search query');
96+
NotificationService.error('Failed to fetch search query', e);
9597
searchResults.value = searchResults.value.filter(s => s.type !== ListScrollItemType.loading);
9698
throw e;
9799
} finally {

src/utils/typescript.utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ export type ArrayElement<ArrayType extends readonly unknown[] | undefined> = Arr
2222
export type RecursiveType<T, R> = {
2323
[K in keyof T]: T[K] extends object ? RecursiveType<T[K], R> : R;
2424
};
25+
26+
export type Mutable<T> = { -readonly [P in keyof T]: T[P] };

0 commit comments

Comments
 (0)