package com.samsung.aspect;

import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpStatusCodeException;

@Aspect
@Service
@Log4j2
public class ExceptionHandler {

    @Pointcut("execution(* com.samsung..*Impl.*(..))")
    public void serviceMethod() {
    }

    @AfterThrowing(pointcut = "serviceMethod()", throwing = "exception")
    public void transfer(JoinPoint thisJoinPoint, Exception exception) {
        // Knox Rest 실패 시
        if (exception instanceof HttpStatusCodeException) {
            log.error(((HttpStatusCodeException) exception).getResponseBodyAsString(), exception);
        } else {
            log.error(exception.getMessage(), exception);
        }
    }
}
