Php 在Laravel和Livewire中抛出时捕获errorException
因此,我想捕获一个异常,它是在我正在使用的API的解析器的函数中产生的: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
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";
}
}