lumen添加自定义异常
在公用工具类写异常类
<?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添加自定义异常的更多相关文章
- 为lumen添加session支持
为lumen添加session支持,同时配置全局函数csrf_token可用 首先laravel和lumen框架的版本要一致,我这里版本都是5.4 1.复制laravel框架config目录下的ses ...
- lumen添加中间件实现cookie自动加密解密
在项目根路径下执行命令:安装illuminate/cookie包 1.composer require illuminate/cookie 2.找到同版本的laravel下的\vendor\larav ...
- lumen 添加配置
app同级目录新建config目录 添加配置文件 bootstrap/app.php里面加载 $app->configure('options');使用 $router->get('/', ...
- SpringBoot------全局异常捕获和自定义异常
1.添加Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...
- 源码剖析Springboot自定义异常
博主看到新服务是封装的自定义异常,准备入手剖析一下,自定义的异常是如何进行抓住我们请求的方法的异常,并进行封装返回到.废话不多说,先看看如何才能实现封装异常,先来一个示例: @ControllerAd ...
- 白话SpringCloud | 第十章:路由网关(Zuul)进阶:过滤器、异常处理
前言 简单介绍了关于Zuul的一些简单使用以及一些路由规则的简单说明.而对于一个统一网关而言,需要处理各种各类的请求,对不同的url进行拦截,或者对调用服务的异常进行二次处理等等.今天,我们就来了解下 ...
- SpringCloud学习系列之七 ----- Zuul路由网关的过滤器和异常处理
前言 在上篇中介绍了SpringCloud Zuul路由网关的基本使用版本,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的路由 ...
- Spring Security Oauth2 自定义 OAuth2 Exception
付出就要得到回报,这种想法是错的. 前言 在使用Spring Security Oauth2登录和鉴权失败时,默认返回的异常信息如下 { "error": "unauth ...
- 14 微服务电商【黑马乐优商城】:day06-使用nginx反向代理并掌握cors解决跨域
本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...
随机推荐
- ByteBuffer使用实例
ByteBuffer作为JDK的字节流处理对象,这里举个小例子说明下用法,直接上代码: package com.wlf.netty.nettyserver; import org.junit.Asse ...
- Spring cloud微服务安全实战-6-8sentinel限流实战
阿里2018年开源的. 简单来说就是干三件事,最终的结果就是保证你的服务可用,不会崩掉.保证服务高可用. 流控 先从最简单的场景来入手. 1.引用一个依赖, 2,声明一个资源. 3.声明一个规则 注意 ...
- LeetCode_404. Sum of Left Leaves
404. Sum of Left Leaves Easy Find the sum of all left leaves in a given binary tree. Example: 3 / \ ...
- .Net MVC 标签页
目录 Bootstrap的标签页 适合.Net MVC的标签页 Bootstrap的标签页 下面是Bootstrap的标签页,挺好的,但是用的id,内容是固定的?我不知道怎么变成不同的视图来 < ...
- LODOP注册语句相关简短问答
注册和角色相关博文:LODOP.C-LODOP注册号的区别.Lodop客户端本地角色注册号常见误区.Lodop.c-lodop注册与角色简短问答.LODOP和C-LODOP注册与角色等简短问答[增强版 ...
- Nodejs Client for FastDFS
FastDFS 是分布式文件存储系统.这个项目是FastDFS的NodeJS客户端,用来与FastDFS Server进行交互,进行文件的相关操作.我测试过的server版本是4.0.6. githu ...
- Nginx虚拟目录(alias)和根目录(root)
功能要求: 假设nginx配置的域名是www.kazihuo.com,现有静态资源/home/www/oye目录需要通过nginx访问. 功能实现: 前提要求: 1.在nginx.conf中到处第二行 ...
- Python机器学习基础教程-第1章-鸢尾花的例子KNN
前言 本系列教程基本就是摘抄<Python机器学习基础教程>中的例子内容. 为了便于跟踪和学习,本系列教程在Github上提供了jupyter notebook 版本: Github仓库: ...
- php设计模式;抽象类、抽象方法
设计模式 什么叫设计模式 所谓设计模式,就是一些解决问题的“常规做法”,是一种认为较好的经验总结.面对不同的问题,可能会有不同的解决办法,此时就可以称为不同的设计模式. 工厂模式 在实际应用中,我们总 ...
- js控制数量包含截取
<div class="usermes_index_line"> 进行中的单 <div id="usermes_index_line_i2"& ...