forked from DebaucheryLibrarian/traxxx
Added error handler to web server.
This commit is contained in:
parent
f018735052
commit
0eba0461c9
|
@ -0,0 +1,20 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
class HttpError extends Error {
|
||||||
|
constructor(message, httpCode, friendlyMessage, data) {
|
||||||
|
super(message);
|
||||||
|
|
||||||
|
this.name = 'HttpError';
|
||||||
|
this.httpCode = httpCode;
|
||||||
|
|
||||||
|
if (friendlyMessage) {
|
||||||
|
this.friendlyMessage = friendlyMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { HttpError };
|
|
@ -5,6 +5,7 @@ const inquirer = require('inquirer');
|
||||||
const logger = require('./logger')(__filename);
|
const logger = require('./logger')(__filename);
|
||||||
const knex = require('./knex');
|
const knex = require('./knex');
|
||||||
const { flushOrphanedMedia } = require('./media');
|
const { flushOrphanedMedia } = require('./media');
|
||||||
|
const { HttpError } = require('./errors');
|
||||||
|
|
||||||
function curateRelease(release, withMedia = false, withPoster = true) {
|
function curateRelease(release, withMedia = false, withPoster = true) {
|
||||||
if (!release) {
|
if (!release) {
|
||||||
|
@ -126,6 +127,10 @@ async function fetchScene(releaseId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchScenes(limit = 100) {
|
async function fetchScenes(limit = 100) {
|
||||||
|
if (typeof limit !== 'number') {
|
||||||
|
throw new HttpError('Limit parameter needs to be a number', 400);
|
||||||
|
}
|
||||||
|
|
||||||
const releases = await knex('releases')
|
const releases = await knex('releases')
|
||||||
.modify(withRelations, false, true)
|
.modify(withRelations, false, true)
|
||||||
.limit(Math.min(limit, 1000000));
|
.limit(Math.min(limit, 1000000));
|
||||||
|
@ -134,6 +139,14 @@ async function fetchScenes(limit = 100) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function searchScenes(query, limit = 100, relevance = 0) {
|
async function searchScenes(query, limit = 100, relevance = 0) {
|
||||||
|
if (typeof limit !== 'number') {
|
||||||
|
throw new HttpError('Limit parameter needs to be a number', 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof relevance !== 'number') {
|
||||||
|
throw new HttpError('Relevance parameter needs to be a number', 400);
|
||||||
|
}
|
||||||
|
|
||||||
const releases = await knex
|
const releases = await knex
|
||||||
.select(knex.raw('search_results.rank as relevance'))
|
.select(knex.raw('search_results.rank as relevance'))
|
||||||
.from(knex.raw('search_releases(:query) as search_results', { query }))
|
.from(knex.raw('search_releases(:query) as search_results', { query }))
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const logger = require('../logger')(__filename);
|
||||||
|
|
||||||
|
function errorHandler(error, req, res, _next) {
|
||||||
|
logger.warn(`Failed to fulfill request to ${req.path}: ${error.message}`);
|
||||||
|
|
||||||
|
if (error.httpCode) {
|
||||||
|
res.status(error.httpCode).send(error.message);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(500).send('Oops... our server messed up. We will be investigating this incident, our apologies for the inconvenience.');
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = errorHandler;
|
|
@ -18,9 +18,11 @@ async function fetchSceneApi(req, res) {
|
||||||
|
|
||||||
async function fetchScenesApi(req, res) {
|
async function fetchScenesApi(req, res) {
|
||||||
const query = req.query.query || req.query.q;
|
const query = req.query.query || req.query.q;
|
||||||
|
const limit = req.query.limit && Number(req.query.limit);
|
||||||
|
const relevance = req.query.relevance && Number(req.query.relevance);
|
||||||
|
|
||||||
const releases = query
|
const releases = query
|
||||||
? await searchScenes(query, req.query.limit, req.query.relevance)
|
? await searchScenes(query, limit, relevance)
|
||||||
: await fetchScenes(req.query.limit);
|
: await fetchScenes(req.query.limit);
|
||||||
|
|
||||||
res.send({ scenes: releases });
|
res.send({ scenes: releases });
|
||||||
|
|
|
@ -17,6 +17,7 @@ const PgOrderByRelatedPlugin = require('@graphile-contrib/pg-order-by-related');
|
||||||
const logger = require('../logger')(__filename);
|
const logger = require('../logger')(__filename);
|
||||||
const knex = require('../knex');
|
const knex = require('../knex');
|
||||||
const { ActorPlugins, SitePlugins, ReleasePlugins } = require('./plugins/plugins');
|
const { ActorPlugins, SitePlugins, ReleasePlugins } = require('./plugins/plugins');
|
||||||
|
const errorHandler = require('./error');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
fetchScene,
|
fetchScene,
|
||||||
|
@ -124,6 +125,7 @@ async function initServer() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.use(errorHandler);
|
||||||
app.use(router);
|
app.use(router);
|
||||||
|
|
||||||
const server = app.listen(config.web.port, config.web.host, () => {
|
const server = app.listen(config.web.port, config.web.host, () => {
|
||||||
|
|
Loading…
Reference in New Issue