Laravel 处理 PDO 异常消息
Laravel handle PDO Exception message
我正在处理我的 Laravel 项目中的异常。
我在 Handler 中这样处理它们:
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);
}
ModelNotFoundException 一切正常。我收到了正确的消息。
显示 PDO 异常消息将导致如下结果:
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, eyJpdiI6IldNSFloZUVmMjFuM1RrRWZHN2NvWVE9PSIsInZhbHVlIjoiRXN3OXZNdlpGMUtRK2VDbTh6ZzFVZz09IiwibWFjIjoiMGNlOTI1YTJlYWI1MzM4OTc4OTAwYWVjMDI1YjVmNDkxZmQ5MjlkMzZiNzg1YWVhOWM2MjQyN2JiZDQ0NDQ2N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InZhbHVlIjoicDlVQ3NtcGZ6Ykt2TDJVWWtqVkN4dz09IiwibWFjIjoiNWNmMDBhNzc5MmJlMjlhMzViYzdhNWNhNzViOGUwMDhjMmJjY2U5ZmIwNDBiZTA0N2NjMWI5MzljYjllZDk4MyJ9, eyJpdiI6IlpVU0xjWEtIUTc1Y3kyUlwva1VBWEh3PT0iLCJ2YWx1ZSI6IkcyM2RJVHRiM3kzZ0hORGxmSVduemc9PSIsIm1hYyI6Ijc1NmI2ZDI5ZTcwZDM1NmEyYzM1YmVjMDk2NWYwOTJkZWI0MTc5MmI1Njg2ZGQzMjc3Y2M4ZjlmZWVmNWE3ZGIifQ==, eyJpdiI6Iml6TGJ3YlF3RThYc01cLytvZUFZa013PT0iLCJ2YWx1ZSI6ImtkTVl5Y0lnQkdXZzlwa0xPZ2V5a1E9PSIsIm1hYyI6IjM3MGZkZjk2YzJiNmFmZWJhODA5Nzc0N2Q2OWNhYjY2YTUxMmE1NThjZTAzNzIzZWI2NTk1MGE0ODM2NTEwN2IifQ==, eyJpdiI6IkQySEZjS1BmZld1OHhQcTJxUEtkQWc9PSIsInZhbHVlIjoieGtMcWhYalBJVlBjYVc2XC9OZWpLTkE9PSIsIm1hYyI6IjMwODA2N2Y1Nzc3ZDM1MWM3MzM1OGJlNzNjMDUwM2E2NDEwYWZjOWZkZTU3MDkwMGZlZDdhMWMxODhhYmFhYzcifQ==, 2020-04-05 13:32:26, 2020-04-05 13:32:26))
这将公开我所有的数据库字段和查询本身。我想要一条简单的消息,例如:
SQLSTATE[HY000]: General error: 1364
我尝试使用 getCode,但这只会显示 "HY000",这是一条一般的数据库错误消息,不会给出任何可能出现问题的提示。
我怎样才能得到我想要的消息?
此外,使用 Debugbar,我看到当 saveOrFail 抛出异常时,它会同时抛出 PDOException 和 QueryException。这是正确的吗?
谢谢。
希望对你有帮助
if ($exception instanceof \PDOException && Str::contains($exception->getMessage(), '(SQL:')) {
dd(Str::before($exception->getMessage(), '(SQL:'));
}
为我打印SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
我正在处理我的 Laravel 项目中的异常。 我在 Handler 中这样处理它们:
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);
}
ModelNotFoundException 一切正常。我收到了正确的消息。
显示 PDO 异常消息将导致如下结果:
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, eyJpdiI6IldNSFloZUVmMjFuM1RrRWZHN2NvWVE9PSIsInZhbHVlIjoiRXN3OXZNdlpGMUtRK2VDbTh6ZzFVZz09IiwibWFjIjoiMGNlOTI1YTJlYWI1MzM4OTc4OTAwYWVjMDI1YjVmNDkxZmQ5MjlkMzZiNzg1YWVhOWM2MjQyN2JiZDQ0NDQ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IsInZhbHVlIjoicDlVQ3NtcGZ6Ykt2TDJVWWtqVkN4dz09IiwibWFjIjoiNWNmMDBhNzc5MmJlMjlhMzViYzdhNWNhNzViOGUwMDhjMmJjY2U5ZmIwNDBiZTA0N2NjMWI5MzljYjllZDk4MyJ9, eyJpdiI6IlpVU0xjWEtIUTc1Y3kyUlwva1VBWEh3PT0iLCJ2YWx1ZSI6IkcyM2RJVHRiM3kzZ0hORGxmSVduemc9PSIsIm1hYyI6Ijc1NmI2ZDI5ZTcwZDM1NmEyYzM1YmVjMDk2NWYwOTJkZWI0MTc5MmI1Njg2ZGQzMjc3Y2M4ZjlmZWVmNWE3ZGIifQ==, eyJpdiI6Iml6TGJ3YlF3RThYc01cLytvZUFZa013PT0iLCJ2YWx1ZSI6ImtkTVl5Y0lnQkdXZzlwa0xPZ2V5a1E9PSIsIm1hYyI6IjM3MGZkZjk2YzJiNmFmZWJhODA5Nzc0N2Q2OWNhYjY2YTUxMmE1NThjZTAzNzIzZWI2NTk1MGE0ODM2NTEwN2IifQ==, eyJpdiI6IkQySEZjS1BmZld1OHhQcTJxUEtkQWc9PSIsInZhbHVlIjoieGtMcWhYalBJVlBjYVc2XC9OZWpLTkE9PSIsIm1hYyI6IjMwODA2N2Y1Nzc3ZDM1MWM3MzM1OGJlNzNjMDUwM2E2NDEwYWZjOWZkZTU3MDkwMGZlZDdhMWMxODhhYmFhYzcifQ==, 2020-04-05 13:32:26, 2020-04-05 13:32:26))
这将公开我所有的数据库字段和查询本身。我想要一条简单的消息,例如:
SQLSTATE[HY000]: General error: 1364
我尝试使用 getCode,但这只会显示 "HY000",这是一条一般的数据库错误消息,不会给出任何可能出现问题的提示。
我怎样才能得到我想要的消息?
此外,使用 Debugbar,我看到当 saveOrFail 抛出异常时,它会同时抛出 PDOException 和 QueryException。这是正确的吗?
谢谢。
希望对你有帮助
if ($exception instanceof \PDOException && Str::contains($exception->getMessage(), '(SQL:')) {
dd(Str::before($exception->getMessage(), '(SQL:'));
}
为我打印SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value