Handle broken links better
This commit is contained in:
parent
b5f99b217a
commit
3abe8f1a79
4 changed files with 93 additions and 70 deletions
|
@ -91,7 +91,9 @@ export async function lookupId(id, type) {
|
|||
},
|
||||
});
|
||||
}
|
||||
return Promise.reject(new Error());
|
||||
const error = new Error('Not Found');
|
||||
error.status = 404;
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
export async function search(data, original = {}) {
|
||||
|
|
|
@ -23,6 +23,7 @@ let ready = pm.initAsync(creds).catch(function(err) {
|
|||
|
||||
export async function lookupId(id, type) {
|
||||
await ready;
|
||||
try {
|
||||
if (type === 'album') {
|
||||
const album = await pm.getAlbumAsync(id, false);
|
||||
return {
|
||||
|
@ -61,7 +62,14 @@ export async function lookupId(id, type) {
|
|||
},
|
||||
};
|
||||
}
|
||||
return { service: 'google' };
|
||||
} catch(e) {
|
||||
const error = new Error('Not Found');
|
||||
error.status = 404;
|
||||
return Promise.reject(error);
|
||||
}
|
||||
const error = new Error('Not Found');
|
||||
error.status = 404;
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
function exactMatch(needle, haystack, type) {
|
||||
|
|
|
@ -39,13 +39,13 @@ export async function lookupId(possibleId, type, countrycode) {
|
|||
if (cc) {
|
||||
path = `/${cc}${path}`;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await request.get(apiRoot + path);
|
||||
let result = JSON.parse(response.text);
|
||||
|
||||
if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) {
|
||||
const error = new Error('Not Found');
|
||||
error.status = 404;
|
||||
throw error;
|
||||
throw new Error();
|
||||
} else {
|
||||
result = result.results[0];
|
||||
|
||||
|
@ -73,6 +73,11 @@ export async function lookupId(possibleId, type, countrycode) {
|
|||
|
||||
return item;
|
||||
}
|
||||
} catch(e) {
|
||||
const error = new Error('Not Found');
|
||||
error.status = 404;
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function search(data) {
|
||||
|
|
|
@ -44,6 +44,12 @@ export async function lookupId(id, type) {
|
|||
|
||||
data = data.body[`${type}s`][0];
|
||||
|
||||
if(!data) {
|
||||
const error = new Error('Not Found');
|
||||
error.status = 404;
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
const artist = data.artists[0];
|
||||
|
||||
if (type === 'album') {
|
||||
|
@ -82,7 +88,9 @@ export async function lookupId(id, type) {
|
|||
},
|
||||
};
|
||||
}
|
||||
return { service: 'spotify' };
|
||||
const error = new Error('Not Found');
|
||||
error.status = 404;
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
export async function search(data, original = {}) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue