package utils

import (
	"errors"
	"fmt"
	"io"
	"net"
	"syscall"
)

func WarpConnErr(err error) (bool, error) {
	if err == nil {
		return true, nil
	}

	var opErr *net.OpError
	switch {

	case errors.Is(err, net.ErrClosed):
		return true, errors.New("连接已关闭")
	case errors.Is(err, io.EOF):
		return true, errors.New("连接已结束")

	case errors.As(err, &opErr):
		switch {
		case
			errors.Is(opErr.Err, syscall.WSAECONNRESET), errors.Is(opErr.Err, syscall.WSAECONNABORTED),
			errors.Is(opErr.Err, syscall.ECONNRESET), errors.Is(opErr.Err, syscall.ECONNABORTED):
			return false, errors.New("连接已重置或中止")
		case opErr.Timeout():
			return false, errors.New("连接已超时")
		}
	}
	return false, fmt.Errorf("连接发生未处理的错误: %w", err)
}
