0

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?

4
  • 1
    Is the exception thrown in php artisan migrate:fresh --seed? How does the rest of the code relate to that? Commented Jan 5, 2022 at 11:35
  • have you tried catch (\QueryException $e) ? Commented Jan 5, 2022 at 11:43
  • try this stackoverflow.com/a/65780158/4575350 Commented Jan 5, 2022 at 12:10
  • The command triggers a function in which I have the try/catch block. I tried both \QueryException and \PDOException. Commented Jan 5, 2022 at 13:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.