Ana-Maria Milea
Ana-Maria Milea

Reputation: 11

Cannot catch QueryException in Laravel

I'm trying to catch a QueryException error in a try/catch block. I'm running a migrate:fresh command from the terminal and setting some config values. I want to rule out some exceptions and catch this error if it appears. I tried everything but could not seem to succeed.

Command

php artisan migrate:fresh --seed

Error

Illuminate\Database\QueryException SQLSTATE[08006] [7] FATAL: database "x" does not exist (SQL: select tablename from pg_catalog.pg_tables where schemana*me in ('public'))

Code

try {
    DB::purge('pgsql');
    Config::set('database.connections.pgsql.database', $config['database']);
    Config::set('database.connections.pgsql.username', $config['username']);
    Config::set('database.connections.pgsql.password', $config['password']);
} catch (\Throwable $e) {
    dd($e); //it never gets here
    Log::error($e->getMessage());
}

I also tried

catch (\Illuminate\Database\QueryException $e) 
catch (\Exception $e)
catch (\PDOException $e)

Is there any way to do this?

Upvotes: 0

Views: 1833

Answers (1)

Ana-Maria Milea
Ana-Maria Milea

Reputation: 11

After hours of trying, I managed to catch the error. The $this->laravel->call([$this, 'handle']) was in another class and this is where the try/catch block should actually be.

Upvotes: 1

Related Questions