phpsqllaravelreporting

How do I find out where this error is coming from?


Working on a Laravel 8 project, I am getting the following error:

ERROR: SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for type integer: "" CONTEXT:  unnamed portal parameter $1 = '' (SQL: select * from "[subjects]" where "[subjects]"."id" =  and "[subjects]"."deleted_at" is null limit 1) {"userId":297,"exception":"[object] (Illuminate\\Database\\QueryException(code: 22P02): SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for type integer: \"\"\nCONTEXT:  unnamed portal parameter $1 = '' (SQL: select * from \"[subjects]\" where \"[subjects]\".\"id\" =  and \"[subjects]\".\"deleted_at\" is null limit 1) at /var/www/[dev_site]/vendor/laravel/framework/src/Illuminate/Database/Connection.php:712)\n[previous exception] [object] (PDOException(code: 22P02): SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for type integer: \"\"\nCONTEXT:  unnamed portal parameter $1 = '' at /var/www/[dev_site]/vendor/laravel/framework/src/Illuminate/Database/Connection.php:373)

This is obviously a query to access all the model values for [subjects] which is failing because the id being passed is not defined. Unfortunately, I don't know where this is being called from. It is called from multiple places and so the error is popping up in multiple pages but I cannot locate the exact code that is generating this error which is being generated by:

/var/www/[dev_site]/vendor/laravel/framework/src/Illuminate/Database/Connection.php:373 

In some places where it comes up in the log, it appears to have no effect on the functioning of the code. In other places it breaks the code. I've been seraching the code with no luck. Is there some way to tell what code is triggering the error in Connection.php?


Solution

  • You can use

    try {
        ... your code here
    } catch (Exception $e) {
        var_dump($e->getTraceAsString());
    }
    

    It will list all method calls leading to the error you're getting. Let's say you have a controller that does lots and lots of things, you can wrap the entire body of the controller in a try-catch block, and anything that is executed in it that throws an error will get caught.

    class MyController
    {
        public function myAction()
        {
            try {
               // lots
               // lots
               // and lots
               // of things
            } catch (Exception $e) {
                var_dump($e->getTraceAsString());
            }
        }
    }
    

    If you have no idea where this error occurs, you can wrap the method in your database wrapper/facade class in a try-catch block, and send the entire trace/exception to your logger, instead of just the message. Just remember to re-throw the exception so it gets handled further down properly, instead of suppressing it.