在公用工具类写异常类

<?php

namespace Brady\Tool\Exception;

use Brady\Tool\Constant\ErrorMsg;
use \Exception; class ExceptionResult extends Exception
{
const DEFAULT_CODE = 500; protected static $messageTemplate = []; public static function setMsgTemplate(array $template = [])
{
static::$messageTemplate = $template;
} public static function getMsgTemplate()
{
return static::$messageTemplate;
} public function errorMsg()
{
return "异常信息:文件-".$this->getFile()." 行号-".$this->getLine()."错误码-".$this->getCode()." 信息-".$this->getMessage();
} /**
* @param $code
* @param bool $isChinese 是否直接抛出中文
*/
public static function throwException($code,$isChinese = false)
{
$class = __CLASS__;
if($isChinese){
throw new $class($code,500);
} else { //根据code获取msg
$msg = static::getMsg($code);
throw new $class($msg,$code);
} } public static function getMsg($code)
{
$template = self::getMsgTemplate(); if(isset($template[$code])){
return $template[$code];
} else {
$class = __CLASS__;
throw new $class("错误码未设置".$code);
} } }

lumen bootstrp的app里面注册服务提供者

$app->register(App\Providers\ExceptionServiceProvider::class);

providers文件夹下新建文件
<?php
/**
* Created by PhpStorm.
* User: costa
* Date: 2019/3/15
* Time: 14:12
*/ namespace App\Providers; use Brady\Tool\Exception\ExceptionResult;
use Illuminate\Support\ServiceProvider; class ExceptionServiceProvider extends ServiceProvider
{
/**
* 注册编码信息
*/
public function register()
{
$tpl = require (dirname(__DIR__) . '/Constants/ErrorMsg.php');
ExceptionResult::setMsgTemplate($tpl);
}
}

app目录下新建目录 Constants

分别存放错误码和错误信息

ErrorCode.php

<?php
/**
* Created by PhpStorm.
* User: costa
* Date: 2019/3/15
* Time: 14:23
*/ namespace App\Constants; class ErrorCode
{
/**
* 基本错误码
*/
const SUCCESS = 200;
const UNAUTHORIZED = 401;
const FAIL = 500; }

ErrorMsg.php

<?php
/**
* Created by PhpStorm.
* User: costa
* Date: 2019/3/15
* Time: 14:22
*/ namespace App\Constants; return [
ErrorCode::SUCCESS => '成功' ,
ErrorCode::UNAUTHORIZED => '暂无权限!' ,
ErrorCode::FAIL => '失败' , ];

使用 在控制器里面

<?php

namespace App\Service;

//服务层
use App\Constants\ErrorCode;
use App\Repository\UserRepository;
use Brady\Tool\Exception\ExceptionResult;
use Mockery\Exception; class UserService
{
public $userRepository; public function __construct()
{
$this->userRepository = new UserRepository();
} public function queryUserList($where)
{
$where = ['name'=>"brady"];
return $this->userRepository->getList($where);
} public function getUserInfo($id)
{
try{
ExceptionResult::throwException(ErrorCode::UNAUTHORIZED); return $this->userRepository->getById($id); } catch (\Exception $e){
var_dump($e->getMessage());
} } }

lumen添加自定义异常的更多相关文章

  1. 为lumen添加session支持

    为lumen添加session支持,同时配置全局函数csrf_token可用 首先laravel和lumen框架的版本要一致,我这里版本都是5.4 1.复制laravel框架config目录下的ses ...

  2. lumen添加中间件实现cookie自动加密解密

    在项目根路径下执行命令:安装illuminate/cookie包 1.composer require illuminate/cookie 2.找到同版本的laravel下的\vendor\larav ...

  3. lumen 添加配置

    app同级目录新建config目录 添加配置文件 bootstrap/app.php里面加载 $app->configure('options');使用 $router->get('/', ...

  4. SpringBoot------全局异常捕获和自定义异常

    1.添加Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...

  5. 源码剖析Springboot自定义异常

    博主看到新服务是封装的自定义异常,准备入手剖析一下,自定义的异常是如何进行抓住我们请求的方法的异常,并进行封装返回到.废话不多说,先看看如何才能实现封装异常,先来一个示例: @ControllerAd ...

  6. 白话SpringCloud | 第十章:路由网关(Zuul)进阶:过滤器、异常处理

    前言 简单介绍了关于Zuul的一些简单使用以及一些路由规则的简单说明.而对于一个统一网关而言,需要处理各种各类的请求,对不同的url进行拦截,或者对调用服务的异常进行二次处理等等.今天,我们就来了解下 ...

  7. SpringCloud学习系列之七 ----- Zuul路由网关的过滤器和异常处理

    前言 在上篇中介绍了SpringCloud Zuul路由网关的基本使用版本,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的路由 ...

  8. Spring Security Oauth2 自定义 OAuth2 Exception

    付出就要得到回报,这种想法是错的. 前言 在使用Spring Security Oauth2登录和鉴权失败时,默认返回的异常信息如下 { "error": "unauth ...

  9. 14 微服务电商【黑马乐优商城】:day06-使用nginx反向代理并掌握cors解决跨域

    本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...

随机推荐

  1. 基于keras的triplet_loss

    https://blog.csdn.net/yjy728/article/details/79570554 https://blog.csdn.net/yjy728/article/details/7 ...

  2. ABAP函数篇2 测试DATE_CONVERT_TO_FACTORYDATE

    DATE_CONVERT_TO_FACTORYDATE   根据日期返回工厂日历日期 函数功能说明: 标出工作日的计算方法 输入传输 CORRECT_OPTION = '+'如果指定的日期不是工作日, ...

  3. 基于ADO的远程Oracle连接

    最近在一个通过MFC做一个界面,通过这个界面可以对布置在另一台服务器上的数据库MySQL.SQl Server.Oracle进行增删创建表的操作.其中我通过ADO很快就完成了对MySQL和SQL Se ...

  4. 原生JavaScript判断浏览器对CSS属性是否支持

    /*判断浏览器是否支持某个css属性*/ function SupportCss(attrName){ var i=0, arr = SupportCss.opt.aBrowser, eleStyle ...

  5. 关于STM32F405的GPIO中断问题

    1. 下面的图,应该是多个引脚中断挂在同一个中断号上面,也就是PA0和PB0同时挂在一个中断源上面,那么就是说只能同时使用其中一个 寄存器的配置,确实只能有一个使用

  6. SpringBoot学习笔记:动态数据源切换

    SpringBoot学习笔记:动态数据源切换 数据源 Java的javax.sql.DataSource接口提供了一种处理数据库连接的标准方法.通常,DataSource使用URL和一些凭据来建立数据 ...

  7. PS更换证件照背景颜色

    同学们大家好,我是阿宝老师,今天给大家讲一下如何使用PS更换证件照背景色. 目前使用PS更换证件照底片有三种方式,这三种方式虽有不同,但是最终目的都是将人像从背景中抠出来.扣取人像有三种方法可供选取, ...

  8. 洛谷 题解 P1550 【[USACO08OCT]打井Watering Hole】

    本题看似很难,实际上思路非常简单--如果你想通了. 首先有一个问题:图中有几个点?大部分的人会回答\(n\)个点.错了,有\(n+1\)个. 多出来的那个点在哪?关键在于你要理解每一个决策的意义.实际 ...

  9. linux CentOS7 安装字体库-转

    前言 报表中发现有中文乱码和中文字体不整齐(重叠)的情况,首先考虑的就是操作系统是否有中文字体,在CentOS 7中发现输入命令查看字体列表是提示命令无效: 如上图可以看出,不仅没有中文字体,连字体库 ...

  10. LeetCode 179. 最大数(Largest Number) 21

    179. 最大数 179. Largest Number 题目描述 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 每日一算法2019/5/24Day 21LeetCode179. La ...