1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2012 Adobe
4
+ * All rights reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
@@ -43,6 +43,17 @@ class ErrorProcessor
43
43
44
44
public const DATA_FORMAT_XML = 'xml ' ;
45
45
46
+ /**
47
+ * Client error keywords
48
+ */
49
+ private const CLIENT_ERROR_KEYWORDS = [
50
+ 'sqlstate ' ,
51
+ 'missing required ' ,
52
+ 'doesn \'t exist ' ,
53
+ 'not found ' ,
54
+ 'not authorized ' ,
55
+ ];
56
+
46
57
/**
47
58
* @var \Magento\Framework\Json\Encoder $encoder
48
59
*/
@@ -145,6 +156,10 @@ public function maskException(\Exception $exception)
145
156
$ stackTrace
146
157
);
147
158
} else {
159
+ // Check if this is a client error based on message content
160
+ $ httpCode = ($ this ->isClientError ($ exception ))
161
+ ? WebapiException::HTTP_BAD_REQUEST
162
+ : WebapiException::HTTP_INTERNAL_ERROR ;
148
163
$ message = $ exception ->getMessage ();
149
164
$ code = $ exception ->getCode ();
150
165
//if not in Dev mode, make sure the message and code is masked for unanticipated exceptions
@@ -157,7 +172,7 @@ public function maskException(\Exception $exception)
157
172
$ maskedException = new WebapiException (
158
173
new Phrase ($ message ),
159
174
$ code ,
160
- WebapiException:: HTTP_INTERNAL_ERROR ,
175
+ $ httpCode ,
161
176
[],
162
177
'' ,
163
178
null ,
@@ -167,6 +182,21 @@ public function maskException(\Exception $exception)
167
182
return $ maskedException ;
168
183
}
169
184
185
+ /**
186
+ * Determine if an exception is a client error based on message content and context
187
+ *
188
+ * @param \Exception $exception
189
+ * @return bool
190
+ */
191
+ private function isClientError (\Exception $ exception )
192
+ {
193
+ $ message = strtolower ($ exception ->getMessage ());
194
+
195
+ return array_filter (self ::CLIENT_ERROR_KEYWORDS , function ($ keyword ) use ($ message ) {
196
+ return strpos ($ message , $ keyword ) !== false ;
197
+ }) !== [];
198
+ }
199
+
170
200
/**
171
201
* Process API exception.
172
202
*
0 commit comments