From 18b24b2dc5a21007f62b2e625d50b1f4b7dec784 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Mar 2021 09:22:05 +0100 Subject: Add jobs tag to logger --- server/controllers/api/jobs.ts | 4 ++-- server/middlewares/validators/jobs.ts | 6 ++++-- server/middlewares/validators/pagination.ts | 33 +++++++++++++++++------------ server/middlewares/validators/sort.ts | 2 +- server/middlewares/validators/utils.ts | 4 ++-- 5 files changed, 28 insertions(+), 21 deletions(-) (limited to 'server') diff --git a/server/controllers/api/jobs.ts b/server/controllers/api/jobs.ts index 861cc22b9..d7cee1605 100644 --- a/server/controllers/api/jobs.ts +++ b/server/controllers/api/jobs.ts @@ -9,10 +9,10 @@ import { authenticate, ensureUserHasRight, jobsSortValidator, + paginationValidatorBuilder, setDefaultPagination, setDefaultSort } from '../../middlewares' -import { paginationValidator } from '../../middlewares/validators' import { listJobsValidator } from '../../middlewares/validators/jobs' const jobsRouter = express.Router() @@ -20,7 +20,7 @@ const jobsRouter = express.Router() jobsRouter.get('/:state?', authenticate, ensureUserHasRight(UserRight.MANAGE_JOBS), - paginationValidator, + paginationValidatorBuilder([ 'jobs' ]), jobsSortValidator, setDefaultSort, setDefaultPagination, diff --git a/server/middlewares/validators/jobs.ts b/server/middlewares/validators/jobs.ts index 99ef25e0a..d87b28c06 100644 --- a/server/middlewares/validators/jobs.ts +++ b/server/middlewares/validators/jobs.ts @@ -1,9 +1,11 @@ import * as express from 'express' import { param, query } from 'express-validator' import { isValidJobState, isValidJobType } from '../../helpers/custom-validators/jobs' -import { logger } from '../../helpers/logger' +import { logger, loggerTagsFactory } from '../../helpers/logger' import { areValidationErrors } from './utils' +const lTags = loggerTagsFactory('validators', 'jobs') + const listJobsValidator = [ param('state') .optional() @@ -14,7 +16,7 @@ const listJobsValidator = [ .custom(isValidJobType).withMessage('Should have a valid job state'), (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking listJobsValidator parameters.', { parameters: req.params }) + logger.debug('Checking listJobsValidator parameters.', { parameters: req.params, ...lTags() }) if (areValidationErrors(req, res)) return diff --git a/server/middlewares/validators/pagination.ts b/server/middlewares/validators/pagination.ts index 1cae7848c..6b0a83d80 100644 --- a/server/middlewares/validators/pagination.ts +++ b/server/middlewares/validators/pagination.ts @@ -4,25 +4,30 @@ import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { PAGINATION } from '@server/initializers/constants' -const paginationValidator = [ - query('start') - .optional() - .isInt({ min: 0 }).withMessage('Should have a number start'), - query('count') - .optional() - .isInt({ min: 0, max: PAGINATION.GLOBAL.COUNT.MAX }).withMessage(`Should have a number count (max: ${PAGINATION.GLOBAL.COUNT.MAX})`), +const paginationValidator = paginationValidatorBuilder() - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking pagination parameters', { parameters: req.query }) +function paginationValidatorBuilder (tags: string[] = []) { + return [ + query('start') + .optional() + .isInt({ min: 0 }).withMessage('Should have a number start'), + query('count') + .optional() + .isInt({ min: 0, max: PAGINATION.GLOBAL.COUNT.MAX }).withMessage(`Should have a number count (max: ${PAGINATION.GLOBAL.COUNT.MAX})`), - if (areValidationErrors(req, res)) return + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking pagination parameters', { parameters: req.query, tags }) - return next() - } -] + if (areValidationErrors(req, res)) return + + return next() + } + ] +} // --------------------------------------------------------------------------- export { - paginationValidator + paginationValidator, + paginationValidatorBuilder } diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index e93ceb200..beecc155b 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts @@ -28,7 +28,7 @@ const SORTABLE_VIDEO_REDUNDANCIES_COLUMNS = createSortableColumns(SORTABLE_COLUM const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS) -const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS) +const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS, [ 'jobs' ]) const abusesSortValidator = checkSort(SORTABLE_ABUSES_COLUMNS) const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) const videoImportsSortValidator = checkSort(SORTABLE_VIDEO_IMPORTS_COLUMNS) diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts index 2899bed6f..4167f6d43 100644 --- a/server/middlewares/validators/utils.ts +++ b/server/middlewares/validators/utils.ts @@ -17,12 +17,12 @@ function areValidationErrors (req: express.Request, res: express.Response) { return false } -function checkSort (sortableColumns: string[]) { +function checkSort (sortableColumns: string[], tags: string[] = []) { return [ query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'), (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking sort parameters', { parameters: req.query }) + logger.debug('Checking sort parameters', { parameters: req.query, tags }) if (areValidationErrors(req, res)) return -- cgit v1.2.3