ApiException.php 532 B

123456789101112131415161718192021222324252627
  1. <?php
  2. namespace App\Exceptions;
  3. use Exception;
  4. use Illuminate\Http\Exceptions\HttpResponseException;
  5. class ApiException extends HttpResponseException
  6. {
  7. public function __construct($code = 422, $message="Validation error", $errors=[])
  8. {
  9. $data=[
  10. 'error'=>[
  11. "code"=>$code,
  12. "message"=>$message
  13. ]
  14. ];
  15. if ($errors) {
  16. $data["error"]["errors"] = $errors;
  17. }
  18. parent::__construct(response()->json($data, $code));
  19. }
  20. }