doulianqi3870 2016-01-23 23:54
浏览 30
已采纳

使用Laravel 5.2从库中捕获异常

Pretty new to laravel, so I'm not exactly sure how it handles errors and how best to catch them.

I'm using a 3rd party game server connection library that can query game servers in order to pull data such as players, current map etc..

This library is called Steam Condenser : https://github.com/koraktor/steam-condenser

I have imported this using composer in my project and all seems to be working fine, however I'm having trouble with catching exceptions that are thrown by the library.

One example is where the game server you are querying is offline.

Here is my code:

public function show($server_name)
{
    try{
        SteamSocket::setTimeout(3000);
        $server = server::associatedServer($server_name);
        $server_info = new SourceServer($server->server_ip);

        $server_info->rconAuth($server->server_rcon);

        $players = $server_info->getPlayers();
        $total_players = count($players);

        $more_info = $server_info->getServerInfo();

        $maps = $server_info->rconExec('maps *');
        preg_match_all("/(?<=fs\)).*?(?=\.bsp)/", $maps, $map_list);    
    }catch(SocketException $e){
        dd("error");
    }
    return view('server', compact('server', 'server_info', 'total_players', 'players', 'more_info', 'map_list'));
}

If the server is offline, it will throw a SocketException, which I try to catch, however this never seems to happen. I then get the error page with the trace.

This causes a bit of a problem as I wish to simply tell the end user that the server is offline, however I cannot do this if I can't catch this error.

Is there something wrong with my try/catch? Does laravel handle catching errors in this way? Is this an issue with the 3rd party library?

  • 写回答

1条回答 默认 最新

  • doulai7239 2016-01-24 01:07
    关注

    A couple things:

    • Does the trace lead to the SocketException or to a different error? It's possible that a different error is being caught before the SocketException can be thrown.
    • Your catch statement is catching SocketException. Are you importing the full namespace at the top of your PHP file? use SteamCondenser\Exceptions\SocketException;

    Also for debugging purposes, you could do an exception "catch all" and dump the type of exception:

    try {
        ...
    }catch(\Exception $e){
        dd(get_class($e));
    }
    

    If you still get the stack trace after trying the above code, then an error is being thrown before the try/catch block starts.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛