I'm working on handling exceptions in my Laravel project. I'm handling them in the Handler like this:
public function render($request, Exception $exception)
{
if ($exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
return response()->view('generalerror', ['name' => 'The requested resource could not be found.', 'error' => $exception->getMessage()]);
}
if ($exception instanceof \PDOException) {
return response()->view('generalerror', ['name' => 'There was a problem adding the requested resource', 'error' => $exception->getMessage()]);
}
return parent::render($request, $exception);
}
Everything works fine with the ModelNotFoundException. I get the correct message.
Displaying the PDO exception message will result in something like this:
SQLSTATE[HY000]: General error: 1364 Field 'fiscal_code' doesn't have a default value (SQL: insert into `patients` (`surname`, `name`, `POB`, `DOB`, `gender`, `marital_status`, `profession`, `residence`, `telephone_1`, `owner_1`, `telephone_2`, `owner_2`, `telephone_3`, `owner_3`, `updated_at`, `created_at`) values (eyJpdiI6IjBjSXVvVUFkQnBWK2UyN1hhc2JSUVE9PSIsInZhbHVlIjoianNBQjI1OEVvTklEUXhZdUV2TmNQQT09IiwibWFjIjoiODE4MjU3OGM3NzAzZDBlYmNhODI3ZmJkOGMxZGRlM2QxYWJjZTY1MWU5YmZiMGNiYzk4ZTMxYTM3NGM2OGEyOCJ9, eyJpdiI6IjlVZUVcL0pDcDdLdEV5akJ2N0lPRFlnPT0iLCJ2YWx1ZSI6Inh3RjdLSFVpSGtQRjRjT1wvOFdLK2VnPT0iLCJtYWMiOiI5ZDA4MzI3MWM1OTk2ZTJhYThhZDM0OTI1NTE3Y2NhODk4OWRhNWZjNDUzOGJkZDhkNzMyZTE0NmMyOGM0NTQ5In0=, , 1111-11-11, eyJpdiI6IldNSFloZUVmMjFuM1RrRWZHN2NvWVE9PSIsInZhbHVlIjoiRXN3OXZNdlpGMUtRK2VDbTh6ZzFVZz09IiwibWFjIjoiMGNlOTI1YTJlYWI1MzM4OTc4OTAwYWVjMDI1YjVmNDkxZmQ5MjlkMzZiNzg1YWVhOWM2MjQyN2JiZDQ0NDQ2NyJ9, eyJpdiI6Ik1udTVsRWF4NXNuY0Fsc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tqVkN4dz09IiwibWFjIjoiNWNmMDBhNzc5MmJlMjlhMzViYzdhNWNhNzViOGUwMDhjMmJjY2U5ZmIwNDBiZTA0N2NjMWI5MzljYjllZDk4MyJ9, eyJpdiI6IlpVU0xjWEtIUTc1Y3kyUlwva1VBWEh3PT0iLCJ2YWx1ZSI6IkcyM2RJVHRiM3kzZ0hORGxmSVduemc9PSIsIm1hYyI6Ijc1NmI2ZDI5ZTcwZDM1NmEyYzM1YmVjMDk2NWYwOTJkZWI0MTc5MmI1Njg2ZGQzMjc3Y2M4ZjlmZWVmNWE3ZGIifQ==, eyJpdiI6Iml6TGJ3YlF3RThYc01cLytvZUFZa013PT0iLCJ2YWx1ZSI6ImtkTVl5Y0lnQkdXZzlwa0xPZ2V5a1E9PSIsIm1hYyI6IjM3MGZkZjk2YzJiNmFmZWJhODA5Nzc0N2Q2OWNhYjY2YTUxMmE1NThjZTAzNzIzZWI2NTk1MGE0ODM2NTEwN2IifQ==, eyJpdiI6IkQySEZjS1BmZld1OHhQcTJxUEtkQWc9PSIsInZhbHVlIjoieGtMcWhYalBJVlBjYVc2XC9OZWpLTkE9PSIsIm1hYyI6IjMwODA2N2Y1Nzc3ZDM1MWM3MzM1OGJlNzNjMDUwM2E2NDEwYWZjOWZkZTU3MDkwMGZlZDdhMWMxODhhYmFhYzcifQ==, 2020-04-05 13:32:26, 2020-04-05 13:32:26))
This will expose all my DB fields and the query itself. I want a simple message like:
SQLSTATE[HY000]: General error: 1364
I tried to use getCode but this will display only "HY000" that is a general database error message, not giving any hint on what the problem could be.
How can I get the message I want?
Furthermore, using Debugbar, I see that when saveOrFail throws an exception, it will throw boht PDOException and QueryException. Is this correct?
Thank you.
I hope it helps
if ($exception instanceof \PDOException && Str::contains($exception->getMessage(), '(SQL:')) {
dd(Str::before($exception->getMessage(), '(SQL:'));
}
Printed SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
for me