zfcampus/zf-content-negotiation

Uncaught InvalidMultipartContentException when the boundary is missing

leogr opened this issue · 2 comments

leogr commented

If the MultipartContentParser is unable to detect MIME boundary an InvalidMultipartContentException is thrown (see here).

When that happens within the ContentTypeListener (see here) then the exception is not caught so a fatal error occurs:

<br />
<b>Fatal error</b>:  Uncaught exception 'ZF\ContentNegotiation\Exception\InvalidMultipartContentException' in /var/www/vendor/zfcampus/zf-content-negotiation/src/MultipartContentParser.php:35
Stack trace:
#0 /var/www/vendor/zfcampus/zf-content-negotiation/src/ContentTypeListener.php(82): ZF\ContentNegotiation\MultipartContentParser-&gt;__construct(Object(Zend\Http\Header\ContentType), Object(ZF\ContentNegotiation\Request))
#1 [internal function]: ZF\ContentNegotiation\ContentTypeListener-&gt;__invoke(Object(Zend\Mvc\MvcEvent))
#2 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(444): call_user_func(Object(ZF\ContentNegotiation\ContentTypeListener), Object(Zend\Mvc\MvcEvent))
#3 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(205): Zend\EventManager\EventManager-&gt;triggerListeners('route', Object(Zend\Mvc\MvcEvent), Object(Closure))
#4 /var/www/vendor/zendframework/zend-mvc/src/Application.php(297): Zend\EventManager\EventManager-&gt;trigger('route', Object(Zend\Mvc in 
<b>/var/www/vendor/zfcampus/zf-content-negotiation/src/MultipartContentParser.php</b> on line 
<b>35</b>
<br />

I would suggest that you move the MultipartContentParser within the try...catch block, so:

try {
    $parser = new MultipartContentParser($contentType, $request);
    $bodyParams = $parser->parse();
} catch (Exception\ExceptionInterface $e) {
    $bodyParams = new ApiProblemResponse(new ApiProblem(
        400,
        $e
    ));
    break;
}

In this way a 400 Bad Request will be returned, instead of getting a fatal error.

If you agree with my solution I can make a PR.

+1, any news about this issue?

leogr commented

I made a PR #63