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,73 @@ 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 LoggerInterface|null $logger
55+ * @param ErrorMessageConfigInterface|null $errorMessageConfig
56+ * @param ValidationErrorMessagesProvider|null $validationErrorMessagesProvider
3557 */
3658 public function __construct (
3759 ActionFlag $ actionFlag ,
38- SerializerInterface $ serializer
60+ SerializerInterface $ serializer ,
61+ ?LoggerInterface $ logger = null ,
62+ ?ErrorMessageConfigInterface $ errorMessageConfig = null ,
63+ ?ValidationErrorMessagesProvider $ validationErrorMessagesProvider = null
3964 ) {
4065 $ this ->actionFlag = $ actionFlag ;
4166 $ this ->serializer = $ serializer ;
67+ $ this ->logger = $ logger
68+ ?? ObjectManager::getInstance ()->get (LoggerInterface::class);
69+ $ this ->errorMessageConfig = $ errorMessageConfig
70+ ?? ObjectManager::getInstance ()->get (ErrorMessageConfigInterface::class);
71+ $ this ->validationErrorMessagesProvider = $ validationErrorMessagesProvider
72+ ?? ObjectManager::getInstance ()->get (ValidationErrorMessagesProvider::class);
4273 }
4374
4475 /**
4576 * Set "no dispatch" flag and error message to Response
4677 *
4778 * @param ResponseInterface $response
48- * @param string $message
79+ * @param array $errorMessages
80+ * @param string $sourceKey
4981 * @return void
5082 */
51- public function processError (ResponseInterface $ response , string $ message ): void
83+ public function processError (ResponseInterface $ response , array $ errorMessages , string $ sourceKey ): void
5284 {
85+ $ validationErrorText = $ this ->errorMessageConfig ->getValidationFailureMessage ();
86+ $ technicalErrorText = $ this ->errorMessageConfig ->getTechnicalFailureMessage ();
87+
88+ $ message = $ errorMessages ? $ validationErrorText : $ technicalErrorText ;
89+
90+ foreach ($ errorMessages as $ errorMessageCode => $ errorMessageText ) {
91+ if (!$ this ->isValidationError ($ errorMessageCode )) {
92+ $ message = $ technicalErrorText ;
93+ $ this ->logger ->error (
94+ __ (
95+ 'reCAPTCHA \'%1 \' form error: %2 ' ,
96+ $ sourceKey ,
97+ $ errorMessageText
98+ )
99+ );
100+ }
101+ }
102+
53103 $ this ->actionFlag ->set ('' , Action::FLAG_NO_DISPATCH , true );
54104
55105 $ jsonPayload = $ this ->serializer ->serialize ([
@@ -58,4 +108,15 @@ public function processError(ResponseInterface $response, string $message): void
58108 ]);
59109 $ response ->representJson ($ jsonPayload );
60110 }
111+
112+ /**
113+ * Check if error code present in validation errors list.
114+ *
115+ * @param string $errorMessageCode
116+ * @return bool
117+ */
118+ private function isValidationError (string $ errorMessageCode ): bool
119+ {
120+ return $ errorMessageCode !== $ this ->validationErrorMessagesProvider ->getErrorMessage ($ errorMessageCode );
121+ }
61122}
0 commit comments