Improve error handling

This commit is contained in:
Jonathan Cremin 2017-11-11 19:18:42 +00:00
parent ca1fbf9e66
commit 82fa61f5d5

View file

@ -14,43 +14,47 @@ const queue = kue.createQueue({
export default function* () { export default function* () {
const url = parse(this.request.body.url); const url = parse(this.request.body.url);
debug(`URL ${url}`); debug(`URL ${url.href}`);
this.assert(url.host, 400, { error: { message: 'You need to submit a url.' } }); this.assert(url.host, 400, { error: { message: 'You need to submit a url.' } });
const music = yield lookup(this.request.body.url); try {
const music = yield lookup(this.request.body.url);
this.assert(music, 400, { error: { message: 'No supported music found at that link :(' } }); this.assert(music, 400, { error: { message: 'No supported music found at that link :(' } });
let share = yield find(music); let share = yield find(music);
if (!share) { if (!share) {
share = yield create(music); share = yield create(music);
for (const service of services) { services.forEach((service) => {
if (service.id === share.service) { if (service.id !== share.service) {
continue; // eslint-disable-line no-continue const job = queue.create('search', { share, service }).save((err) => {
} debug(err || job.id);
const job = queue.create('search', { share: share, service: service }).save((err) => { });
if (!err) console.log(job.id); }
}); });
} }
share = share.toJSON();
share.id = share.externalId;
const unmatched = services.filter(service =>
!share.matches.find(match => match.service === service.id));
share.matches = share.matches.concat(unmatched.map((service) => {
return {
service: service.id,
matching: true,
};
}));
share.matches = share.matches.sort(a => !!a.externalId);
this.body = share;
} catch (e) {
this.throw(400, { error: { message: 'Unexpected error looking up music. Please try again later.' } });
throw e;
} }
share = share.toJSON();
share.id = share.externalId;
const unmatched = services.filter(service =>
!share.matches.find(match => match.service === service.id));
share.matches = share.matches.concat(unmatched.map((service) => {
return {
service: service.id,
matching: true,
};
}));
share.matches = share.matches.sort(a => !!a.externalId);
this.body = share;
} }