@brook
Для создания кастомного исключения в Laravel нужно выполнить следующие шаги:
Пример:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
namespace AppExceptions; use Exception; class CustomException extends Exception { public function __construct(string $message = "", int $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } public function getDescription() { return 'This is a custom exception.'; } public function getErrorCode() { return 500; } public function render() { return response()->json([ 'message' => $this->getMessage(), 'description' => $this->getDescription(), ], $this->getErrorCode()); } } |
После этого вы можете использовать этот класс в своем коде для выброса кастомных исключений:
1
|
throw new CustomException('Something went wrong.'); |