refactor: Better error message on geoblocked track URLs

This commit is contained in:
Namkhai B 2021-08-25 12:28:45 -05:00
parent 001114e595
commit d069afcd6c
No known key found for this signature in database
GPG Key ID: 9DC021F538318528

View File

@ -7,6 +7,7 @@ interface userData {
license_token: string; license_token: string;
can_stream_lossless: boolean; can_stream_lossless: boolean;
can_stream_hq: boolean; can_stream_hq: boolean;
country: string;
} }
class WrongLicense extends Error { 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; let user_data: userData | null = null;
const dzAuthenticate = async (): Promise<userData> => { const dzAuthenticate = async (): Promise<userData> => {
@ -31,6 +40,7 @@ const dzAuthenticate = async (): Promise<userData> => {
license_token: data.results.USER.OPTIONS.license_token, 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_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, can_stream_hq: data.results.USER.OPTIONS.web_hq || data.results.USER.OPTIONS.mobile_hq,
country: data.results.COUNTRY,
}; };
return user_data; return user_data;
}; };
@ -54,6 +64,7 @@ const getTrackUrlFromServer = async (track_token: string, format: string): Promi
if (data.data.length > 0) { if (data.data.length > 0) {
if (data.data[0].errors) { 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(', ')); 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; return data.data[0].media.length > 0 ? data.data[0].media[0].sources[0].url : null;
@ -67,6 +78,7 @@ const getTrackUrlFromServer = async (track_token: string, format: string): Promi
*/ */
export const getTrackDownloadUrl = async (track: trackType, quality: number): Promise<{trackUrl: string, isEncrypted: boolean, fileSize: number} | null> => { export const getTrackDownloadUrl = async (track: trackType, quality: number): Promise<{trackUrl: string, isEncrypted: boolean, fileSize: number} | null> => {
let wrongLicense = false; let wrongLicense = false;
let geoBlocked: string | null = null;
let formatName: string; let formatName: string;
switch (quality) { switch (quality) {
case 9: case 9:
@ -98,6 +110,8 @@ export const getTrackDownloadUrl = async (track: trackType, quality: number): Pr
} catch (err) { } catch (err) {
if (err instanceof WrongLicense) { if (err instanceof WrongLicense) {
wrongLicense = true; wrongLicense = true;
} else if (err instanceof GeoBlocked) {
geoBlocked = err.message;
} else { } else {
throw err; throw err;
} }
@ -115,6 +129,7 @@ export const getTrackDownloadUrl = async (track: trackType, quality: number): Pr
}; };
} }
if (wrongLicense) throw new Error(`Your account can't stream ${formatName} tracks`); if (wrongLicense) throw new Error(`Your account can't stream ${formatName} tracks`);
if (geoBlocked) throw new Error(geoBlocked);
return null; return null;
}; };