package com.m2pool.lease.exception;

import com.m2pool.lease.dto.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器，用于捕获并处理应用中抛出的异常。
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理商品已出售异常，返回统一的错误结果。
     *
     * @param e 商品已出售异常对象
     * @return 包含错误信息的统一结果对象
     */
    @ExceptionHandler(ProductSoldOutException.class)
    public Result<String> handleProductSoldOutException(ProductSoldOutException e) {
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(MachineException.class)
    public Result<String> handleMachineException(MachineException e) {
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(PaymentException.class)
    public Result<String> handlePaymentException(PaymentException e) {
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(OrderException.class)
    public Result<String> handleOrderException(OrderException e) {
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(PayRechargeException.class)
    public Result<String> handleOrderException(PayRechargeException e) {
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(RSAException.class)
    public Result<String> handleRSAException(RSAException e) {
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(AuthException.class)
    public Result<String> handleAuthException(AuthException e) {
        return Result.fail(e.getMessage());
    }

    /**
     * 处理其他未明确捕获的异常，返回统一的错误结果。
     *
     * @param e 异常对象
     * @return 包含错误信息的统一结果对象
     */
    @ExceptionHandler(Exception.class)
    public Result<String> handleException(Exception e) {
        return Result.fail("系统异常，请稍后再试: " + e.getMessage());
    }
}
