1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

chore(modernize): Admin cleanup error-handlers

This commit is contained in:
ivaosthu 2018-12-05 21:16:51 +01:00 committed by Ivar Conradi Østhus
parent ed14e3ed8b
commit 3c8e51118e
3 changed files with 39 additions and 62 deletions

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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 };