Handle errors in Amazon API

This commit is contained in:
Jonathan Cremin 2018-04-08 17:22:19 +01:00
parent b0fcf55071
commit 3428986268
3 changed files with 70 additions and 58 deletions

View file

@ -20,7 +20,7 @@ const query = {
{ model: models.match }, { model: models.match },
], ],
order: [ order: [
['updatedAt', 'DESC'], ['createdAt', 'DESC'],
], ],
}; };

View file

@ -1,8 +1,12 @@
import { parse } from 'url'; import { parse } from 'url';
import { inspect } from 'util'; import { inspect } from 'util';
import amazon from 'amazon-product-api'; import amazon from 'amazon-product-api';
import debuglog from 'debug';
import urlMatch from './url'; import urlMatch from './url';
const debug = debuglog('combine.fm:youtube');
const client = amazon.createClient({ const client = amazon.createClient({
awsId: process.env.AWS_ACCESS_KEY_ID, awsId: process.env.AWS_ACCESS_KEY_ID,
awsSecret: process.env.AWS_SECRET_ACCESS_KEY, awsSecret: process.env.AWS_SECRET_ACCESS_KEY,
@ -10,7 +14,7 @@ const client = amazon.createClient({
}); });
export function* lookupId(id, type) { export function* lookupId(id, type) {
try {
const results = yield client.itemLookup({ const results = yield client.itemLookup({
itemId: id, itemId: id,
responseGroup: 'ItemAttributes,Images,ItemIds', responseGroup: 'ItemAttributes,Images,ItemIds',
@ -18,7 +22,7 @@ export function* lookupId(id, type) {
const result = results[0]; const result = results[0];
if (!result) { if (!result || !result.Error) {
return { service: 'amazon' }; return { service: 'amazon' };
} }
@ -58,22 +62,28 @@ export function* lookupId(id, type) {
}, },
}; };
} }
} catch (err) {
debug(inspect(err, { depth: 4 }));
}
return { service: 'amazon' }; return { service: 'amazon' };
} }
export function* search(data, original = {}) { export function* search(data, original = {}) {
try {
const type = data.type; const type = data.type;
const results = yield client.itemSearch({ const results = yield client.itemSearch({
author: data.artist.name, author: data.artist.name.normalize(),
title: data.name, title: data.name.normalize(),
searchIndex: 'MP3Downloads', searchIndex: 'MP3Downloads',
responseGroup: 'ItemAttributes,Tracks,Images,ItemIds', responseGroup: 'ItemAttributes,Tracks,Images,ItemIds',
}); });
const result = results[0]; const result = results[0];
if (result) { if (!result || !result.Error) {
return { service: 'amazon' };
}
if (type === 'album') { if (type === 'album') {
return { return {
service: 'amazon', service: 'amazon',
@ -110,8 +120,9 @@ export function* search(data, original = {}) {
}, },
}; };
} }
} catch (err) {
debug(inspect(err, { depth: 4 }));
} }
return { service: 'amazon' }; return { service: 'amazon' };
} }

View file

@ -2,6 +2,7 @@ import co from 'co';
import kue from 'kue'; import kue from 'kue';
import raven from 'raven'; import raven from 'raven';
import debuglog from 'debug'; import debuglog from 'debug';
import { inspect } from 'util';
import models from './models'; import models from './models';
import services from './lib/services'; import services from './lib/services';
@ -51,16 +52,16 @@ function search(data, done) {
} catch (err) { } catch (err) {
debug(`Error searching on: ${service.id}`); debug(`Error searching on: ${service.id}`);
debug(share); debug(share);
debug(err); debug(inspect(err, { depth: 5 }));
raven.captureException(err); raven.captureException(err);
return done(err); return done(err);
} }
}).catch((err) => { }).catch((err) => {
debug(`Error searching on: ${service.id}`); debug(`Error searching on: ${service.id}`);
debug(share); debug(share);
debug(err); debug(inspect(err, { depth: 5 }));
raven.captureException(err); raven.captureException(err);
return done(); return done(err);
}); });
} }