robrogers3/laravel-jsonaware-exception-handler
Composer 安装命令:
composer require robrogers3/laravel-jsonaware-exception-handler
包简介
Laravel Json Aware Exception Handler
README 文档
README
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.
$ composer require robrogers3/laravel-json-aware-exception-handler
Configuration
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 use RobRogers3\LaravelExceptionHandler\JsonAwareExceptionHandler; class Handler extends JsonAwareExceptionHandler { }
Last Step
You need to run this artisan command:
$ artisan vendor:publish
This will copy the exception messages to your local lang directory.
Optional Setup.
Open the exceptionmessages.php and change the messages you want to show for different http status codes.
You may want to change 401 to something more clever or more corporate. It's up to you.
Usage
Server Side Usage
There really is only one thing to use: Taking advantage of the MessagingException::class
Throwing this with a custom message allows you to display something detailed or specific to the situtation.
Client Side Usage with Ajax
Here's where you want to take advantage of this.
Take a look at this handling of an ajax response error.
handle (error) { if (error.response.data) { //the error message returned by the json response corresponds to the error.response.data property //Usually it's a string if (typeof error.response.data == 'string') { return alert(error.response.data); } //validation errors are an array if (error.response.status == 422 && error.response.data.length) { let errors = []; error.response.data.forEach(datum => { 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'); } }
robrogers3/laravel-jsonaware-exception-handler 适用场景与选型建议
robrogers3/laravel-jsonaware-exception-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 6, 最近一次更新时间为 2017 年 07 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「error handler」 「exception handler」 「ajax」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 robrogers3/laravel-jsonaware-exception-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 robrogers3/laravel-jsonaware-exception-handler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 robrogers3/laravel-jsonaware-exception-handler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Lazy loading for middleware and request handlers
Kinikit - PHP Application development framework MVC component
Set Links with a specific language parameter
ext-json wrapper with sane defaults
Bootstraps errors and handles them via reporters and renderers
A package to cast json fields, each sub-keys is castable
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-28