mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-11 00:08:30 +01:00
063d3f0e4a
- After seeing frontend behaviour where the user could add the same tag multiple times, and not get errors or be stopped doing so, we'll change the backend to return a 409 if you tag a feature with a tag it already has. - Previous to this commit, the setup was to do `onConflict().ignore()` which caused the frontend to not get any help from the backend as to whether or not the operation was allowed - This fix adds a custom error and adds a branch to the handleError util method for handling just that error type with a 409. - This caused a couple of tests to receive 409, probably due to insufficient cleanup between tests. Adding faker as a dev-dependency and randomising toggle names and tag values for each test reduces the chance that we'll run into duplicate issues in the future for the tests that touches this problem fixes: #711
57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const joi = require('joi');
|
|
|
|
const customJoi = joi.extend(j => ({
|
|
type: 'isUrlFriendly',
|
|
base: j.string(),
|
|
messages: {
|
|
'isUrlFriendly.base': '{{#label}} must be URL friendly',
|
|
},
|
|
validate(value, helpers) {
|
|
// Base validation regardless of the rules applied
|
|
if (encodeURIComponent(value) !== value) {
|
|
// Generate an error, state and options need to be passed
|
|
return { value, errors: helpers.error('isUrlFriendly.base') };
|
|
}
|
|
return undefined;
|
|
},
|
|
}));
|
|
|
|
const nameType = customJoi
|
|
.isUrlFriendly()
|
|
.min(2)
|
|
.max(100)
|
|
.required();
|
|
|
|
const handleErrors = (res, logger, error) => {
|
|
logger.warn(error.message);
|
|
// eslint-disable-next-line no-param-reassign
|
|
error.isJoi = true;
|
|
switch (error.name) {
|
|
case 'NotFoundError':
|
|
return res.status(404).end();
|
|
case 'InvalidOperationError':
|
|
case 'NameExistsError':
|
|
return res
|
|
.status(409)
|
|
.json(error)
|
|
.end();
|
|
case 'ValidationError':
|
|
return res
|
|
.status(400)
|
|
.json(error)
|
|
.end();
|
|
case 'FeatureHasTagError':
|
|
return res
|
|
.status(409)
|
|
.json(error)
|
|
.end();
|
|
default:
|
|
logger.error('Server failed executing request', error);
|
|
return res.status(500).end();
|
|
}
|
|
};
|
|
|
|
module.exports = { customJoi, nameType, handleErrors };
|