<?php

namespace App\Exceptions;

use Illuminate\Http\JsonResponse;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;

class Handler extends ExceptionHandler {
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];

    public function report(Throwable $exception) {
        parent::report($exception);
    }

    public function render($request, Throwable $exception) {
        $rendered = parent::render($request, $exception);

        if($rendered instanceof JsonResponse) {
            return $rendered;
        }

        if(config('app.debug') === false) {
            $message = $exception instanceof HttpException ? $exception->getMessage() : 'Server Error';
        } else {
            $message = $exception->getMessage();
        }

        return response()->json([
            'error' => $rendered->getStatusCode(),
            'message' => $message,
        ], $rendered->getStatusCode());
    }
}
