mirror of
https://github.com/strapi/strapi.git
synced 2026-01-07 20:58:16 +00:00
Chore: Give precedence to error.message over error.details
This commit is contained in:
parent
2a83d266b8
commit
750655fb4e
@ -16,6 +16,10 @@ export function formatAPIError(error, { formatMessage, intlMessagePrefixCallback
|
||||
|
||||
const normalizedError = normalizeAPIError(error, intlMessagePrefixCallback);
|
||||
|
||||
if (normalizedError.message) {
|
||||
return normalizedError.message;
|
||||
}
|
||||
|
||||
// stringify multiple errors
|
||||
if (normalizedError?.errors) {
|
||||
return normalizedError.errors
|
||||
|
||||
@ -62,4 +62,25 @@ describe('formatAPIError', () => {
|
||||
test('error if formatMessage was not passed', () => {
|
||||
expect(() => formatAPIError(API_VALIDATION_ERROR_FIXTURE)).toThrow();
|
||||
});
|
||||
|
||||
test('give precedence to error.message over error.details', () => {
|
||||
expect(
|
||||
formatAPIError(
|
||||
{
|
||||
...API_VALIDATION_ERROR_FIXTURE,
|
||||
response: {
|
||||
...API_VALIDATION_ERROR_FIXTURE.response,
|
||||
data: {
|
||||
...API_VALIDATION_ERROR_FIXTURE.response.data,
|
||||
error: {
|
||||
...API_VALIDATION_ERROR_FIXTURE.response.data.error,
|
||||
message: 'Main error message',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{ formatMessage, getTrad: (translation) => translation }
|
||||
)
|
||||
).toBe('Main error message');
|
||||
});
|
||||
});
|
||||
|
||||
@ -20,6 +20,7 @@ export function normalizeAPIError(apiError, intlMessagePrefixCallback) {
|
||||
if (error?.details?.errors) {
|
||||
return {
|
||||
name: error.name,
|
||||
message: error?.message || null,
|
||||
errors: error.details.errors.map((err) =>
|
||||
normalizeError(err, { name: error.name, intlMessagePrefixCallback })
|
||||
),
|
||||
|
||||
@ -38,6 +38,7 @@ describe('normalizeAPIError', () => {
|
||||
test('Handle ValidationError', () => {
|
||||
expect(normalizeAPIError(API_VALIDATION_ERROR_FIXTURE)).toStrictEqual({
|
||||
name: 'ValidationError',
|
||||
message: null,
|
||||
errors: [
|
||||
{
|
||||
defaultMessage: 'Field contains errors',
|
||||
@ -65,6 +66,7 @@ describe('normalizeAPIError', () => {
|
||||
|
||||
expect(normalizeAPIError(API_VALIDATION_ERROR_FIXTURE, prefixFunction)).toStrictEqual({
|
||||
name: 'ValidationError',
|
||||
message: null,
|
||||
errors: [
|
||||
{
|
||||
name: 'ValidationError',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user