9
9
10
10
use Magento \Framework \App \Action \Action ;
11
11
use Magento \Framework \App \ActionFlag ;
12
+ use Magento \Framework \App \ObjectManager ;
12
13
use Magento \Framework \App \ResponseInterface ;
13
14
use Magento \Framework \Serialize \SerializerInterface ;
15
+ use Magento \ReCaptchaUi \Model \ErrorMessageConfigInterface ;
16
+ use Magento \ReCaptchaValidationApi \Model \ValidationErrorMessagesProvider ;
17
+ use Psr \Log \LoggerInterface ;
14
18
15
19
/**
16
20
* Process error during ajax login
@@ -29,27 +33,73 @@ class ErrorProcessor
29
33
*/
30
34
private $ serializer ;
31
35
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
+
32
51
/**
33
52
* @param ActionFlag $actionFlag
34
53
* @param SerializerInterface $serializer
54
+ * @param LoggerInterface|null $logger
55
+ * @param ErrorMessageConfigInterface|null $errorMessageConfig
56
+ * @param ValidationErrorMessagesProvider|null $validationErrorMessagesProvider
35
57
*/
36
58
public function __construct (
37
59
ActionFlag $ actionFlag ,
38
- SerializerInterface $ serializer
60
+ SerializerInterface $ serializer ,
61
+ ?LoggerInterface $ logger = null ,
62
+ ?ErrorMessageConfigInterface $ errorMessageConfig = null ,
63
+ ?ValidationErrorMessagesProvider $ validationErrorMessagesProvider = null
39
64
) {
40
65
$ this ->actionFlag = $ actionFlag ;
41
66
$ 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);
42
73
}
43
74
44
75
/**
45
76
* Set "no dispatch" flag and error message to Response
46
77
*
47
78
* @param ResponseInterface $response
48
- * @param string $message
79
+ * @param array $errorMessages
80
+ * @param string $sourceKey
49
81
* @return void
50
82
*/
51
- public function processError (ResponseInterface $ response , string $ message ): void
83
+ public function processError (ResponseInterface $ response , array $ errorMessages , string $ sourceKey ): void
52
84
{
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
+
53
103
$ this ->actionFlag ->set ('' , Action::FLAG_NO_DISPATCH , true );
54
104
55
105
$ jsonPayload = $ this ->serializer ->serialize ([
@@ -58,4 +108,15 @@ public function processError(ResponseInterface $response, string $message): void
58
108
]);
59
109
$ response ->representJson ($ jsonPayload );
60
110
}
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
+ }
61
122
}
0 commit comments