diff --git a/lib/routes/admin-api/feature.js b/lib/routes/admin-api/feature.js index f52278228b..093e2057f2 100644 --- a/lib/routes/admin-api/feature.js +++ b/lib/routes/admin-api/feature.js @@ -3,7 +3,6 @@ const Controller = require('../controller'); const joi = require('joi'); -const logger = require('../../logger')('/admin-api/feature.js'); const { FEATURE_CREATED, FEATURE_UPDATED, @@ -11,38 +10,9 @@ const { } = require('../../event-type'); const NameExistsError = require('../../error/name-exists-error'); const NameInvalidError = require('../../error/name-invalid-error'); -const { isUrlFriendlyName } = require('./util'); - +const { isUrlFriendlyName, handleErrors } = require('./util'); const extractUser = require('../../extract-user'); - const { featureShema } = require('./feature-schema'); - -const handleErrors = (req, res, error) => { - logger.warn('Error creating or updating feature', error); - switch (error.name) { - case 'NotFoundError': - return res.status(404).end(); - case 'NameInvalidError': - return res - .status(400) - .json([{ msg: error.message }]) - .end(); - case 'NameExistsError': - return res - .status(403) - .json([{ msg: error.message }]) - .end(); - case 'ValidationError': - return res - .status(400) - .json(error) - .end(); - default: - logger.error('Server failed executing request', error); - return res.status(500).end(); - } -}; - const version = 1; class FeatureController extends Controller { @@ -85,7 +55,7 @@ class FeatureController extends Controller { await this.validateUniqueName(name); res.status(201).end(); } catch (error) { - handleErrors(req, res, error); + handleErrors(res, error); } } @@ -128,7 +98,7 @@ class FeatureController extends Controller { }); res.status(201).end(); } catch (error) { - handleErrors(req, res, error); + handleErrors(res, error); } } @@ -149,7 +119,7 @@ class FeatureController extends Controller { }); res.status(200).end(); } catch (error) { - handleErrors(req, res, error); + handleErrors(res, error); } } @@ -170,7 +140,7 @@ class FeatureController extends Controller { }); res.status(200).end(); } catch (error) { - handleErrors(req, res, error); + handleErrors(res, error); } } @@ -189,7 +159,7 @@ class FeatureController extends Controller { }); res.status(200).end(); } catch (error) { - handleErrors(req, res, error); + handleErrors(res, error); } } } diff --git a/lib/routes/admin-api/strategy.js b/lib/routes/admin-api/strategy.js index 1fb0384bbd..a6e721e5d2 100644 --- a/lib/routes/admin-api/strategy.js +++ b/lib/routes/admin-api/strategy.js @@ -4,10 +4,10 @@ const Controller = require('../controller'); const joi = require('joi'); const eventType = require('../../event-type'); -const logger = require('../../logger')('/admin-api/strategy.js'); const NameExistsError = require('../../error/name-exists-error'); const extractUser = require('../../extract-user'); const strategySchema = require('./strategy-schema'); +const { handleErrors } = require('./util'); const version = 1; class StrategyController extends Controller { @@ -53,7 +53,7 @@ class StrategyController extends Controller { }); res.status(200).end(); } catch (error) { - this._handleErrorResponse(req, res, error); + handleErrors(res, error); } } @@ -68,7 +68,7 @@ class StrategyController extends Controller { }); res.status(201).end(); } catch (error) { - this._handleErrorResponse(req, res, error); + handleErrors(res, error); } } @@ -87,7 +87,7 @@ class StrategyController extends Controller { }); res.status(200).end(); } catch (error) { - this._handleErrorResponse(req, res, error); + handleErrors(res, error); } } @@ -113,27 +113,6 @@ class StrategyController extends Controller { throw new Error(`Cannot edit strategy ${strategy.name}`); } } - - _handleErrorResponse(req, res, error) { - logger.warn('Error creating or updating strategy', error); - switch (error.name) { - case 'NotFoundError': - return res.status(404).end(); - case 'NameExistsError': - return res - .status(403) - .json([{ msg: error.message }]) - .end(); - case 'ValidationError': - return res - .status(400) - .json(error) - .end(); - default: - logger.error('Could perfom operation', error); - return res.status(500).end(); - } - } } module.exports = StrategyController; diff --git a/lib/routes/admin-api/util.js b/lib/routes/admin-api/util.js index 6664e034ad..8c62d31cd3 100644 --- a/lib/routes/admin-api/util.js +++ b/lib/routes/admin-api/util.js @@ -1,5 +1,33 @@ 'use strict'; +const logger = require('../../logger')('/admin-api/util.js'); + const isUrlFriendlyName = input => encodeURIComponent(input) === input; -module.exports = { isUrlFriendlyName }; +const handleErrors = (res, error) => { + logger.warn(error.message); + switch (error.name) { + case 'NotFoundError': + return res.status(404).end(); + case 'NameInvalidError': + return res + .status(400) + .json([{ msg: error.message }]) + .end(); + case 'NameExistsError': + return res + .status(403) + .json([{ msg: error.message }]) + .end(); + case 'ValidationError': + return res + .status(400) + .json(error) + .end(); + default: + logger.error('Server failed executing request', error); + return res.status(500).end(); + } +}; + +module.exports = { isUrlFriendlyName, handleErrors };