在公用工具类写异常类

<?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. Spring Cloud 如何使用Eureka注册服务 4.2.2

    要使用Eureka实现服务发现,需要项目中包含Eureka的服务端发现组件以及客户端发现组件. 搭建Maven父工程 创建一个Maven父工程xcservice-springcloud,并在工程的po ...

  2. mongodb多个条件查询in,日期查询,嵌套查询,统计集合总数等常用实例

    1. 多个条件查询in in db.inventory.find( { qty: { $in: [ 5, 15 ] } } ) 2. 日期查询 db.books.find({}) 查询时间大于6-,结 ...

  3. TroubleShooting经验总结

    1.WinDbg是微软开发的免费源代码级的调试工具.WinDbg可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件.WinDbg对于dump文件的调试可以通过菜单设置Symbol Fi ...

  4. Linux配置Docker镜像加速器

    Docker默认镜像为官方镜像,可以配置成国内加速器提高速度 登录阿里云控制台,搜索容器镜像服务获取到镜像加速服务地址 新建配置文件 /etc/docker/daemon.json 输入以下内容 { ...

  5. springboot maven 收发JSON

    先在pom.xml添加 json 库 <dependency> <groupId>com.alibaba</groupId> <artifactId>f ...

  6. cordon、drain、delete node区别

    cordon.drain.delete node区别 主要目的:导致node处于不可调度状态,新创建的pod容器不会调度在node上. cordon将node置为SchedulingDisabled不 ...

  7. OneNote中更改英文输入默认不是微软雅黑的问题

    win10下的终极版解决方案: 1.进入C:\Windows\Fonts找到Calibri字体,点进去后先右键Calibri常规-属性-安全-高级,将所有者从“TrustedInstaller”更改为 ...

  8. Spring Boot 初学避免犯不必要的错误

    创建项目时的目录问题: 新包体(例如controller)必须和启动文件 DemoApplication.java 在同一级目录下,如下 spring boot 初步使用创建新的项目:https:// ...

  9. top命令常用

    top 使用时的命令: Ctrl+L擦除并且重写屏幕. h或者?显示帮助画面,给出一些简短的命令总结说明. k 终止一个进程.系统将提示用户输入需要终止的进程PID,以及需要发送给该进程什么样的信号. ...

  10. Ubuntu18.04 安装MySQL

    安装 #命令1 sudo apt-get update #命令2 sudo apt-get install mysql-server 初始化 sudo mysql_secure_installatio ...