mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-28 00:06:53 +01:00
ccaab0c47b
Instead of instructing users to do static calls in to Unleash, she should instead be allwed to specify the log provider as an option to Unleash. This commit introduces the "getLogger" option, a function responsible for creating a logger.
55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const joi = require('joi');
|
|
|
|
const customJoi = joi.extend(j => ({
|
|
base: j.string(),
|
|
name: 'string',
|
|
language: {
|
|
isUrlFriendly: 'must be URL friendly',
|
|
},
|
|
rules: [
|
|
{
|
|
name: 'isUrlFriendly',
|
|
validate(params, value, state, options) {
|
|
if (encodeURIComponent(value) !== value) {
|
|
// Generate an error, state and options need to be passed
|
|
return this.createError(
|
|
'string.isUrlFriendly',
|
|
{ v: value },
|
|
state,
|
|
options
|
|
);
|
|
}
|
|
return value; // Everything is OK
|
|
},
|
|
},
|
|
],
|
|
}));
|
|
|
|
const nameType = customJoi
|
|
.string()
|
|
.isUrlFriendly()
|
|
.min(2)
|
|
.max(100)
|
|
.required();
|
|
|
|
const handleErrors = (res, logger, error) => {
|
|
logger.warn(error.message);
|
|
switch (error.name) {
|
|
case 'NotFoundError':
|
|
return res.status(404).end();
|
|
case 'NameExistsError':
|
|
case 'ValidationError':
|
|
return res
|
|
.status(400)
|
|
.json(error)
|
|
.end();
|
|
default:
|
|
logger.error('Server failed executing request', error);
|
|
return res.status(500).end();
|
|
}
|
|
};
|
|
|
|
module.exports = { nameType, handleErrors };
|