Update to Koa 2

This commit is contained in:
Jonathan Cremin 2018-04-13 21:44:31 +01:00
parent 42b6cd5a32
commit 87459b2acc
15 changed files with 467 additions and 419 deletions

View file

@ -3,16 +3,16 @@ import debuglog from 'debug';
const debug = debuglog('combine.fm');
export default function (raven) {
return function* error(next) {
this.set('Server', 'Nintendo 64');
return async function error(ctx, next) {
ctx.set('Server', 'Nintendo 64');
try {
yield next;
await next();
} catch (err) {
if (err.status === 404) {
this.body = '404 Page Not Found';
ctx.body = '404 Page Not Found';
} else if (err.status >= 400 && err.status < 500) {
this.status = err.status;
this.body = err.error;
ctx.status = err.status;
ctx.body = err.error;
} else {
debug('Error: %o', err);
raven.captureException(err);
@ -20,21 +20,21 @@ export default function (raven) {
}
}
if (this.status !== 404) return;
if (ctx.status !== 404) return;
switch (this.accepts('html', 'json')) {
switch (ctx.accepts('html', 'json')) {
case 'html':
this.type = 'html';
this.body = '404 Page Not Found';
ctx.type = 'html';
ctx.body = '404 Page Not Found';
break;
case 'json':
this.body = {
ctx.body = {
message: 'Page Not Found',
};
break;
default:
this.type = 'text';
this.body = 'Page Not Found';
ctx.type = 'text';
ctx.body = 'Page Not Found';
}
};
}

View file

@ -47,43 +47,3 @@ export function create(music) {
],
});
}
export function findMatchesAsync(share) {
process.nextTick(() => {
for (const service of services) {
if (service.id === share.service) {
continue; // eslint-disable-line no-continue
}
co(function* gen() { // eslint-disable-line no-loop-func
const match = yield service.search(share);
if (match.id) {
models.match.create({
trackId: share.$modelOptions.name.singular == 'track' ? share.id : null,
albumId: share.$modelOptions.name.singular == 'album' ? share.id : null,
externalId: match.id.toString(),
service: match.service,
name: match.name,
streamUrl: match.streamUrl,
purchaseUrl: match.purchaseUrl,
artworkSmall: match.artwork.small,
artworkLarge: match.artwork.large,
});
} else {
models.match.create({
trackId: share.$modelOptions.name.singular == 'track' ? share.id : null,
albumId: share.$modelOptions.name.singular == 'album' ? share.id : null,
externalId: null,
service: match.service,
name: null,
streamUrl: null,
purchaseUrl: null,
artworkSmall: null,
artworkLarge: null,
});
}
}).catch((err) => {
debug(err);
});
}
});
}