File "Handler.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Exceptions/Handler.php
File size: 2.49 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Exceptions;

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Queue\Middleware\ThrottlesExceptions;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array<int, class-string<Throwable>>
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }

    public function render($request, \Exception|Throwable $e)
    {
        if($e instanceof ModelNotFoundException)
        {
            if ($request->expectsJson()){
                return response()->json([
                    'message' => "Record not found.",
                    'errors' => ["error" => ["Record not found."]]
                ], Response::HTTP_NOT_FOUND);
            }else{
                abort(Response::HTTP_NOT_FOUND);
            }
        } else if ($e instanceof NotFoundHttpException){
            if ($request->expectsJson()){
                return response()->json([
                    'message' => "Invalid URL.",
                    'errors' => ["error" => ["Invalid URL."]]
                ], Response::HTTP_NOT_FOUND);
            }else{
                abort(Response::HTTP_NOT_FOUND);
            }
        } else if ($e instanceof ThrottleRequestsException){
            if ($request->expectsJson()){
                return response()->json([
                    'message' => "Please wait before retrying.",
                    'errors' => ["error" => ["Too many attempts."]]
                ], Response::HTTP_TOO_MANY_REQUESTS);
            }else{
                abort(Response::HTTP_TOO_MANY_REQUESTS);
            }
        }
        return parent::render($request, $e);
    }

}