Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/robrogers3/laravel-jsonaware-exception-handler
Provide Meaningful Ajax error messages for your Laravel Project.
https://github.com/robrogers3/laravel-jsonaware-exception-handler
ajax custom error-handling exception-handler javascript json laravel laravel-exception-handler service-provider
Last synced: about 1 month ago
JSON representation
Provide Meaningful Ajax error messages for your Laravel Project.
- Host: GitHub
- URL: https://github.com/robrogers3/laravel-jsonaware-exception-handler
- Owner: robrogers3
- License: mit
- Created: 2017-07-27T11:41:51.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-02T20:52:38.000Z (over 6 years ago)
- Last Synced: 2024-05-16T01:02:39.939Z (7 months ago)
- Topics: ajax, custom, error-handling, exception-handler, javascript, json, laravel, laravel-exception-handler, service-provider
- Language: PHP
- Homepage:
- Size: 43.9 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: license.md
Awesome Lists containing this project
README
[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Total Downloads][ico-downloads]][link-downloads]
[![Build Status][ico-travis]][link-travis]# Laravel Json Aware Exception Handler
Every one likes cool error message pages. Github has an awesome 404.
And it's very easy to create your own custom error pages for html responses.
But doing this for Ajax and Json responses meant either doing something generic or figuring out your own solution.
Often something like returning an error message like 'Sorry we cant handle your request'. Nothing informative.This package solves this problem.
Once it's installed. Tune your error messages for over a dozen possible error codes.
You can even add more. Just create a ZizzZazzException assign a status code to it, and a custom message. Done!## Installation
Install Laravel Json Aware Exception Handler with Composer.
```bash
$ composer require robrogers3/laravel-json-aware-exception-handler
```## Configuration
```php
RobRogers3\LaravelExceptionHandler\ServiceProvider::class,
```### Using it:
Option 1: Update your .env file by adding this line:
USE_JSON_EXCEPTION_HANDLER=true
This will use the JsonHandler for json requests and the Laravel Exception Handler for regular requests.
Note: it will completely ignore your app's Exception Handler. This means you can't override anything in this class.
Option 2: Update your App Handler class to extend the JsonAwareExceptionHandler
You do not have to update your .env file.
The benefit of this is you can overide how the JsonAwareExceptionHandler
To do this you need to change your `App\Exceptions\Handler` class to extend `RobRogers3\LaravelExceptionHandler\JsonAwareExceptionHandler` rather than extending `Illuminate\Foundation\Exceptions\Handler`. Like so:
```php
{
errors.push(datum);
});
return alert(errors.join("\n"));
}//used to handle a teapot message 418
//can be a random exception you throw as say MessagingException
//you can tweek your teapot messages to have an extra 'message' property. Up to you.
//by default it doesn't
if (error.response.status == 418 && error.response.data.message) {
return alert(error.response.data);
}
}
return alert('We could not handle your request');
}
}
```[ico-version]: https://img.shields.io/packagist/v/robrogers3/laravel-jsonaware-exception-handler.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/robrogers3/laravel-jsonaware-exception-handler/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/robrogers3/laraldap-auth.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/robrogers3/laraldap-auth.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/robrogers3/laravel-jsonaware-exception-handler.svg?style=flat-square[link-packagist]: https://packagist.org/packages/robrogers3/laravel-jsonaware-exception-handler
[link-travis]: https://travis-ci.org/robrogers3/laravel-jsonaware-exception-handler
[link-scrutinizer]: https://scrutinizer-ci.com/g/robrogers3/laradauth/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/robrogers3/laraldap-auth
[link-downloads]: https://packagist.org/packages/robrogers3/laravel-jsonaware-exception-handler
[link-author]: https://github.com/robrogers3
[link-contributors]: ../../contributors