使用Laravel 5.2从库中捕获异常 [英] Catching Exceptions from library using Laravel 5.2

查看:216
本文介绍了使用Laravel 5.2从库中捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当新的laravel,所以我不知道如何处理错误,如何最好地抓住他们。

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..

此库称为Steam Condenser: https://github.com/koraktor/steam-condenser

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.

这是我的代码:

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'));
}

如果服务器脱机,它会抛出一个SocketException,我尝试抓住,但这似乎从来没有发生。然后我收到带有跟踪的错误页面。

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.

我的try / catch有什么问题吗?拉扯这种方式会抓住错误吗?这是第三方图书馆的问题吗?

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?

推荐答案

几件事:


  • 跟踪是否导致SocketException或其他错误?在SocketException可以抛出之前,可能会遇到一个不同的错误。

  • 您的catch语句捕获 SocketException 。您是否在PHP文件顶部导入完整的命名空间? 使用SteamCondenser\Exceptions\SocketException;

  • 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;

为了进行调试,你可以做一个例外catch all并转储异常类型:

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));
}

如果在尝试上述代码后仍然获得堆栈跟踪,则会出现错误在try / catch块启动之前被抛出。

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

这篇关于使用Laravel 5.2从库中捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆