mirror of
https://github.com/strapi/strapi.git
synced 2025-12-25 14:14:10 +00:00
refactor error-handling PR
This commit is contained in:
parent
cb098ec280
commit
db1cb3040c
@ -77,7 +77,7 @@ describe('Authenticated User', () => {
|
||||
{
|
||||
message: 'this field has unspecified keys: roles',
|
||||
name: 'ValidationError',
|
||||
path: [''],
|
||||
path: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
@ -105,7 +105,7 @@ describe('Authenticated User', () => {
|
||||
{
|
||||
message: 'this field has unspecified keys: isActive',
|
||||
name: 'ValidationError',
|
||||
path: [''],
|
||||
path: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
@ -133,7 +133,7 @@ describe('Authenticated User', () => {
|
||||
{
|
||||
message: 'this field has unspecified keys: isActive',
|
||||
name: 'ValidationError',
|
||||
path: [''],
|
||||
path: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@ -235,17 +235,12 @@ describe('Content Type Builder - Components', () => {
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(res.body).toEqual({
|
||||
error: {
|
||||
<<<<<<< HEAD
|
||||
'component.category': ['category.required'],
|
||||
'component.icon': ['icon.required'],
|
||||
'component.displayName': ['displayName.required'],
|
||||
=======
|
||||
details: {
|
||||
errors: [
|
||||
{
|
||||
message: 'name.required',
|
||||
message: 'displayName.required',
|
||||
name: 'ValidationError',
|
||||
path: ['component', 'name'],
|
||||
path: ['component', 'displayName'],
|
||||
},
|
||||
{
|
||||
message: 'icon.required',
|
||||
@ -261,7 +256,6 @@ describe('Content Type Builder - Components', () => {
|
||||
},
|
||||
message: '3 errors occurred',
|
||||
name: 'ValidationError',
|
||||
>>>>>>> e0bdafeecb (refactor error handling)
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
8
packages/core/upload/server/bootstrap.js
vendored
8
packages/core/upload/server/bootstrap.js
vendored
@ -51,8 +51,12 @@ const createProvider = config => {
|
||||
|
||||
return Object.assign(Object.create(baseProvider), {
|
||||
...providerInstance,
|
||||
upload: (file, options = actionOptions.upload) => providerInstance.upload(file, options),
|
||||
delete: (file, options = actionOptions.delete) => providerInstance.delete(file, options),
|
||||
upload(file, options = actionOptions.upload) {
|
||||
return providerInstance.upload(file, options);
|
||||
},
|
||||
delete(file, options = actionOptions.delete) {
|
||||
return providerInstance.delete(file, options);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@ -1,20 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const { isEmpty } = require('lodash/fp');
|
||||
|
||||
// Temporary fix of this issue : https://github.com/jquense/yup/issues/616
|
||||
const sanitizeErrorMessage = message =>
|
||||
message.replace(
|
||||
`\n If "null" is intended as an empty value be sure to mark the schema as \`.nullable()\``,
|
||||
''
|
||||
);
|
||||
const { isEmpty, toPath } = require('lodash/fp');
|
||||
|
||||
const formatYupInnerError = yupError => ({
|
||||
path: yupError.path
|
||||
.replace(/\[/g, '.')
|
||||
.replace(/]/g, '')
|
||||
.split('.'),
|
||||
message: sanitizeErrorMessage(yupError.message),
|
||||
path: toPath(yupError.path),
|
||||
message: yupError.message,
|
||||
name: yupError.name,
|
||||
});
|
||||
|
||||
@ -22,7 +12,7 @@ const formatYupErrors = yupError => ({
|
||||
errors: isEmpty(yupError.inner)
|
||||
? [formatYupInnerError(yupError)]
|
||||
: yupError.inner.map(formatYupInnerError),
|
||||
message: sanitizeErrorMessage(yupError.message),
|
||||
message: yupError.message,
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
|
||||
51
packages/core/utils/lib/print-value.js
Normal file
51
packages/core/utils/lib/print-value.js
Normal file
@ -0,0 +1,51 @@
|
||||
'use strict';
|
||||
|
||||
// Code copied from the yup library (https://github.com/jquense/yup)
|
||||
// https://github.com/jquense/yup/blob/2778b88bdacd5260d593c6468793da2e77daf21f/src/util/printValue.ts
|
||||
|
||||
const toString = Object.prototype.toString;
|
||||
const errorToString = Error.prototype.toString;
|
||||
const regExpToString = RegExp.prototype.toString;
|
||||
const symbolToString = typeof Symbol !== 'undefined' ? Symbol.prototype.toString : () => '';
|
||||
|
||||
const SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/;
|
||||
|
||||
function printNumber(val) {
|
||||
if (val != +val) return 'NaN';
|
||||
const isNegativeZero = val === 0 && 1 / val < 0;
|
||||
return isNegativeZero ? '-0' : '' + val;
|
||||
}
|
||||
|
||||
function printSimpleValue(val, quoteStrings = false) {
|
||||
if (val == null || val === true || val === false) return '' + val;
|
||||
|
||||
const typeOf = typeof val;
|
||||
if (typeOf === 'number') return printNumber(val);
|
||||
if (typeOf === 'string') return quoteStrings ? `"${val}"` : val;
|
||||
if (typeOf === 'function') return '[Function ' + (val.name || 'anonymous') + ']';
|
||||
if (typeOf === 'symbol') return symbolToString.call(val).replace(SYMBOL_REGEXP, 'Symbol($1)');
|
||||
|
||||
const tag = toString.call(val).slice(8, -1);
|
||||
if (tag === 'Date') return isNaN(val.getTime()) ? '' + val : val.toISOString(val);
|
||||
if (tag === 'Error' || val instanceof Error) return '[' + errorToString.call(val) + ']';
|
||||
if (tag === 'RegExp') return regExpToString.call(val);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function printValue(value, quoteStrings) {
|
||||
let result = printSimpleValue(value, quoteStrings);
|
||||
if (result !== null) return result;
|
||||
|
||||
return JSON.stringify(
|
||||
value,
|
||||
function(key, value) {
|
||||
let result = printSimpleValue(this[key], quoteStrings);
|
||||
if (result !== null) return result;
|
||||
return value;
|
||||
},
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = printValue;
|
||||
@ -5,6 +5,7 @@ const _ = require('lodash');
|
||||
const { defaults } = require('lodash/fp');
|
||||
const utils = require('./string-formatting');
|
||||
const { YupValidationError } = require('./errors');
|
||||
const printValue = require('./print-value');
|
||||
|
||||
const MixedSchemaType = yup.mixed;
|
||||
|
||||
@ -82,6 +83,26 @@ const validateYupSchemaSync = (schema, options = {}) => (body, errorMessage) =>
|
||||
}
|
||||
};
|
||||
|
||||
// Temporary fix of this issue : https://github.com/jquense/yup/issues/616
|
||||
yup.setLocale({
|
||||
mixed: {
|
||||
notType({ path, type, value, originalValue }) {
|
||||
let isCast = originalValue != null && originalValue !== value;
|
||||
let msg =
|
||||
`${path} must be a \`${type}\` type, ` +
|
||||
`but the final value was: \`${printValue(value, true)}\`` +
|
||||
(isCast ? ` (cast from the value \`${printValue(originalValue, true)}\`).` : '.');
|
||||
|
||||
/* Remove comment that is not supposed to be seen by the enduser
|
||||
if (value === null) {
|
||||
msg += `\n If "null" is intended as an empty value be sure to mark the schema as \`.nullable()\``;
|
||||
}
|
||||
*/
|
||||
return msg;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
yup,
|
||||
handleYupError,
|
||||
|
||||
@ -261,7 +261,7 @@ describe('CRUD locales', () => {
|
||||
details: {
|
||||
errors: [
|
||||
{
|
||||
path: [''],
|
||||
path: [],
|
||||
name: 'ValidationError',
|
||||
message: 'this field has unspecified keys: code',
|
||||
},
|
||||
@ -293,7 +293,7 @@ describe('CRUD locales', () => {
|
||||
details: {
|
||||
errors: [
|
||||
{
|
||||
path: [''],
|
||||
path: [],
|
||||
name: 'ValidationError',
|
||||
message: 'this field has unspecified keys: code',
|
||||
},
|
||||
|
||||
@ -17,9 +17,6 @@
|
||||
"cloudinary": "^1.25.1",
|
||||
"into-stream": "^5.1.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@strapi/plugin-upload": "^4.0.0"
|
||||
},
|
||||
"strapi": {
|
||||
"isProvider": true
|
||||
},
|
||||
|
||||
@ -33,9 +33,6 @@
|
||||
"dependencies": {
|
||||
"@strapi/utils": "3.6.8"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@strapi/plugin-upload": "^4.0.0"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/strapi/strapi/issues"
|
||||
},
|
||||
|
||||
@ -19,12 +19,10 @@ Object.defineProperty(global, 'strapi', {
|
||||
plugin.policy = name => plugin.policies[name];
|
||||
});
|
||||
|
||||
const oldService = value.service;
|
||||
strapiInstance.service = (name = '') => {
|
||||
if (name.startsWith('admin::')) {
|
||||
return strapiInstance.admin.services[name.split('admin::')[1]];
|
||||
}
|
||||
return oldService(name);
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user