Coder Social home page Coder Social logo

laravel-exception-handler's Introduction

Laravel Exception Handler

Latest Version Total Downloads License

Introduction

Laravel API Exception Handler with readable error messages. However, this package is specifically designed to be compatible with older PHP versions and Laravel or Lumen legacy projects.

Installation

composer require ohansyah/laravel-exception-handler

Register Service Provider

$app->register(Ohansyah\ExceptionHandler\ExceptionHandlerServiceProvider::class);

Register Config

$app->configure('exception-handler');

Refreshing

composer dump-autoload

Publish Config and Lang

php artisan exception-handler:publish

Usage

use the Traits and simpy using available method resException(), resError(), resSuccess()

use Ohansyah\ExceptionHandler\Traits\ResponseTransform;

class Name 
{
    use ResponseTransform;
    ...
    resException(Exception $exception);
    resError($responseMessage, int $responseCode = 400, array $additionals = []);
    resSuccess($responseMessage, int $responseCode = 200);
    resThrowable(Throwable $throwable);
}

Global Exception Handler

// app\Exceptions\Handler.php
if (!($exception instanceof ValidationException) && !($exception instanceof NotFoundHttpException)) {
    return $this->resException($exception);
}

Sample Usage

Request:
GET {{your-base-url}}/exception-handler/error?code=500

Response:
{
    "status": "error",
    "message": "Kami telah menerima laporan bahwa layanan ini sedang mengalami gangguan. Mohon maaf atas ketidaknyamanannya dan mohon menunggu sebentar.",
    "errors": {
        "message": [
            "Kami telah menerima laporan bahwa layanan ini sedang mengalami gangguan. Mohon maaf atas ketidaknyamanannya dan mohon menunggu sebentar."
        ]
    },
    "title": "Ups!! Terjadi Kendala Teknis",
    "exception": null
}

Translation

resources\lang\en\http.php
resources\lang\id\http.php

Config

// config\exception-handler.php

'trace_exception' => env('SERVICE_TRACE_EXCEPTION', true),

License

The Laravel Exception Handler package is open-sourced software licensed under the MIT License.

laravel-exception-handler's People

Contributors

ohansyah avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.