package com.cusc.nirvana.user.rnr.fp.common.handler;

import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.exception.CuscUserException;
import com.cusc.nirvana.user.rnr.fp.common.ResponseCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author yubo
 * @since 2022-04-21 20:06
 */
@ControllerAdvice
@Order(0)
public class RnrFpExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(RnrFpExceptionHandler.class);

    @ExceptionHandler(CuscUserException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Response<?> cuscUserHandler(CuscUserException e) {
        LOGGER.warn("cuscRnrHandler Handler code: {}, msg: {}", e.getCode(), e.getMessage());
		return Response.createError(e.getMessage(), StringUtils.isBlank(e.getCode()) ? ResponseCode.SYSTEM_ERROR.getCode() : Integer.parseInt(e.getCode()));
    }
}
