Created
November 3, 2018 09:36
-
-
Save disc99/3787633968f99901c2814dc4dcc25074 to your computer and use it in GitHub Desktop.
よく使うAOP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.grpc.Status; | |
import lombok.extern.slf4j.Slf4j; | |
import org.aspectj.lang.JoinPoint; | |
import org.aspectj.lang.annotation.AfterThrowing; | |
import org.aspectj.lang.annotation.Aspect; | |
import org.aspectj.lang.annotation.Before; | |
import org.springframework.stereotype.Component; | |
import javax.validation.ConstraintViolationException; | |
import java.util.Arrays; | |
@Aspect | |
@Component | |
@Slf4j | |
public class ApplicationAdvice { | |
@Before("@annotation(org.springframework.cloud.stream.annotation.StreamListener)") | |
public void beforeStreamListener(JoinPoint joinPoint) { | |
log.info("{}#{}: {}", | |
joinPoint.getTarget().getClass(), | |
joinPoint.getSignature().getName(), | |
Arrays.toString(joinPoint.getArgs())); | |
} | |
@AfterThrowing(value="execution(* com.example.ui.grpc..*.*(..))", throwing="ex") | |
public void handle(RuntimeException ex) { | |
String message = ex.getMessage(); | |
throw Status.INTERNAL | |
.withDescription(message == null ? ex.toString() : message) | |
.asRuntimeException(); | |
} | |
@AfterThrowing(value="execution(* com.example.ui.rest..*.*(..))", throwing="ex") | |
public void handle(ConstraintViolationException ex) { | |
throw new BadRequestException(ex.getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment