99
1010use Magento \Framework \App \Action \Action ;
1111use Magento \Framework \App \ActionFlag ;
12+ use Magento \Framework \App \ObjectManager ;
1213use Magento \Framework \App \ResponseInterface ;
1314use Magento \Framework \Serialize \SerializerInterface ;
15+ use Magento \ReCaptchaUi \Model \ErrorMessageConfigInterface ;
16+ use Magento \ReCaptchaValidationApi \Model \ValidationErrorMessagesProvider ;
17+ use Psr \Log \LoggerInterface ;
1418
1519/**
1620 * Process error during ajax login
@@ -29,27 +33,72 @@ class ErrorProcessor
2933 */
3034 private $ serializer ;
3135
36+ /**
37+ * @var LoggerInterface
38+ */
39+ private $ logger ;
40+
41+ /**
42+ * @var ErrorMessageConfigInterface
43+ */
44+ private $ errorMessageConfig ;
45+
46+ /**
47+ * @var ValidationErrorMessagesProvider
48+ */
49+ private $ validationErrorMessagesProvider ;
50+
3251 /**
3352 * @param ActionFlag $actionFlag
3453 * @param SerializerInterface $serializer
54+ * @param ErrorMessageConfigInterface|null $errorMessageConfig
55+ * @param ValidationErrorMessagesProvider|null $validationErrorMessagesProvider
3556 */
3657 public function __construct (
3758 ActionFlag $ actionFlag ,
38- SerializerInterface $ serializer
59+ SerializerInterface $ serializer ,
60+ ?LoggerInterface $ logger = null ,
61+ ?ErrorMessageConfigInterface $ errorMessageConfig = null ,
62+ ?ValidationErrorMessagesProvider $ validationErrorMessagesProvider = null
3963 ) {
4064 $ this ->actionFlag = $ actionFlag ;
4165 $ this ->serializer = $ serializer ;
66+ $ this ->logger = $ logger
67+ ?? ObjectManager::getInstance ()->get (LoggerInterface::class);
68+ $ this ->errorMessageConfig = $ errorMessageConfig
69+ ?? ObjectManager::getInstance ()->get (ErrorMessageConfigInterface::class);
70+ $ this ->validationErrorMessagesProvider = $ validationErrorMessagesProvider
71+ ?? ObjectManager::getInstance ()->get (ValidationErrorMessagesProvider::class);
4272 }
4373
4474 /**
4575 * Set "no dispatch" flag and error message to Response
4676 *
4777 * @param ResponseInterface $response
48- * @param string $message
78+ * @param array $errorMessages
79+ * @param string $sourceKey
4980 * @return void
5081 */
51- public function processError (ResponseInterface $ response , string $ message ): void
82+ public function processError (ResponseInterface $ response , array $ errorMessages , string $ sourceKey ): void
5283 {
84+ $ validationErrorText = $ this ->errorMessageConfig ->getValidationFailureMessage ();
85+ $ technicalErrorText = $ this ->errorMessageConfig ->getTechnicalFailureMessage ();
86+
87+ $ message = $ errorMessages ? $ validationErrorText : $ technicalErrorText ;
88+
89+ foreach ($ errorMessages as $ errorMessageCode => $ errorMessageText ) {
90+ if (!$ this ->isValidationError ($ errorMessageCode )) {
91+ $ message = $ technicalErrorText ;
92+ $ this ->logger ->error (
93+ __ (
94+ 'reCAPTCHA \'%1 \' form error: %2 ' ,
95+ $ sourceKey ,
96+ $ errorMessageText
97+ )
98+ );
99+ }
100+ }
101+
53102 $ this ->actionFlag ->set ('' , Action::FLAG_NO_DISPATCH , true );
54103
55104 $ jsonPayload = $ this ->serializer ->serialize ([
@@ -58,4 +107,15 @@ public function processError(ResponseInterface $response, string $message): void
58107 ]);
59108 $ response ->representJson ($ jsonPayload );
60109 }
110+
111+ /**
112+ * Check if error code present in validation errors list.
113+ *
114+ * @param string $errorMessageCode
115+ * @return bool
116+ */
117+ private function isValidationError (string $ errorMessageCode ): bool
118+ {
119+ return $ errorMessageCode !== $ this ->validationErrorMessagesProvider ->getErrorMessage ($ errorMessageCode );
120+ }
61121}
0 commit comments