Skip to content

Commit 9e7dae9

Browse files
committed
AC-746: Malformed request body or parameters cause "Internal Server Error"
Update API response, to change malformed param request to respond with 400 instead of 500
1 parent 4eedd8e commit 9e7dae9

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

app/code/Magento/Webapi/Controller/Rest/SynchronousRequestProcessor.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,7 @@ public function process(\Magento\Framework\Webapi\Rest\Request $request)
9494
/**
9595
* @var \Magento\Framework\Api\AbstractExtensibleObject $outputData
9696
*/
97-
try {
98-
$outputData = call_user_func_array([$service, $serviceMethodName], $inputParams);
99-
} catch (\Exception $e) {
100-
// Re-throw other exceptions as WebapiException with 400 status code
101-
throw new WebapiException(
102-
new Phrase($e->getMessage()),
103-
0,
104-
WebapiException::HTTP_BAD_REQUEST
105-
);
106-
}
97+
$outputData = call_user_func_array([$service, $serviceMethodName], $inputParams);
10798

10899
$outputData = $this->serviceOutputProcessor->process(
109100
$outputData,

lib/internal/Magento/Framework/Webapi/ErrorProcessor.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
*/
66
declare(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

Comments
 (0)