Я хотел бы зарегистрировать контекст исключения как текущие данные в некоторых переменных. Но обработчик repost(), который я использую для регистрации исключений, не поддерживает параметры контекста. Есть ли другой способ сделать это?
try {
// process the points
$customerPointsService->subtractPointsForRewardReservation($customerReward);
} catch(\Throwable $e) {
report($e);
// I would like to log also some variable values
return $this->sendJsonErrorWithCorrectStatusCode("Customer point subtraction failed.", [], 422);
}
Вы имеете в виду использовать оба? Log::error должно быть достаточно. Не должно быть?
Создайте собственное исключение:
class ContextualException extends \Exception {
protected $context;
public function __construct($message = "", $code = 0, Throwable $previous = null, $context = []) {
parent::__construct($message, $code, $previous);
$this->context = $context;
}
public function getContext() {
return $this->context;
}
}
try {
// If an error occurs
throw new ContextualException("Error message", 0, null, ['variable' => $variableValue]);
} catch (ContextualException $e) {
report($e);
// Access context data
$context = $e->getContext();
// Log context or do something with it
return $this->sendJsonErrorWithCorrectStatusCode("Customer point subtraction failed.", [], 422);
}
чуть выше отчета($e) добавьте Log::error('Не удалось вычесть баллы клиента.', ['customer_reward_id' => $customerReward->id]);