<?php

namespace App\Exceptions;

use Exception;
use PDOException;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    //tractament de les diferents exepcions
    public function render($request, Exception $e)
    {
        if($e instanceof PDOException)
        {
            return response()->view('errors.pdoexception');
        }
        switch ($e->getStatusCode())
        {
            case 403: /* permission denied */
                return response()->view('errors.403', [], 403);

            case 404: /* not found */
                return response()->view('errors.404', [], 404);

            case 503: /* internal error */
                return response()->view('errors.503', [], 503);

            default:
                return response()->view('errors.503', [], 503);
        }

        return parent::render($request, $e);
    }
}
