/*
 *    Copyright (c) 2018-2025, clinical All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: clinical
 */

package com.zhgd.xmgl.handler;


import com.zhgd.exception.CustomException;
import com.zhgd.jeecg.common.api.vo.Result;
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.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
@Slf4j
@RestController
@RestControllerAdvice
@Order(999999)
public class GlobalExceptionHandler {

	/**
	 * 全局异常.
	 */
	@ExceptionHandler({Exception.class, RuntimeException.class})
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public Result<Object> handleGlobalException(Exception e) {
		log.error("全局异常信息 {}", e.getMessage(), e);
		return Result.error("操作失败!");
	}

	/**
	 * 鉴权异常.
	 */
	@ExceptionHandler({CustomException.class})
	@ResponseStatus(HttpStatus.FORBIDDEN)
	public Result<Object> customException(Exception e) {
		log.error("鉴权异常信息 {}", e.getMessage(), e);
		return Result.error("登录失效!");
	}

}
