Merge pull request #6 from forkbomb9/error-improvements
update error messages
This commit is contained in:
commit
ce6b94113d
|
@ -7,6 +7,7 @@ interface userData {
|
|||
license_token: string;
|
||||
can_stream_lossless: boolean;
|
||||
can_stream_hq: boolean;
|
||||
country: string;
|
||||
}
|
||||
|
||||
class WrongLicense extends Error {
|
||||
|
@ -17,6 +18,14 @@ class WrongLicense extends Error {
|
|||
}
|
||||
}
|
||||
|
||||
class GeoBlocked extends Error {
|
||||
constructor(country: string) {
|
||||
super();
|
||||
this.name = 'GeoBlocked';
|
||||
this.message = `This track is not available in your country (${country})`;
|
||||
}
|
||||
}
|
||||
|
||||
let user_data: userData | null = null;
|
||||
|
||||
const dzAuthenticate = async (): Promise<userData> => {
|
||||
|
@ -31,6 +40,7 @@ const dzAuthenticate = async (): Promise<userData> => {
|
|||
license_token: data.results.USER.OPTIONS.license_token,
|
||||
can_stream_lossless: data.results.USER.OPTIONS.web_lossless || data.results.USER.OPTIONS.mobile_loseless,
|
||||
can_stream_hq: data.results.USER.OPTIONS.web_hq || data.results.USER.OPTIONS.mobile_hq,
|
||||
country: data.results.COUNTRY,
|
||||
};
|
||||
return user_data;
|
||||
};
|
||||
|
@ -54,6 +64,9 @@ const getTrackUrlFromServer = async (track_token: string, format: string): Promi
|
|||
|
||||
if (data.data.length > 0) {
|
||||
if (data.data[0].errors) {
|
||||
if (data.data[0].errors[0].code === 2002) {
|
||||
throw new GeoBlocked(user.country);
|
||||
}
|
||||
throw new Error(Object.entries(data.data[0].errors[0]).join(', '));
|
||||
}
|
||||
return data.data[0].media.length > 0 ? data.data[0].media[0].sources[0].url : null;
|
||||
|
@ -66,7 +79,8 @@ const getTrackUrlFromServer = async (track_token: string, format: string): Promi
|
|||
* @param quality 1 = 128kbps, 3 = 320kbps and 9 = flac (around 1411kbps)
|
||||
*/
|
||||
export const getTrackDownloadUrl = async (track: trackType, quality: number): Promise<{trackUrl: string, isEncrypted: boolean, fileSize: number} | null> => {
|
||||
let wrongLicense = false;
|
||||
let wrongLicense: WrongLicense | null = null;
|
||||
let geoBlocked: GeoBlocked | null = null;
|
||||
let formatName: string;
|
||||
switch (quality) {
|
||||
case 9:
|
||||
|
@ -97,7 +111,9 @@ export const getTrackDownloadUrl = async (track: trackType, quality: number): Pr
|
|||
}
|
||||
} catch (err) {
|
||||
if (err instanceof WrongLicense) {
|
||||
wrongLicense = true;
|
||||
wrongLicense = err;
|
||||
} else if (err instanceof GeoBlocked) {
|
||||
geoBlocked = err;
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
|
@ -114,7 +130,12 @@ export const getTrackDownloadUrl = async (track: trackType, quality: number): Pr
|
|||
fileSize: fileSize,
|
||||
};
|
||||
}
|
||||
if (wrongLicense) throw new Error(`Your account can't stream ${formatName} tracks`);
|
||||
if (wrongLicense) {
|
||||
throw wrongLicense;
|
||||
}
|
||||
if (geoBlocked) {
|
||||
throw geoBlocked;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue