| | |
| | | /** 运行时异常 */ |
| | | @ExceptionHandler(RuntimeException.class) |
| | | public Result<?> runtimeExceptionHandler(RuntimeException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("运行时异常"); |
| | | } |
| | | |
| | | /** 类型转换异常 */ |
| | | @ExceptionHandler(ClassCastException.class) |
| | | public Result<?> classCastExceptionHandler(ClassCastException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("类型转换异常"); |
| | | } |
| | | /** 文件未找到异常 */ |
| | | @ExceptionHandler(FileNotFoundException.class) |
| | | public Result<?> FileNotFoundException(FileNotFoundException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("文件未找到异常"); |
| | | } |
| | | /** 数字格式异常 */ |
| | | @ExceptionHandler(NumberFormatException.class) |
| | | public Result<?> NumberFormatException(NumberFormatException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("数字格式异常"); |
| | | } |
| | | /** 安全异常 */ |
| | | @ExceptionHandler(SecurityException.class) |
| | | public Result<?> SecurityException(SecurityException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("安全异常"); |
| | | } |
| | | |
| | | /** 类型不存在异常 */ |
| | | @ExceptionHandler(TypeNotPresentException.class) |
| | | public Result<?> TypeNotPresentException(TypeNotPresentException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("类型不存在异常"); |
| | | } |
| | | |
| | | /** IO异常 */ |
| | | @ExceptionHandler(IOException.class) |
| | | public Result<?> iOExceptionHandler(IOException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("IO异常"); |
| | | } |
| | | |
| | | /** 未知方法异常 */ |
| | | @ExceptionHandler(NoSuchMethodException.class) |
| | | public Result<?> noSuchMethodExceptionHandler(NoSuchMethodException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("未知方法异常"); |
| | | } |
| | | |
| | | /** 数组越界异常 */ |
| | | @ExceptionHandler(IndexOutOfBoundsException.class) |
| | | public Result<?> indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("数组越界异常"); |
| | | } |
| | | /** sql语法错误异常 */ |
| | | @ExceptionHandler(BadSqlGrammarException.class) |
| | | public Result<?> BadSqlGrammarException(BadSqlGrammarException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("sql语法错误异常"); |
| | | } |
| | | |
| | | /** 无法注入bean异常 */ |
| | | @ExceptionHandler(NoSuchBeanDefinitionException.class) |
| | | public Result<?> NoSuchBeanDefinitionException(NoSuchBeanDefinitionException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("无法注入bean"); |
| | | } |
| | | |
| | | /** Http消息不可读异常 */ |
| | | @ExceptionHandler({HttpMessageNotReadableException.class}) |
| | | public Result<?> requestNotReadable(HttpMessageNotReadableException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("Http消息不可读"); |
| | | } |
| | | |
| | | /** 400错误 */ |
| | | @ExceptionHandler({TypeMismatchException.class}) |
| | | public Result<?> requestTypeMismatch(TypeMismatchException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("服务器异常"); |
| | | } |
| | | |
| | | /** 500错误 */ |
| | | @ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class}) |
| | | public Result<?> server500(RuntimeException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("服务器异常"); |
| | | } |
| | | |
| | | /** 栈溢出 */ |
| | | @ExceptionHandler({StackOverflowError.class}) |
| | | public Result<?> requestStackOverflow(StackOverflowError e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("栈溢出异常"); |
| | | } |
| | | |
| | | /** 除数不能为0 */ |
| | | @ExceptionHandler({ArithmeticException.class}) |
| | | public Result<?> arithmeticException(ArithmeticException e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("除数不能为0异常"); |
| | | } |
| | | |
| | | /** 其他错误 */ |
| | | @ExceptionHandler({Exception.class}) |
| | | public Result<?> exception(Exception e) { |
| | | log.error(e.getMessage(), e.getCause()); |
| | | e.printStackTrace(); |
| | | return Result.fail("网络连接失败,请退出后再试"); |
| | | } |
| | | } |