11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2012 Adobe
4+ * All rights reserved .
55 */
66declare (strict_types=1 );
77
@@ -43,6 +43,17 @@ class ErrorProcessor
4343
4444 public const DATA_FORMAT_XML = 'xml ' ;
4545
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+
4657 /**
4758 * @var \Magento\Framework\Json\Encoder $encoder
4859 */
@@ -145,6 +156,10 @@ public function maskException(\Exception $exception)
145156 $ stackTrace
146157 );
147158 } 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 ;
148163 $ message = $ exception ->getMessage ();
149164 $ code = $ exception ->getCode ();
150165 //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)
157172 $ maskedException = new WebapiException (
158173 new Phrase ($ message ),
159174 $ code ,
160- WebapiException:: HTTP_INTERNAL_ERROR ,
175+ $ httpCode ,
161176 [],
162177 '' ,
163178 null ,
@@ -167,6 +182,21 @@ public function maskException(\Exception $exception)
167182 return $ maskedException ;
168183 }
169184
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+
170200 /**
171201 * Process API exception.
172202 *
0 commit comments