Handle search failures better
This commit is contained in:
parent
01b4209ba1
commit
740b125a45
3 changed files with 20 additions and 9 deletions
|
@ -19,11 +19,10 @@ module.exports.lookupId = function(id, type) {
|
||||||
request.get(apiRoot + path, function(res) {
|
request.get(apiRoot + path, function(res) {
|
||||||
var data = JSON.parse(res.text);
|
var data = JSON.parse(res.text);
|
||||||
|
|
||||||
if (!data.results || !data.results[0] || !data.results[0].collectionId) {
|
if (!data.results || data.resultCount == 0 || !data.results[0].collectionId) {
|
||||||
var error = new Error("Not Found");
|
var error = new Error("Not Found");
|
||||||
error.status = 404;
|
error.status = 404;
|
||||||
deferred.reject(error);
|
deferred.reject(error);
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
var result = data.results[0];
|
var result = data.results[0];
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach
|
||||||
|
|
||||||
module.exports = function(req, res) {
|
module.exports = function(req, res) {
|
||||||
var url = parse(req.body.url);
|
var url = parse(req.body.url);
|
||||||
|
var searching = false;
|
||||||
|
|
||||||
if (!url.host) {
|
if (!url.host) {
|
||||||
req.flash('search-error', 'Paste a music link above to find and share the matches');
|
req.flash('search-error', 'Paste a music link above to find and share the matches');
|
||||||
|
@ -24,17 +25,29 @@ module.exports = function(req, res) {
|
||||||
for (var id in services) {
|
for (var id in services) {
|
||||||
var matched = services[id].match(req.body.url);
|
var matched = services[id].match(req.body.url);
|
||||||
if (matched) {
|
if (matched) {
|
||||||
services[id].parseUrl(req.body.url).then(function(result) {
|
searching = true;
|
||||||
|
Q.timeout(services[id].parseUrl(req.body.url), 5000).then(function(result) {
|
||||||
if (!result.id) {
|
if (!result.id) {
|
||||||
req.flash('search-error', 'No match found for this link');
|
req.flash('search-error', 'No match found for this link');
|
||||||
res.redirect('/');
|
res.redirect('/');
|
||||||
}
|
}
|
||||||
res.redirect("/" + id + "/" + result.type + "/" + result.id);
|
res.redirect("/" + id + "/" + result.type + "/" + result.id);
|
||||||
})
|
}, function(error) {
|
||||||
return;
|
if (error.code == "ETIMEDOUT") {
|
||||||
|
error = new Error("Error talking to music service");
|
||||||
|
error.status = "502";
|
||||||
|
} else if (!error.status) {
|
||||||
|
error = new Error("An unexpected error happenend");
|
||||||
|
error.status = 500;
|
||||||
|
}
|
||||||
|
next(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!searching) {
|
||||||
req.flash('search-error', 'No match found for this link');
|
req.flash('search-error', 'No match found for this link');
|
||||||
res.redirect('/');
|
res.redirect('/');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -61,7 +61,6 @@ module.exports = function(req, res, next) {
|
||||||
res.render(type, {page: type, items: items});
|
res.render(type, {page: type, items: items});
|
||||||
});
|
});
|
||||||
}, function(error) {
|
}, function(error) {
|
||||||
console.log(error)
|
|
||||||
if (error.code == "ETIMEDOUT") {
|
if (error.code == "ETIMEDOUT") {
|
||||||
error = new Error("Error talking to music service");
|
error = new Error("Error talking to music service");
|
||||||
error.status = "502";
|
error.status = "502";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue