Skip to content

Commit dcbd879

Browse files
committed
fix: improve error handling in song loading process
- Refactored song loading logic in SongEditForm and EditSongProvider to use async/await for better readability and error management. - Added comprehensive error handling to provide user feedback for various failure scenarios during song loading. - Ensured consistent handling of song data and instrument settings upon loading.
1 parent fd591c3 commit dcbd879

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

apps/frontend/src/modules/song-edit/components/client/SongEditForm.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,17 @@ export const SongEditForm = ({
2626
) as useEditSongProviderType;
2727

2828
useEffect(() => {
29-
loadSong(songId, username, songData);
30-
setSongId(songId);
29+
const loadSongData = async () => {
30+
try {
31+
await loadSong(songId, username, songData);
32+
setSongId(songId);
33+
} catch (error) {
34+
// Error is already handled in loadSong with setSendError and toaster
35+
console.error('Failed to load song:', error);
36+
}
37+
};
38+
39+
loadSongData();
3140
}, [loadSong, setSongId, songData, songId, username]);
3241
// TODO: The username is injected into the form differently in SongUploadForm (defaultAuthorName) and SongEditForm (username). This should be consistent
3342

apps/frontend/src/modules/song-edit/components/client/context/EditSong.context.tsx

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -236,25 +236,51 @@ export const EditSongProvider = ({
236236
// fetch song
237237
const token = getTokenLocal();
238238

239-
const songFile = (
240-
await axiosInstance.get(`/song/${id}/download`, {
241-
params: {
242-
src: 'edit',
243-
},
244-
responseType: 'arraybuffer',
245-
headers: { authorization: `Bearer ${token}` },
246-
})
247-
).data as ArrayBuffer;
248-
249-
// convert to song
250-
const song = await parseSongFromBuffer(songFile);
251-
252-
// set instruments array
253-
const songInstruments = songData.customInstruments;
254-
setInstrumentSounds(songInstruments);
255-
formMethods.setValue('customInstruments', songInstruments);
256-
257-
setSong(song as unknown as SongFileType); // TODO: Investigate this weird type error
239+
try {
240+
const songFile = (
241+
await axiosInstance.get(`/song/${id}/download`, {
242+
params: {
243+
src: 'edit',
244+
},
245+
responseType: 'arraybuffer',
246+
headers: { authorization: `Bearer ${token}` },
247+
})
248+
).data as ArrayBuffer;
249+
250+
// convert to song
251+
const song = await parseSongFromBuffer(songFile);
252+
253+
// set instruments array
254+
const songInstruments = songData.customInstruments;
255+
setInstrumentSounds(songInstruments);
256+
formMethods.setValue('customInstruments', songInstruments);
257+
258+
setSong(song as unknown as SongFileType); // TODO: Investigate this weird type error
259+
} catch (error: any) {
260+
console.error('Error loading song', error);
261+
262+
let errorMessage = 'An unknown error occurred while loading the song!';
263+
264+
if (error.response) {
265+
// Server responded with an error status
266+
errorMessage =
267+
error.response.data.message ||
268+
Object.values(error.response.data.error || {})[0] ||
269+
`Failed to load song: ${error.response.status}`;
270+
} else if (error.request) {
271+
console.error('Error loading song', error);
272+
// Request was made but no response received (network error)
273+
errorMessage =
274+
'Network error: Unable to connect to the server. Please check your internet connection and try again.';
275+
} else {
276+
// Something else happened
277+
errorMessage = error.message || errorMessage;
278+
}
279+
280+
setSendError(errorMessage);
281+
toaster.error(errorMessage);
282+
throw error; // Re-throw to allow caller to handle if needed
283+
}
258284
},
259285
[formMethods, setSong],
260286
);

0 commit comments

Comments
 (0)