2022-12-19 15:30:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-01-30 15:12:17 +00:00
|
|
|
import { createNotice, DEFAULT_ERROR_MESSAGE } from '@woocommerce/base-utils';
|
2022-12-19 15:30:13 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2023-01-19 16:40:52 +00:00
|
|
|
import {
|
|
|
|
objectHasProp,
|
|
|
|
ApiErrorResponse,
|
|
|
|
isApiErrorResponse,
|
2024-01-16 11:01:51 +00:00
|
|
|
ApiErrorResponseData,
|
|
|
|
isObject,
|
|
|
|
isString,
|
2023-01-19 16:40:52 +00:00
|
|
|
} from '@woocommerce/types';
|
2022-12-19 15:30:13 +00:00
|
|
|
import { noticeContexts } from '@woocommerce/base-context/event-emit/utils';
|
|
|
|
|
|
|
|
type ApiParamError = {
|
|
|
|
param: string;
|
|
|
|
id: string;
|
|
|
|
code: string;
|
|
|
|
message: string;
|
2024-01-16 11:01:51 +00:00
|
|
|
data?: ApiErrorResponseData;
|
2022-12-19 15:30:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flattens error details which are returned from the API when multiple params are not valid.
|
|
|
|
*
|
|
|
|
* - Codes will be prefixed with the param. For example, `invalid_email` becomes `billing_address_invalid_email`.
|
|
|
|
* - Additional error messages will be flattened alongside the main error message.
|
|
|
|
* - Supports 1 level of nesting.
|
|
|
|
* - Decodes HTML entities in error messages.
|
|
|
|
*/
|
2023-01-18 17:02:38 +00:00
|
|
|
export const getErrorDetails = (
|
|
|
|
response: ApiErrorResponse
|
|
|
|
): ApiParamError[] => {
|
2022-12-19 15:30:13 +00:00
|
|
|
const errorDetails = objectHasProp( response.data, 'details' )
|
|
|
|
? Object.entries( response.data.details )
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if ( ! errorDetails ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return errorDetails.reduce(
|
|
|
|
(
|
|
|
|
acc,
|
|
|
|
[
|
|
|
|
param,
|
2024-01-16 11:01:51 +00:00
|
|
|
{
|
|
|
|
code,
|
|
|
|
message,
|
|
|
|
additional_errors: additionalErrors = [],
|
|
|
|
data,
|
|
|
|
},
|
2022-12-19 15:30:13 +00:00
|
|
|
]
|
|
|
|
) => {
|
|
|
|
return [
|
|
|
|
...acc,
|
|
|
|
{
|
|
|
|
param,
|
|
|
|
id: `${ param }_${ code }`,
|
|
|
|
code,
|
|
|
|
message: decodeEntities( message ),
|
2024-01-16 11:01:51 +00:00
|
|
|
data,
|
2022-12-19 15:30:13 +00:00
|
|
|
},
|
|
|
|
...( Array.isArray( additionalErrors )
|
|
|
|
? additionalErrors.flatMap( ( additionalError ) => {
|
|
|
|
if (
|
|
|
|
! objectHasProp( additionalError, 'code' ) ||
|
|
|
|
! objectHasProp( additionalError, 'message' )
|
|
|
|
) {
|
|
|
|
return [];
|
|
|
|
}
|
2024-01-16 11:01:51 +00:00
|
|
|
const errorObject = [
|
2022-12-19 15:30:13 +00:00
|
|
|
{
|
|
|
|
param,
|
|
|
|
id: `${ param }_${ additionalError.code }`,
|
|
|
|
code: additionalError.code,
|
|
|
|
message: decodeEntities(
|
|
|
|
additionalError.message
|
|
|
|
),
|
2024-01-20 07:21:47 +00:00
|
|
|
data,
|
2022-12-19 15:30:13 +00:00
|
|
|
},
|
|
|
|
];
|
2024-01-16 11:01:51 +00:00
|
|
|
if ( typeof additionalError.data !== 'undefined' ) {
|
|
|
|
return [
|
|
|
|
...errorObject,
|
|
|
|
...getErrorDetails( additionalError ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return errorObject;
|
2022-12-19 15:30:13 +00:00
|
|
|
} )
|
|
|
|
: [] ),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
[] as ApiParamError[]
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-30 15:12:17 +00:00
|
|
|
/**
|
|
|
|
* Gets appropriate error context from error code.
|
|
|
|
*/
|
|
|
|
const getErrorContextFromCode = ( code: string ): string => {
|
|
|
|
switch ( code ) {
|
|
|
|
case 'woocommerce_rest_missing_email_address':
|
|
|
|
case 'woocommerce_rest_invalid_email_address':
|
|
|
|
return noticeContexts.CONTACT_INFORMATION;
|
|
|
|
default:
|
|
|
|
return noticeContexts.CART;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-01-18 12:33:50 +00:00
|
|
|
* Gets appropriate error context from error param name. Also checks the error code to check if it's an email error
|
|
|
|
* (Email is part of the billing address).
|
2023-01-30 15:12:17 +00:00
|
|
|
*/
|
2024-01-18 12:33:50 +00:00
|
|
|
const getErrorContextFromParam = (
|
|
|
|
param: string,
|
|
|
|
code: string
|
|
|
|
): string | undefined => {
|
2023-01-30 15:12:17 +00:00
|
|
|
switch ( param ) {
|
|
|
|
case 'invalid_email':
|
|
|
|
return noticeContexts.CONTACT_INFORMATION;
|
|
|
|
case 'billing_address':
|
2024-01-18 12:33:50 +00:00
|
|
|
if ( code === 'invalid_email' ) {
|
|
|
|
return noticeContexts.CONTACT_INFORMATION;
|
|
|
|
}
|
2023-01-30 15:12:17 +00:00
|
|
|
return noticeContexts.BILLING_ADDRESS;
|
|
|
|
case 'shipping_address':
|
|
|
|
return noticeContexts.SHIPPING_ADDRESS;
|
|
|
|
default:
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-16 11:01:51 +00:00
|
|
|
/**
|
|
|
|
* Gets appropriate error context from additional field location.
|
|
|
|
*/
|
|
|
|
const getErrorContextFromAdditionalFieldLocation = (
|
|
|
|
location: string
|
|
|
|
): string | undefined => {
|
|
|
|
switch ( location ) {
|
|
|
|
case 'contact':
|
|
|
|
return noticeContexts.CONTACT_INFORMATION;
|
|
|
|
case 'additional':
|
|
|
|
return noticeContexts.ADDITIONAL_INFORMATION;
|
|
|
|
default:
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-19 15:30:13 +00:00
|
|
|
/**
|
|
|
|
* Processes the response for an invalid param error, with response code rest_invalid_param.
|
|
|
|
*/
|
2023-01-18 17:02:38 +00:00
|
|
|
const processInvalidParamResponse = (
|
|
|
|
response: ApiErrorResponse,
|
|
|
|
context: string | undefined
|
|
|
|
) => {
|
2022-12-19 15:30:13 +00:00
|
|
|
const errorDetails = getErrorDetails( response );
|
|
|
|
|
2024-01-16 11:01:51 +00:00
|
|
|
errorDetails.forEach( ( { code, message, id, param, data } ) => {
|
|
|
|
let additionalFieldContext: string | undefined = '';
|
|
|
|
// Check if this error response comes from an additional field.
|
|
|
|
if (
|
|
|
|
isObject( data ) &&
|
|
|
|
objectHasProp( data, 'key' ) &&
|
|
|
|
objectHasProp( data, 'location' ) &&
|
|
|
|
isString( data.location )
|
|
|
|
) {
|
|
|
|
additionalFieldContext = getErrorContextFromAdditionalFieldLocation(
|
|
|
|
data.location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:12:17 +00:00
|
|
|
createNotice( 'error', message, {
|
|
|
|
id,
|
|
|
|
context:
|
|
|
|
context ||
|
2024-01-16 11:01:51 +00:00
|
|
|
additionalFieldContext ||
|
2024-01-18 12:33:50 +00:00
|
|
|
getErrorContextFromParam( param, code ) ||
|
2023-01-30 15:12:17 +00:00
|
|
|
getErrorContextFromCode( code ),
|
|
|
|
} );
|
2022-12-19 15:30:13 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes an API response object and creates error notices to display to the customer.
|
|
|
|
*
|
|
|
|
* This is where we can handle specific error codes and display notices in specific contexts.
|
|
|
|
*/
|
2023-01-18 17:02:38 +00:00
|
|
|
export const processErrorResponse = (
|
2023-01-30 15:12:17 +00:00
|
|
|
response: ApiErrorResponse | null,
|
|
|
|
context?: string | undefined
|
2023-01-18 17:02:38 +00:00
|
|
|
) => {
|
2023-01-19 16:40:52 +00:00
|
|
|
if ( ! isApiErrorResponse( response ) ) {
|
2022-12-19 15:30:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-01-30 15:12:17 +00:00
|
|
|
|
|
|
|
if ( response.code === 'rest_invalid_param' ) {
|
|
|
|
return processInvalidParamResponse( response, context );
|
|
|
|
}
|
|
|
|
|
|
|
|
let errorMessage =
|
|
|
|
decodeEntities( response.message ) || DEFAULT_ERROR_MESSAGE;
|
|
|
|
|
|
|
|
// Replace the generic invalid JSON message with something more user friendly.
|
|
|
|
if ( response.code === 'invalid_json' ) {
|
|
|
|
errorMessage = DEFAULT_ERROR_MESSAGE;
|
2022-12-19 15:30:13 +00:00
|
|
|
}
|
2023-01-30 15:12:17 +00:00
|
|
|
|
|
|
|
createNotice( 'error', errorMessage, {
|
|
|
|
id: response.code,
|
|
|
|
context: context || getErrorContextFromCode( response.code ),
|
|
|
|
} );
|
2022-12-19 15:30:13 +00:00
|
|
|
};
|