在公用工具类写异常类

<?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. Dart中的类型转换总结:

    1.Dart中数组转换为字符串:join var a=[1,2,3,4]; var str=a.join(',');

  2. datax实例——全量、增量同步

    一.全量同步 本文以mysql -> mysql为示例: 本次测试的表为mysql的系统库-sakila中的actor表,由于不支持目的端自动建表,此处预先建立目的表: CREATE TABLE ...

  3. Linux下Mycat安装配置和使用

    mysql安装下载mysql[百度云]tar -zxvf mysql-5.6.32-linux-glibc2.5-x86_64.tar.gz 解压把mysql文件夹移动到 /usr/local/ 下m ...

  4. Zabbix 3.2.6-Mysql多实例监控-Percona Monitoring Plugins自动发现

    mysql多实例监控实录   系统环境: cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 内核版本: uname -r 3.1 ...

  5. C#中Invoke的用法(转)

    invoke和begininvoke 区别 一直对invoke和begininvoke的使用和概念比较混乱,这两天看了些资料,对这两个的用法和原理有了些新的认识和理解. 首先说下,invoke和beg ...

  6. EasyRTMP结合海康HCNetSDK获取海康摄像机H.264实时流并转化成为RTMP直播推流(附源码)

    最近一家深耕于南方电网的科技公司同事找到我们,咨询关于调用海康HCNetSDK取流,并进行互联网转化的方案,经过反复的沟通以及自身在EasyDSS和EasyNVR方面的经验,我们推荐了海康HCNetS ...

  7. 【ML】京东人工智能设计神器「羚珑」

    https://www.uisdc.com/linglong 文后的彩蛋说的特别好,让设计师发挥更高阶的价值.

  8. (二十七)JVM类加载器机制与类加载过程

    一.Java虚拟机启动.加载类过程分析 下面我将定义一个非常简单的java程序并运行它,来逐步分析java虚拟机启动的过程. package org.luanlouis.jvm.load; impor ...

  9. APP排查内存泄漏最简单和直观的方法

        内存泄漏无疑会严重影响用户体验,一些本应该废弃的资源和对象无法被释放,导致手机内存的浪费,app使用的卡顿,那么如何排查内存泄漏呢? 当然,首先我们有google的官方文档可以参考,大部分博客 ...

  10. Xmanager教程

    简介 Xmanager是市场上领先的PC X服务器,可将X应用程序的强大功能带入Windows环境. 提供了强大的会话管理控制台,易于使用的X应用程序启动器,X服务器配置文件管理工具,SSH模块和高性 ...