Skip to content

Commit 0662bb0

Browse files
committed
fix: add proper error message when session expired
1 parent 75e2605 commit 0662bb0

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

web/src/lib/axios/token.utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ export const getTokenLocal = (): string | never => {
88
?.split('=')[1];
99

1010
// TODO: should be changed to a redirect to the login page?
11-
if (!token) throw new Error('Failed to get token');
11+
if (!token) throw new InvalidTokenError('Token not found');
1212

1313
return token;
1414
};
15+
16+
export class InvalidTokenError extends Error {
17+
constructor(msg: string) {
18+
super(msg);
19+
20+
// Set the prototype explicitly.
21+
Object.setPrototypeOf(this, InvalidTokenError.prototype);
22+
}
23+
}

web/src/modules/song-upload/components/client/context/UploadSong.context.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import {
1515
import { toast } from 'react-hot-toast';
1616

1717
import axiosInstance from '@web/src/lib/axios';
18-
import { getTokenLocal } from '@web/src/lib/axios/token.utils';
18+
import {
19+
InvalidTokenError,
20+
getTokenLocal,
21+
} from '@web/src/lib/axios/token.utils';
1922

2023
import {
2124
UploadSongForm,
@@ -146,9 +149,15 @@ export const UploadSongProvider = ({
146149
} catch (e) {
147150
console.error('Error submitting song', e);
148151

149-
setSendError(
150-
'An unknown error occurred while submitting the song! Please contact us.',
151-
);
152+
if (e instanceof InvalidTokenError) {
153+
setSendError(
154+
'Your session has expired! Please sign in again and try to submit the song.',
155+
);
156+
} else {
157+
setSendError(
158+
'An unknown error occurred while submitting the song! Please contact us.',
159+
);
160+
}
152161
} finally {
153162
setIsSubmitting(false);
154163
}

0 commit comments

Comments
 (0)