package org.lym.pom.controller.moment.ui;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.shoulder.core.exception.BaseRuntimeException;
import org.shoulder.core.util.ServletUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 对于页面Get请求，异常了将异常 msg 作为前端的提示返回 modelAndView
 *
 * @author lym
 **/
@Component
@Aspect
public class SetMsgToFrontOnExForPageRequestAspect {

    @Around("execution(* pub.developers.forum.portal..*.*(..)) && @annotation(getMapping)")
    public Object process(ProceedingJoinPoint joinPoint, GetMapping getMapping) throws Throwable {
        String toastMessage;

        try {
            return joinPoint.proceed();
        } catch (BaseRuntimeException bizException) {
            toastMessage = bizException.getMessage();
        } catch (Exception e) {
            toastMessage = "未知异常";
        }

        ServletUtil.getRequest().setAttribute("toast", toastMessage);
        return "error";
    }

}
