如何在Laravel作业上格式化运行时错误消息-Laravel

 收藏

我目前正在使用laravel job将csv文件中的数据插入数据库。并且,代码如下:

try{
    //insertCode

} catch (\Exception $e) {
    $this->error_arr[] = $e->getMessage();
    $this->error_row_numbers[] = $this->data['row_value'];
}

$this->data['row_value'] = $this->data['row_value'] + 1;

在这里,我使用了try catch块,因此只要遇到任何运行时错误,它都会捕获错误消息并存储row_numbers。这样一来,我就可以发送有关该错误发生在哪一行的详细信息。

该流程运行正常,只是我要格式化的错误消息。基本上可以发生两种类型的错误。

the value could be empty the value could be in different format.

目前,它引发如下消息:

空记录

ERROR: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'initial_amenity_value' at row 1 (SQL: insert into `amenity_values` (`amenity_id`, `initial_amenity_value`, `amenity_value`, `updated_at`, `created_at`) values (3103, , , 2020-03-26 20:12:16, 2020-03-26 20:12:16))

格式错误

ERROR: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'initial_amenity_value' at row 1 (SQL: insert into `amenity_values` (`amenity_id`, `initial_amenity_value`, `amenity_value`, `updated_at`, `created_at`) values (3105, , , 2020-03-26 20:12:16, 2020-03-26 20:12:16))

How could I format this message to something like: Empty value initial_amenity_value,wrong format on initial_amenity_value

回复