Skip to content

Bug: JSONFormatter->format() cannot handle errordata, only outputs it's own error #2434

Description

@hcbd

Describe the bug
CI only outputs the error thrown in JSONFormatter->format() function, not the one that preceded it.
CI Outputs:

<br />
<font size='1'><table class='xdebug-error xe-uncaught-exception' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Uncaught CodeIgniter\Format\Exceptions\FormatException: Failed to parse json string, error: "{0}". in /source/system/Format/Exceptions/FormatException.php on line <i>9</i></th></tr>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> CodeIgniter\Format\Exceptions\FormatException: Failed to parse json string, error: "{0}". in /source/system/Format/Exceptions/FormatException.php on line <i>9</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0133</td><td bgcolor='#eeeeec' align='right'>2283712</td><td bgcolor='#eeeeec'>CodeIgniter\Debug\Exceptions->exceptionHandler(  )</td><td title='/source/system/Debug/Exceptions.php' bgcolor='#eeeeec'>.../Exceptions.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0135</td><td bgcolor='#eeeeec' align='right'>2294352</td><td bgcolor='#eeeeec'>CodeIgniter\Debug\Exceptions->respond(  )</td><td title='/source/system/Debug/Exceptions.php' bgcolor='#eeeeec'>.../Exceptions.php<b>:</b>165</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0135</td><td bgcolor='#eeeeec' align='right'>2294352</td><td bgcolor='#eeeeec'>CodeIgniter\Debug\Exceptions->format(  )</td><td title='/source/system/API/ResponseTrait.php' bgcolor='#eeeeec'>.../ResponseTrait.php<b>:</b>125</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.0138</td><td bgcolor='#eeeeec' align='right'>2367056</td><td bgcolor='#eeeeec'>CodeIgniter\Format\JSONFormatter->format(  )</td><td title='/source/system/API/ResponseTrait.php' bgcolor='#eeeeec'>.../ResponseTrait.php<b>:</b>387</td></tr>
</table></font>
{
    "title": "ErrorException",
    "type": "ErrorException",
    "code": 500,
    "message": "Uncaught CodeIgniter\\Format\\Exceptions\\FormatException: Failed to parse json string, error: \"{0}\". in /source/system/Format/Exceptions/FormatException.php:9\nStack trace:\n#0 /source/system/Format/JSONFormatter.php(68): CodeIgniter\\Format\\Exceptions\\FormatException::forInvalidJSON('Type is not sup...')\n#1 /source/system/API/ResponseTrait.php(387): CodeIgniter\\Format\\JSONFormatter->format(Array)\n#2 /source/system/API/ResponseTrait.php(125): CodeIgniter\\Debug\\Exceptions->format(Array)\n#3 /source/system/Debug/Exceptions.php(165): CodeIgniter\\Debug\\Exceptions->respond(Array, 500)\n#4 [internal function]: Code",
    "file": "/source/system/Format/Exceptions/FormatException.php",
    "line": 9,
    "trace": [
        {
            "function": "shutdownHandler",
            "class": "CodeIgniter\\Debug\\Exceptions",
            "type": "->",
            "args": []
        }
    ]
}

CodeIgniter 4 version
4.0.0-rc3

Affected module(s)
Format\JSONFormatter

Expected behavior, and steps to reproduce if appropriate
Get the first error or all errors displayed in the requested format (aka either html, json or xml)

Reproduce
I encountered this bug when i was sending a respons from my controller which uses a model to get information from a database and display that, but i made a typo in my SQL query, which caused an error, which caused another error in JSONFormatter->format().

public function format($data)

I only found my SQL typo because i added an print_r($data); statement in the above function and saw the 'first' error. This also made me see that the format() function runs twice, once for my error and after that it ran again, this time with the error information of the error generated in the same function
so:

  1. first run of format($data)
    it receives an error object via $data,
    json_encode() fails on this object,
    json_last_error() gives 8 (== JSON_ERROR_UNSUPPORTED_TYPE )
    an Exception is thrown because of the json_encode failure,
  2. second run of format($data)
    it receives the above thrown error message, which can be parsed by json_encode, so no error
    but now it returns the error array.

The output is like above: a HTML error with after that a JSON error
But the first error is missing completely from this error, which is not handy for debugging

Context

  • OS: Fedora 31
  • Web server: PHP build-in Webserver
  • PHP version 7.3.12

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions