Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel和Livewire中抛出时捕获errorException_Php_Laravel - Fatal编程技术网

Php 在Laravel和Livewire中抛出时捕获errorException

Php 在Laravel和Livewire中抛出时捕获errorException,php,laravel,Php,Laravel,因此,我想捕获一个异常,它是在我正在使用的API的解析器的函数中产生的: public static function throwHabboAPIException($data) { // Do we find 'maintenance' anywhere? if (strstr($data, 'maintenance')) { throw new MaintenanceException("Hotel API is down for maintenan

因此,我想捕获一个异常,它是在我正在使用的API的解析器的函数中产生的:

public static function throwHabboAPIException($data)
{
    // Do we find 'maintenance' anywhere?
    if (strstr($data, 'maintenance')) {
        throw new MaintenanceException("Hotel API is down for maintenance");
    }

    // Check if data is JSON
    if ($data[0] == "{") { // Quick 'hack' to see if this could be JSON
        $json = json_decode($data, true);
        if (isset($json['errors'])) {
            if ($json['errors'][0]['msg'] == "user.invalid_name") {
                throw new UserInvalidException("The name you supplied appears to be invalid");
            }
            $defaultMessage = $json['errors'][0]['msg'];
        } else if (isset($json['error'])) {
            if (preg_match('#not-found#', $json['error'])) {
                throw new \Exception("We can not find the Habbosssss you're looking for");
            }

        } else {
            $defaultMessage = $json;
        }
    } else {
        $defaultMessage = "An unknown HTML page was returned";
    }

    throw new Exception("Unknown HabboAPI exception occurred: " . $defaultMessage);
}
我试图编写代码来截获异常,但在livewire中抛出了上述异常,但没有捕获

                     public function render()
{
    try{
    $items = training_type::all();
    return view('livewire.trainings', compact('items'));
    }
    catch(\Exception $exception){
        return back()->withError($exception->getMessage())->withInput();

    }
}
以下是异常类:

    namespace HabboAPI\Exceptions;

use Exception;

class HabboNotFoundException extends Exception
{
    public function __construct($message, $code = 0, Exception $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }

    public function __toString()
    {
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
    }
}