时间:2024-6-18 作者:老大夫 分类: SpringCloud
exp.GlobalExceptionHandler.java
实际也是一个controller,出现异常报错就会跳转到这个controller中
package com.atguigu.cloud.exp;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResultData<String> exception(Exception e){
System.out.println("######## come in GlobalExceptionHandler");
log.error("全局异常信息:{}",e.getMessage(),e);
return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
}
}
@GetMapping("error")
public ResultData<Integer> getPayError() {
Integer i = Integer.valueOf(200);
try {
System.out.println("com in try catch");
int age=10/0;
}catch (Exception e){
e.printStackTrace();
return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
}
return ResultData.success(i);
}
推荐阅读:
OpenFeign重试机制
OpenFeign日志打印功能
OpenFeign更换HTTPClient提升性能
OpenFeign请求压缩功能
gateway 报错:Unable to find RoutePredicateFactory with name My
Resilience4J的CircuitBreaker断路器之熔断
Resilience4J的BulkHead舱壁隔离
OpenFeign超时控制功能
四种限流算法
报错:I/O error on POST request for "http://localhost:9411/api/v2/spans": Connect to http://localhost:9411 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: no further information
行动消除疑虑