Skip to content

Commit

Permalink
✨ customize stacktrace logger in ExceptionResolver #222
Browse files Browse the repository at this point in the history
  • Loading branch information
trydofor committed Mar 20, 2024
1 parent 5e99f44 commit 7085c7b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 27 deletions.
2 changes: 1 addition & 1 deletion observe/mirana
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<commons-text.version>1.11.0</commons-text.version> <!-- https://commons.apache.org/proper/commons-text/ -->
<joda-convert.version>2.2.3</joda-convert.version> <!-- https://github.com/JodaOrg/joda-convert/releases -->
<!-- wings project -->
<mirana.version>2.7.1</mirana.version> <!-- https://github.com/trydofor/pro.fessional.mirana -->
<mirana.version>2.7.2</mirana.version> <!-- https://github.com/trydofor/pro.fessional.mirana -->
<meepo.version>1.5.1</meepo.version> <!-- https://github.com/trydofor/pro.fessional.meepo -->
<kaptcha.version>2.3.3</kaptcha.version> <!-- https://github.com/trydofor/kaptcha -->
<fastjson2.version>2.0.47</fastjson2.version> <!-- https://github.com/alibaba/fastjson2/releases -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pro.fessional.wings.slardar.concur.impl;

import org.jetbrains.annotations.NotNull;
import org.springframework.core.annotation.Order;
import pro.fessional.mirana.text.StringTemplate;
import pro.fessional.wings.silencer.spring.WingsOrdered;
Expand All @@ -21,7 +22,7 @@ public DoubleKillExceptionResolver(SimpleResponse defaultResponse) {
}

@Override
protected SimpleResponse resolve(DoubleKillException e) {
protected SimpleResponse resolve(@NotNull DoubleKillException e) {
final String body = StringTemplate
.dyn(defaultResponse.getResponseBody())
.bindStr("{key}", e.getProgressKey())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

/**
* @author trydofor
Expand All @@ -14,7 +15,7 @@ public class SimpleExceptionResolver<T extends Exception> extends WingsException
protected final SimpleResponse defaultResponse;

@Override
protected SimpleResponse resolve(T ce) {
protected SimpleResponse resolve(@NotNull T ce) {
return defaultResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
import pro.fessional.wings.slardar.servlet.response.view.PlainTextView;

import java.lang.reflect.ParameterizedType;
Expand All @@ -17,8 +21,11 @@
* @since 2021-03-25
*/
@Slf4j
public abstract class WingsExceptionResolver<T extends Exception> extends AbstractHandlerExceptionResolver {
@Getter
@Setter
public abstract class WingsExceptionResolver<T extends Exception> implements HandlerExceptionResolver, Ordered {

private int order = Ordered.LOWEST_PRECEDENCE;
protected final Class<?> acceptClass;

protected WingsExceptionResolver() {
Expand All @@ -32,14 +39,15 @@ protected WingsExceptionResolver() {

@Override
@SuppressWarnings("unchecked")
protected ModelAndView doResolveException(
public ModelAndView resolveException(
@NotNull HttpServletRequest request,
@NotNull HttpServletResponse response, Object handler,
@NotNull HttpServletResponse response, @Nullable Object handler,
@NotNull Exception ex) {

if (acceptClass.isInstance(ex)) {
final SimpleResponse body = resolve((T) ex);
if (body == null) return null;

ModelAndView mav = new ModelAndView();
PlainTextView pv = new PlainTextView(body.getContentType(), body.getResponseBody());
mav.setStatus(HttpStatus.valueOf(body.getHttpStatus()));
Expand All @@ -56,5 +64,6 @@ protected ModelAndView doResolveException(
* @param ex current exception
* @return null if not support
*/
protected abstract SimpleResponse resolve(T ex);
@Nullable
protected abstract SimpleResponse resolve(@NotNull T ex);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package pro.fessional.wings.warlock.errorhandle;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
Expand Down Expand Up @@ -32,48 +37,65 @@
* @since 2021-03-25
*/
@Slf4j
@Getter
public class DefaultExceptionResolver extends SimpleExceptionResolver<Exception> {

protected final MessageSource messageSource;
protected final ObjectMapper objectMapper;

@Setter(onMethod_ = {@Autowired(required = false)})
protected Handler handler = null;

public DefaultExceptionResolver(SimpleResponse defaultResponse, MessageSource messageSource, ObjectMapper objectMapper) {
super(defaultResponse);
this.messageSource = messageSource;
this.objectMapper = objectMapper;
}

@Override
protected SimpleResponse resolve(Exception exception) {
protected SimpleResponse resolve(@NotNull Exception exception) {
SimpleResponse response = null;
try {
Throwable tmp = exception;
for (; tmp != null; tmp = tmp.getCause()) {
if (tmp instanceof HttpStatusException ex) {
return handle(ex);
Throwable cause = exception;
for (; response == null && cause != null; cause = cause.getCause()) {
if (cause instanceof HttpStatusException ex) {
response = handle(ex);
}
else if (tmp instanceof TerminalContextException ex) {
return handleUnauthorized(ex);
else if (cause instanceof TerminalContextException ex) {
response = handleUnauthorized(ex);
}
else if (tmp instanceof CodeException ex) {
return handle(ex);
else if (cause instanceof CodeException ex) {
response = handle(ex);
}
else if (tmp instanceof DataResult<?> ex) {
return handle(ex);
else if (cause instanceof DataResult<?> ex) {
response = handle(ex);
}
else if (tmp instanceof AuthenticationException ex) {
return handleUnauthorized(ex);
else if (cause instanceof AuthenticationException ex) {
response = handleUnauthorized(ex);
}
else if (tmp instanceof AccessDeniedException ex) {
return handleAccessDenied(ex);
else if (cause instanceof AccessDeniedException ex) {
response = handleAccessDenied(ex);
}
}
// handler
if (handler != null) {
// use original exception if response is null, otherwise the cause
response = handler.handle(response == null ? exception : cause, response);
}
}
catch (Exception e) {
catch (Throwable e) {
DummyBlock.ignore(e);
}

log.error("unhandled exception, response default", exception);
return defaultResponse;
if (response == null) {
log.error("unhandled exception, response default", exception);
response = defaultResponse;
}
else {
log.debug("handled exception, response simple", exception);
}

return response;
}

@SneakyThrows
Expand Down Expand Up @@ -132,4 +154,17 @@ protected String resolveMessage(String code, Object... args) {
Locale locale = LocaleZoneIdUtil.LocaleNonnull.get();
return messageSource.getMessage(code, args, locale);
}

/**
* handle response and exception after resolving cause
*/
public interface Handler {
/**
* use original exception if response is null, otherwise the cause
*/
@Nullable
default SimpleResponse handle(@NotNull Throwable cause, @Nullable SimpleResponse response) {
return response;
}
}
}

0 comments on commit 7085c7b

Please sign in to comment.