Skip to content

Instantly share code, notes, and snippets.

@Oranzh
Forked from joseluisq/1README.md
Created June 9, 2021 01:21
Show Gist options
  • Select an option

  • Save Oranzh/e872be15afb32fd82979f4ab21cc2a3a to your computer and use it in GitHub Desktop.

Select an option

Save Oranzh/e872be15afb32fd82979f4ab21cc2a3a to your computer and use it in GitHub Desktop.

Revisions

  1. @joseluisq joseluisq revised this gist Aug 28, 2018. No changes.
  2. @joseluisq joseluisq revised this gist Aug 28, 2018. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions Handler.php
    Original file line number Diff line number Diff line change
    @@ -53,13 +53,10 @@ public function render($request, Exception $e)
    return parent::render($request, $e);
    }

    $success = false;
    $response = null;
    $status = Response::HTTP_INTERNAL_SERVER_ERROR;

    if ($e instanceof HttpResponseException) {
    $status = Response::HTTP_INTERNAL_SERVER_ERROR;
    $response = $e->getResponse();
    } elseif ($e instanceof MethodNotAllowedHttpException) {
    $status = Response::HTTP_METHOD_NOT_ALLOWED;
    $e = new MethodNotAllowedHttpException([], 'HTTP_METHOD_NOT_ALLOWED', $e);
    @@ -72,13 +69,12 @@ public function render($request, Exception $e)
    } elseif ($e instanceof \Dotenv\Exception\ValidationException && $e->getResponse()) {
    $status = Response::HTTP_BAD_REQUEST;
    $e = new \Dotenv\Exception\ValidationException('HTTP_BAD_REQUEST', $status, $e);
    $response = $e->getResponse();
    } elseif ($e) {
    $e = new HttpException($status, 'HTTP_INTERNAL_SERVER_ERROR');
    }

    return response()->json([
    'success' => $success,
    'success' => false,
    'status' => $status,
    'message' => $e->getMessage()
    ], $status);
  3. @joseluisq joseluisq revised this gist Apr 18, 2017. 2 changed files with 2 additions and 9 deletions.
    File renamed without changes.
    11 changes: 2 additions & 9 deletions Handler.php
    Original file line number Diff line number Diff line change
    @@ -53,34 +53,27 @@ public function render($request, Exception $e)
    return parent::render($request, $e);
    }

    $success = true;
    $status = Response::HTTP_OK;
    $success = false;
    $response = null;
    $status = Response::HTTP_INTERNAL_SERVER_ERROR;

    if ($e instanceof HttpResponseException) {
    $success = false;
    $status = Response::HTTP_INTERNAL_SERVER_ERROR;
    $response = $e->getResponse();
    } elseif ($e instanceof MethodNotAllowedHttpException) {
    $success = false;
    $status = Response::HTTP_METHOD_NOT_ALLOWED;
    $e = new MethodNotAllowedHttpException([], 'HTTP_METHOD_NOT_ALLOWED', $e);
    } elseif ($e instanceof NotFoundHttpException) {
    $success = false;
    $status = Response::HTTP_NOT_FOUND;
    $e = new NotFoundHttpException('HTTP_NOT_FOUND', $e);
    } elseif ($e instanceof AuthorizationException) {
    $success = false;
    $status = Response::HTTP_FORBIDDEN;
    $e = new AuthorizationException('HTTP_FORBIDDEN', $status);
    } elseif ($e instanceof \Dotenv\Exception\ValidationException && $e->getResponse()) {
    $success = false;
    $status = Response::HTTP_BAD_REQUEST;
    $e = new \Dotenv\Exception\ValidationException('HTTP_BAD_REQUEST', $status, $e);
    $response = $e->getResponse();
    } elseif ($e) {
    $success = false;
    $status = Response::HTTP_INTERNAL_SERVER_ERROR;
    $e = new HttpException($status, 'HTTP_INTERNAL_SERVER_ERROR');
    }

  4. @joseluisq joseluisq revised this gist Apr 18, 2017. 2 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion Handler.php
    Original file line number Diff line number Diff line change
    @@ -53,8 +53,8 @@ public function render($request, Exception $e)
    return parent::render($request, $e);
    }

    $status = 200;
    $success = true;
    $status = Response::HTTP_OK;
    $response = null;

    if ($e instanceof HttpResponseException) {
  5. @joseluisq joseluisq revised this gist Apr 18, 2017. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions Handler.php
    Original file line number Diff line number Diff line change
    @@ -54,32 +54,32 @@ public function render($request, Exception $e)
    }

    $status = 200;
    $success = TRUE;
    $response = NULL;
    $success = true;
    $response = null;

    if ($e instanceof HttpResponseException) {
    $success = FALSE;
    $success = false;
    $status = Response::HTTP_INTERNAL_SERVER_ERROR;
    $response = $e->getResponse();
    } elseif ($e instanceof MethodNotAllowedHttpException) {
    $success = FALSE;
    $success = false;
    $status = Response::HTTP_METHOD_NOT_ALLOWED;
    $e = new MethodNotAllowedHttpException([], 'HTTP_METHOD_NOT_ALLOWED', $e);
    } elseif ($e instanceof NotFoundHttpException) {
    $success = FALSE;
    $success = false;
    $status = Response::HTTP_NOT_FOUND;
    $e = new NotFoundHttpException('HTTP_NOT_FOUND', $e);
    } elseif ($e instanceof AuthorizationException) {
    $success = FALSE;
    $success = false;
    $status = Response::HTTP_FORBIDDEN;
    $e = new AuthorizationException('HTTP_FORBIDDEN', $status);
    } elseif ($e instanceof \Dotenv\Exception\ValidationException && $e->getResponse()) {
    $success = FALSE;
    $success = false;
    $status = Response::HTTP_BAD_REQUEST;
    $e = new \Dotenv\Exception\ValidationException('HTTP_BAD_REQUEST', $status, $e);
    $response = $e->getResponse();
    } elseif ($e) {
    $success = FALSE;
    $success = false;
    $status = Response::HTTP_INTERNAL_SERVER_ERROR;
    $e = new HttpException($status, 'HTTP_INTERNAL_SERVER_ERROR');
    }
  6. @joseluisq joseluisq revised this gist Apr 13, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Lumen 5 HTTP Exceptions Handler with JSON support.
    # Lumen 5 HTTP Exception Handlers with JSON support.

    > Configure [PHP Lumen 5](https://lumen.laravel.com/) HTTP Exceptions Handler with common JSON responses.
    > Configure [PHP Lumen 5](https://lumen.laravel.com/) HTTP Exception Handlers with common JSON responses.
    ![image](https://cloud.githubusercontent.com/assets/1700322/24999979/e1be8ec4-203f-11e7-916c-46368d680aab.png)

  7. @joseluisq joseluisq revised this gist Apr 13, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Lumen 5 Exceptions Handler with JSON support.
    # Lumen 5 HTTP Exceptions Handler with JSON support.

    > Configure [PHP Lumen 5](https://lumen.laravel.com/) Exceptions Handler with JSON responses.
    > Configure [PHP Lumen 5](https://lumen.laravel.com/) HTTP Exceptions Handler with common JSON responses.
    ![image](https://cloud.githubusercontent.com/assets/1700322/24999979/e1be8ec4-203f-11e7-916c-46368d680aab.png)

  8. @joseluisq joseluisq revised this gist Apr 13, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,3 +7,5 @@
    ### Setup

    Copy (replace) only the attached files to their respective directories. `app/Exceptions/Handler.php` and `app/Http/Middleware/Authenticate.php`

    __Tip:__ Via your `.env` file you can handle the visibility of the HTTP Exception responses. `APP_DEBUG=false` (for display json exception responses in production)
  9. @joseluisq joseluisq revised this gist Apr 13, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,4 @@

    ### Setup

    Copy (replace) only the attached files to their respective directories.
    Copy (replace) only the attached files to their respective directories. `app/Exceptions/Handler.php` and `app/Http/Middleware/Authenticate.php`
  10. @joseluisq joseluisq renamed this gist Apr 13, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. @joseluisq joseluisq created this gist Apr 13, 2017.
    49 changes: 49 additions & 0 deletions Authenticate.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    <?php

    namespace App\Http\Middleware;

    use Closure;
    use Illuminate\Contracts\Auth\Factory as Auth;

    class Authenticate
    {
    /**
    * The authentication guard factory instance.
    *
    * @var \Illuminate\Contracts\Auth\Factory
    */
    protected $auth;

    /**
    * Create a new middleware instance.
    *
    * @param \Illuminate\Contracts\Auth\Factory $auth
    * @return void
    */
    public function __construct(Auth $auth)
    {
    $this->auth = $auth;
    }

    /**
    * Handle an incoming request.
    *
    * @param \Illuminate\Http\Request $request
    * @param \Closure $next
    * @param string|null $guard
    * @return mixed
    */
    public function handle($request, Closure $next, $guard = null)
    {
    if ($this->auth->guard($guard)->guest()) {
    return response()
    ->json([
    'success' => false,
    'status' => 401,
    'message' => 'HTTP_UNAUTHORIZED'
    ], 401);
    }

    return $next($request);
    }
    }
    9 changes: 9 additions & 0 deletions Handler.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Lumen 5 Exceptions Handler with JSON support.

    > Configure [PHP Lumen 5](https://lumen.laravel.com/) Exceptions Handler with JSON responses.
    ![image](https://cloud.githubusercontent.com/assets/1700322/24999979/e1be8ec4-203f-11e7-916c-46368d680aab.png)

    ### Setup

    Copy (replace) only the attached files to their respective directories.
    93 changes: 93 additions & 0 deletions Handler.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    <?php

    namespace App\Exceptions;

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

    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
    */
    public function render($request, Exception $e)
    {
    if (env('APP_DEBUG')) {
    return parent::render($request, $e);
    }

    $status = 200;
    $success = TRUE;
    $response = NULL;

    if ($e instanceof HttpResponseException) {
    $success = FALSE;
    $status = Response::HTTP_INTERNAL_SERVER_ERROR;
    $response = $e->getResponse();
    } elseif ($e instanceof MethodNotAllowedHttpException) {
    $success = FALSE;
    $status = Response::HTTP_METHOD_NOT_ALLOWED;
    $e = new MethodNotAllowedHttpException([], 'HTTP_METHOD_NOT_ALLOWED', $e);
    } elseif ($e instanceof NotFoundHttpException) {
    $success = FALSE;
    $status = Response::HTTP_NOT_FOUND;
    $e = new NotFoundHttpException('HTTP_NOT_FOUND', $e);
    } elseif ($e instanceof AuthorizationException) {
    $success = FALSE;
    $status = Response::HTTP_FORBIDDEN;
    $e = new AuthorizationException('HTTP_FORBIDDEN', $status);
    } elseif ($e instanceof \Dotenv\Exception\ValidationException && $e->getResponse()) {
    $success = FALSE;
    $status = Response::HTTP_BAD_REQUEST;
    $e = new \Dotenv\Exception\ValidationException('HTTP_BAD_REQUEST', $status, $e);
    $response = $e->getResponse();
    } elseif ($e) {
    $success = FALSE;
    $status = Response::HTTP_INTERNAL_SERVER_ERROR;
    $e = new HttpException($status, 'HTTP_INTERNAL_SERVER_ERROR');
    }

    return response()->json([
    'success' => $success,
    'status' => $status,
    'message' => $e->getMessage()
    ], $status);
    }
    }