Search code examples
exceptionlumen

Need Lumen render() method to return HTTP '200' status code


Building a simple Lumen API that takes webhook payloads from WooCommerce. When my API returns a 500 'internal error response', WooCommerce automatically switches off the webhook, as a matter of reliability. I find this a bit problematic in my case.

I would like my Lumen API to return exceptions, such as errors like it normally does, but always with a 200 status code, never a (for instance) 500 or 404.

I've learned that I need to adapt /App/Exceptions/Handler.php, more specifically the render() method. It contains the line: return parent::render($request, $exception);, which generates and returns the typical, helpful Laravel/Lumen error data.

(How) can I get my application to return this same helpful error data, but with a HTTP 200 status code and never a 500, or any other? Maybe something like this below (which does not work by the way as the render() method directly renders, as opposed to actually return a string).

return response(parent::render($request, $exception), 200);

Solution

  • The render() method actually returns a response object, that has the response HTML as a property. Therefore, my effort above was already close to the solution. I just needed to retrieve the HTML-content from the response object. The following was therefore the solution:

    $response = parent::render($request, $exception);
    return response($response->content(), 200);
    

    The following source was particularly useful to get here: https://laravel.com/api/5.5/Illuminate/Http/Response.html.