Handle broken links better

This commit is contained in:
Jonathan Cremin 2019-03-11 23:26:54 +00:00
parent b5f99b217a
commit 3abe8f1a79
4 changed files with 93 additions and 70 deletions

View file

@ -39,39 +39,44 @@ export async function lookupId(possibleId, type, countrycode) {
if (cc) {
path = `/${cc}${path}`;
}
const response = await request.get(apiRoot + path);
let result = JSON.parse(response.text);
if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) {
try {
const response = await request.get(apiRoot + path);
let result = JSON.parse(response.text);
if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) {
throw new Error();
} else {
result = result.results[0];
const item = {
service: 'itunes',
type,
id: cc + id,
name: result.trackName ? result.trackName : result.collectionName,
streamUrl: null,
purchaseUrl: result.collectionViewUrl,
artwork: {
small: `${result.artworkUrl100.replace('100x100', '200x200').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`,
large: `${result.artworkUrl100.replace('100x100', '600x600').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`,
},
artist: {
name: result.artistName,
},
};
if (type === 'track') {
item.album = {
name: result.collectionName,
};
}
return item;
}
} catch(e) {
const error = new Error('Not Found');
error.status = 404;
throw error;
} else {
result = result.results[0];
const item = {
service: 'itunes',
type,
id: cc + id,
name: result.trackName ? result.trackName : result.collectionName,
streamUrl: null,
purchaseUrl: result.collectionViewUrl,
artwork: {
small: `${result.artworkUrl100.replace('100x100', '200x200').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`,
large: `${result.artworkUrl100.replace('100x100', '600x600').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`,
},
artist: {
name: result.artistName,
},
};
if (type === 'track') {
item.album = {
name: result.collectionName,
};
}
return item;
return Promise.reject(error);
}
}