package com.xunhong.erp.turbo.satoken;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author shenyifei
 */
@Slf4j
@RestControllerAdvice
@Order(2)
public class SaTokenExceptionHandler {

	@ExceptionHandler({NotLoginException.class})
	@ResponseStatus(value = HttpStatus.OK)
	public Response notLoginException(NotLoginException e) {
		log.error("not login", e);
		return SingleResponse.buildFailure(HttpStatus.UNAUTHORIZED.value() + "", e.getMessage());
	}

	@ExceptionHandler({NotRoleException.class})
	@ResponseStatus(value = HttpStatus.OK)
	public Response notRoleException(NotRoleException e) {
		log.error("not role", e);
		return SingleResponse.buildFailure(HttpStatus.FORBIDDEN.value() + "", e.getMessage());
	}

	@ExceptionHandler({NotPermissionException.class})
	@ResponseStatus(value = HttpStatus.OK)
	public Response notNotPermissionException(NotPermissionException e) {
		log.error("not permission", e);
		return SingleResponse.buildFailure(HttpStatus.FORBIDDEN.value() + "", e.getMessage());
	}

}
