在公用工具类写异常类

<?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. c# list 使用Where()方法过滤数据

    //根据任务id过滤数据 Func<RfidCodeResultDto, bool> expression = c => c.lineTaskId == _lineTaskId; r ...

  2. Python高级笔记(六)-- property属性【重要】

    1. property属性 目的:简化逻辑流程 class Goods(object): @property def size(self): return 100 obj = Goods() ret ...

  3. build doris 0.11.5 on centos 7/ubuntu

    doris has envolved many thirdparty components since v0.9. so the build progress has changed a lot si ...

  4. C# WinForm获取 当前执行程序路径的几种方法(转)

    1.获取和设置当前目录的完全限定路径. string str = System.Environment.CurrentDirectory; Result: C:xxxxxx 2.获取启动了应用程序的可 ...

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

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

  6. [ kvm ] 学习笔记 4:KVM 高级功能详解

    1. 半虚拟化驱动 1.1 virtio 概述 KVM 是必须使用硬件虚拟化辅助技术(如 Intel VT-x .AMD-V)的 Hypervisor,在CPU 运行效率方面有硬件支持,其效率是比较高 ...

  7. 【Leetcode_easy】872. Leaf-Similar Trees

    problem 872. Leaf-Similar Trees 参考 1. Leetcode_easy_872. Leaf-Similar Trees; 完

  8. Flutter 路由传入中文参数报错无法push问题

    flutter自带路由传递参数和使用第三方库fluro路由传递参数都可以通过一下方式解决问题 String jsonString = json.encode(mapValue); var jsons ...

  9. css height:100%和height:auto的区别

    css height:100%和height:auto的区别 height:auto,是指根据块内内容自动调节高度.height:100%,是指其相对父块高度而定义的高度,也就是按照离它最近且有定义高 ...

  10. 深入理解Flink核心技术及原理

    前言 Apache Flink(下简称Flink)项目是大数据处理领域最近冉冉升起的一颗新星,其不同于其他大数据项目的诸多特性吸引了越来越多人的关注.本文将深入分析Flink的一些关键技术与特性,希望 ...