在我们的用于手枪比赛的Laravel系统中,当创建一个比赛时,我们使用一个地方或城镇在地图上做一个标记。它工作得很完美,但有时我们会得到一个不可能的地方名称和一个巨大的错误通知。错误会持续几秒钟,然后消失,用户可以返回并给出正确的位置地址。我想要一个像“非法地址,请更正”之类的错误信息。这是控制器中的地理编码部分:
$address = $competition->contact_city; // From create.blade.php
$json = file_get_contents('https://eu1.locationiq.com/v1/search.php?key=pk.0bf95265572a146a766348aff22ea4ad&q=' . $address . '&format=json');
$jsonArr = json_decode($json);
$lat = $jsonArr[0]->lat;
$lng = $jsonArr[0]->lon;
$competition->lat = $lat; // Stores coordinates in database
$competition->lng = $lng; 如何在创建竞赛时捕获错误并提供反馈?
错误的一部分:(使用冒泡池作为地址)
key=pk.0bf95265572a146a766348aff22ea4ad&q=sdfgsdfg&format=json):无法打开流: HTTP请求失败!/var/www/app/Http/Controllers/Api/CompetitionsController.php:204 /1.1 404在/var/www/vendor/sentry/sentry/lib/Raven/Breadcrumbs/ErrorHandler.php(34):Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2,堆栈跟踪中找不到:#0 Raven_Breadcrumbs_ErrorHandler->handleError(2,'file_get_conten...','/var/www/app/Ht...',204,数组) #1内部函数:HTTP 'file_get_conten...','/var/www/app/Ht...',204,数组) #2 /var/www/app/Http/Controllers/Api/CompetitionsController.php(204):数组(‘https://eu1.loc...') #3内部函数:数组#4 /var/www/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55):file_get_contents(数组,数组) #5
在create.blade.php中,我收到了一条巨大的错误消息,其中有数百行我提取了上面的行。
如果需要,我也可以安装create blade。
发布于 2021-05-22 04:49:44
我要说的是,你可以做一些事情,其中之一是关闭生产服务器中的错误报告。第二种方法是通过在函数前面使用@来使函数静默:
$json = @file_get_contents('https://eu1.locationiq.com/v1/search.php?key=pk.0bf95265572a146a766348aff22ea4ad&q=' . $address . '&format=json');
# Check first if the return is empty
if(!empty($json)) {
$jsonArr = json_decode($json);
$competition->lat = $lat = $jsonArr[0]->lat;
$competition->lng = $lng = $jsonArr[0]->lon;
}
else {
# Whatever you want here for the error stuff, maybe throw an Exception?
# This kinda depends on what you are doing for errors. I don't really
# Know Laravel, so maybe there is some standard way to handle errors
}https://stackoverflow.com/questions/67640768
复制相似问题