Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand Down Expand Up @@ -60,19 +61,6 @@ public ExceptionHandlers(final MessageSource messageSource) {
this.messageSource = messageSource;
}

@ExceptionHandler(Exception.class)
protected ShenyuAdminResult handleExceptionHandler(final Exception exception) {
LOG.error(exception.getMessage(), exception);
String message;
if (exception instanceof ShenyuException) {
ShenyuException shenyuException = (ShenyuException) exception;
message = shenyuException.getMessage();
} else {
message = "The system is busy, please try again later";
}
return ShenyuAdminResult.error(message);
}

@ExceptionHandler(DuplicateKeyException.class)
protected ShenyuAdminResult handleDuplicateKeyException(final DuplicateKeyException exception) {
LOG.error("duplicate key exception ", exception);
Expand Down Expand Up @@ -133,10 +121,21 @@ protected ShenyuAdminResult handleConstraintViolationException(final ConstraintV
.collect(Collectors.joining("| ")));
}

@ExceptionHandler(ShenyuAdminException.class)
protected ShenyuAdminResult handleShenyuException(final ShenyuAdminException exception) {
LOG.error("shenyu admin exception ", exception);
return ShenyuAdminResult.error(CommonErrorCode.ERROR, exception.getMessage());
@ExceptionHandler(ShenyuException.class)
protected ShenyuAdminResult handleShenyuException(final ShenyuException exception) {
String message = exception.getCause() == null ? null : exception.getCause().getMessage();
if (!StringUtils.hasText(message)) {
message = exception.getMessage();
}
LOG.error(exception.getMessage());
return ShenyuAdminResult.error(message);
}

@ExceptionHandler(Exception.class)
protected ShenyuAdminResult handleExceptionHandler(final Exception exception) {
LOG.error(exception.getMessage(), exception);
String message = "The system is busy, please try again later";
return ShenyuAdminResult.error(message);
}

@ExceptionHandler(WebI18nException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ public void testServerExceptionHandlerByException() {

@Test
public void testServerExceptionHandlerByShenyuException() {
Exception shenyuException = new ShenyuException("Test shenyuException message!");
ShenyuAdminResult result = exceptionHandlersUnderTest.handleExceptionHandler(shenyuException);
ShenyuException shenyuException = new ShenyuException(new Throwable("Test shenyuException message!"));
ShenyuAdminResult result = exceptionHandlersUnderTest.handleShenyuException(shenyuException);
assertEquals(result.getCode().intValue(), CommonErrorCode.ERROR);
assertEquals(result.getMessage(), shenyuException.getMessage());
assertEquals(result.getMessage(), shenyuException.getCause().getMessage());
}

@Test
Expand Down