You can do it:
create exception class
class APIException extends Exception{ }
then throw it off the controller
throw new APIException('api exception');
and catch it from Exceptions / Handler.php
public function render($request, Exception $e) { if ($e instanceof APIException){ return response(['success' => false, 'data' => [], 'message' => $e->getMessage(), 401); } if ($e instanceof SomeException){ return response(['success' => false, 'data' => [], 'message' => 'Exception'], 401); } return parent::render($request, $e); }
source share