ibnudirsan/handle-http-api

ini adalah package untuk meng-handle http request API La

1.0.0 2022-09-26 13:48 UTC

This package is auto-updated.

Last update: 2024-10-27 14:13:44 UTC


README

Latest Version on Packagist Size Code on Packagist issues on Packagist follower on Packagist discussions on Packagist commit on Packagist

Laravel package untuk Handler Response http request API.

Cara menggunakannya :

install Package composer require ibnudirsan/handle-http-api

Untuk menghandle http code pada API bisa gunakan function ini :

// app/Exceptions/Handler.php

<?php

namespace App\Exceptions;

use Throwable;
use Ibnudirsan\HandleHttpApi\Handler\HttpResponse;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Throwable
     */
    public function render($request, Throwable $exception) {

        if ($request->is('api/*')) {
            $code = method_exists($exception, 'getStatusCode');
            if (method_exists($exception, 'getStatusCode')) {
                $statusCode = $this->prepareException($exception)->getStatusCode();
                return HttpResponse::Status($statusCode);
            } else {
                $statusCode = 500;
                    return HttpResponse::Status($statusCode);
            }
                return parent::render($request, $exception);
        }

    }
}