修改全局变量 增加全局异常变量
parent
c82c902cfc
commit
14255d650e
@ -0,0 +1,27 @@
|
||||
package com.hchbox.controller;
|
||||
|
||||
import com.hchbox.model.param.CommonResult;
|
||||
import com.hchbox.service.TaoBaoShopService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2021/7/26
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/source")
|
||||
@RequiredArgsConstructor
|
||||
public class TestController {
|
||||
|
||||
private final TaoBaoShopService taoBaoShopService;
|
||||
|
||||
@GetMapping("/info")
|
||||
public CommonResult<String> saveTemplate() {
|
||||
return new CommonResult<String>().success(taoBaoShopService.getShopSellerGetResponse("sendTemplate"));
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
|
||||
|
||||
package com.hchbox.exception;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date 2021-7-26
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class CheckedAccountException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1751129723237168054L;
|
||||
|
||||
public CheckedAccountException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public CheckedAccountException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public CheckedAccountException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public CheckedAccountException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.hchbox.handle;
|
||||
|
||||
import com.hchbox.exception.CheckedAccountException;
|
||||
import com.hchbox.model.param.CommonResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 全局异常处理器结合sentinel 全局异常处理器不能作用在 oauth server https://gitee.com/log4j/pig/issues/I1M2TJ
|
||||
* </p>
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2020-06-29
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalBizExceptionHandler {
|
||||
|
||||
/**
|
||||
* 账号检测异常
|
||||
* @param e e
|
||||
* @return 返回信息
|
||||
*/
|
||||
@ExceptionHandler(CheckedAccountException.class)
|
||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||
public CommonResult<String> handleGlobalException(CheckedAccountException e) {
|
||||
log.error("全局异常信息 ex={}", e.getMessage(), e);
|
||||
|
||||
// 业务异常交由 sentinel 记录 Tracer.trace(e);
|
||||
return new CommonResult<String>().failed(e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理业务校验过程中碰到的非法参数异常 该异常基本由{@link Assert}抛出
|
||||
* @see Assert#hasLength(String, String)
|
||||
* @see Assert#hasText(String, String)
|
||||
* @see Assert#isTrue(boolean, String)
|
||||
* @see Assert#isNull(Object, String)
|
||||
* @see Assert#notNull(Object, String)
|
||||
* @param exception 参数校验异常
|
||||
* @return API返回结果对象包装后的错误输出结果
|
||||
*/
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public CommonResult<String> handleIllegalArgumentException(IllegalArgumentException exception) {
|
||||
log.error("非法参数,ex = {}", exception.getMessage(), exception);
|
||||
return new CommonResult<String>().failed(exception.getMessage());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue