aungwinthant / apilogger

Small laravel package for viewing api logs which can be used in debugging.
MIT License
346 stars 61 forks source link

Laravel 6.X | Undefined Offset Bug? #8

Closed lloy0076 closed 4 years ago

lloy0076 commented 4 years ago

[
    {
        "id": 1,
        "testCamelCase": "Camel Case",
        "testSnakeCase": null,
        "test_snake_case": "Snake Case"
    }
]

{
    "message": "Undefined offset: 1",
    "exception": "ErrorException",
    "file": "C:\\laragon\\webroots\\current\\laravel-six\\vendor\\awt\\apilogger\\src\\AbstractLogger.php",
    "line": 48,
    "trace": [
        {
            "file": "C:\\laragon\\webroots\\current\\laravel-six\\vendor\\awt\\apilogger\\src\\AbstractLogger.php",
            "line": 48,
            "function": "handleError",
            "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
            "type": "->"
        },
        {
            "file": "C:\\laragon\\webroots\\current\\laravel-six\\vendor\\awt\\apilogger\\src\\DBLogger.php",
            "line": 33,
            "function": "logData",
            "class": "AWT\\AbstractLogger",
            "type": "->"
        },
        {
            "file": "C:\\laragon\\webroots\\current\\laravel-six\\vendor\\awt\\apilogger\\src\\Http\\Middleware\\ApiLogger.php",
            "line": 30,
            "function": "saveLogs",
            "class": "AWT\\DBLogger",
            "type": "->"
        },
        {
            "file": "C:\\laragon\\webroots\\current\\laravel-six\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
            "line": 218,
            "function": "terminate",
            "class": "AWT\\Http\\Middleware\\ApiLogger",
            "type": "->"
        },
        {
            "file": "C:\\laragon\\webroots\\current\\laravel-six\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
            "line": 189,
            "function": "terminateMiddleware",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "C:\\laragon\\webroots\\current\\laravel-six\\public\\index.php",
            "line": 60,
            "function": "terminate",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        }
    ]
}```
lloy0076 commented 4 years ago

I caused that with the attached file in routes/api.php on Laravel 6.

Route::any('v1/test',
    'TestController',
    function (Request $request) {
        $results = [];
        $tests   = [
            [
                'id' => 1,
                'testCamelCase',
            ],
        ];

        $result =
            [
                'id'              => 1,
                'testCamelCase'   => 'testCamelCase',
                'testSnakeCase'   => 'testSnakeCase',
                'test_snake_case' => 'test_snake_case',
            ];

        Log::debug(__FILE__, [$result]);

        $results[] = $result;

        return response()->json($results, 200);
    }
)->name('api.test')->middleware('apilogger');

The file, zipped:

api.zip

The composer.install, zipped:

composer.zip

lloy0076 commented 4 years ago

The offending line is https://github.com/aungwinthant/apilogger/blob/master/src/AbstractLogger.php#L48

        if($currentRouteAction){
            list($controller,$action) = explode('@',$currentRouteAction);
        }

We shouldn't assume that a route has a @.

Let's say we have this:

This is valid because the TestController may have an __invoke as per https://laravel.com/docs/6.x/controllers#single-action-controllers

Or we just put the route into the route file as a closure as above.

aungwinthant commented 4 years ago

Ah, I see. This is a bug for sure and it is my bad. I completely forgot about single action controllers. I will update the package with better testing soon.

lloy0076 commented 4 years ago

@aungwinthant I've put together a fix that at least stops a complete crash and I now see:

Screenshot_2019-09-30 Laravel Six

lloy0076 commented 4 years ago

Oops, I was cowboy coding and took out some important code.