为什么:并入宿主项目后须与宿主日志体系一致——宿主统一用 pkg/logger 初始化 logrus 全局实例并直接 logrus.Xxx 包级调用;包一层 Logger 接口会让 logrus 的 caller 定位(报告调用文件:行号)失准。 改动: - 删除 logger 包(Logger 接口 / Default / SetDefault,默认本就是 logrus.StandardLogger) - 11 个结构体移除 lgr logger.Logger 字段、SetLogger 方法及构造中的 lgr 初始化 - 192 处 lgr.Xxxf 调用改为 logrus.Xxxf,日志消息文本保持不变 - xiaomi.Client 移除 Logger() 访问器,air_conditioner 回读日志去掉判空调用直连 logrus - miot_i18n.go 两处标准库 log.Printf 改为 logrus.Errorf,统一日志出口 - go.mod:logrus 从 indirect 提升为直接依赖 - goimports/goformat 全树规范化:此前部分文件未 gofmt(单行 if、对齐),本次顺带 格式化,纯空白/换行变化,无逻辑改动 验证:go build / go vet / go test ./... 全部通过(bridge、miot、xiaomi、devices、specs)
202 lines
5.8 KiB
Go
202 lines
5.8 KiB
Go
// Package miot provides MIoT core client for Xiaomi Home devices.
|
|
// Ported from py-miot/miot_error.py.
|
|
package miot
|
|
|
|
import "fmt"
|
|
|
|
// ============================================================================
|
|
// Error codes (aligned with Python MIoTErrorCode enum)
|
|
// ============================================================================
|
|
|
|
const (
|
|
// Base error codes
|
|
CodeUnknown = -10000
|
|
CodeUnavailable = -10001
|
|
CodeInvalidParams = -10002
|
|
CodeResourceError = -10003
|
|
CodeInternalError = -10004
|
|
CodeUnauthorizedAccess = -10005
|
|
CodeTimeout = -10006
|
|
|
|
// OAuth error codes
|
|
CodeOauthUnauthorized = -10020
|
|
|
|
// HTTP error codes
|
|
CodeHttpInvalidAccessToken = -10030
|
|
|
|
// MIPS error codes
|
|
CodeMipsInvalidResult = -10040
|
|
|
|
// Cert error codes
|
|
CodeCertInvalidCert = -10050
|
|
|
|
// Config error codes
|
|
CodeConfigInvalidInput = -10100
|
|
CodeConfigInvalidState = -10101
|
|
|
|
// LAN error codes
|
|
CodeLanUnavailable = -10120
|
|
)
|
|
|
|
// ============================================================================
|
|
// MIoTError — base error type (implements error interface)
|
|
// ============================================================================
|
|
|
|
// MIoTError is the base error type for MIoT operations, aligned with
|
|
// Python's MIoTError(Exception). It holds a numeric code and a message.
|
|
type MIoTError struct {
|
|
Code int // MIoT error code (one of the CodeXxx constants)
|
|
Message string // Human-readable error message
|
|
}
|
|
|
|
// Error implements the builtin error interface.
|
|
func (e *MIoTError) Error() string {
|
|
return fmt.Sprintf("code=%d, message=%s", e.Code, e.Message)
|
|
}
|
|
|
|
// ToDict returns the error as a map, mirroring Python's to_dict().
|
|
func (e *MIoTError) ToDict() map[string]interface{} {
|
|
return map[string]interface{}{
|
|
"code": e.Code,
|
|
"message": e.Message,
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// Sub-error types (aligned with Python subclasses of MIoTError)
|
|
// ============================================================================
|
|
|
|
// MIoTOauthError represents an OAuth-related error.
|
|
type MIoTOauthError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTHttpError represents an HTTP-related error.
|
|
type MIoTHttpError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTMipsError represents a MIPS-related error.
|
|
type MIoTMipsError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTDeviceError represents a device-related error.
|
|
type MIoTDeviceError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTSpecError represents a SPEC-related error.
|
|
type MIoTSpecError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTStorageError represents a storage-related error.
|
|
type MIoTStorageError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTCertError represents a certificate-related error.
|
|
type MIoTCertError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTClientError represents a client-related error.
|
|
type MIoTClientError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTEvError represents an event-related error.
|
|
type MIoTEvError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MipsServiceError represents a MIPS service error.
|
|
type MipsServiceError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTConfigError represents a config-related error.
|
|
type MIoTConfigError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTOptionsError represents an options-related error.
|
|
type MIoTOptionsError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// MIoTLanError represents a LAN-related error.
|
|
type MIoTLanError struct {
|
|
MIoTError
|
|
}
|
|
|
|
// ============================================================================
|
|
// Constructors
|
|
// ============================================================================
|
|
|
|
// NewMIoTOauthError creates a new MIoTOauthError.
|
|
func NewMIoTOauthError(message string) *MIoTOauthError {
|
|
return &MIoTOauthError{MIoTError{Code: CodeOauthUnauthorized, Message: message}}
|
|
}
|
|
|
|
// NewMIoTHttpError creates a new MIoTHttpError.
|
|
func NewMIoTHttpError(message string) *MIoTHttpError {
|
|
return &MIoTHttpError{MIoTError{Code: CodeHttpInvalidAccessToken, Message: message}}
|
|
}
|
|
|
|
// NewMIoTMipsError creates a new MIoTMipsError.
|
|
func NewMIoTMipsError(message string) *MIoTMipsError {
|
|
return &MIoTMipsError{MIoTError{Code: CodeMipsInvalidResult, Message: message}}
|
|
}
|
|
|
|
// NewMIoTDeviceError creates a new MIoTDeviceError.
|
|
func NewMIoTDeviceError(message string) *MIoTDeviceError {
|
|
return &MIoTDeviceError{MIoTError{Code: CodeUnknown, Message: message}}
|
|
}
|
|
|
|
// NewMIoTSpecError creates a new MIoTSpecError.
|
|
func NewMIoTSpecError(message string) *MIoTSpecError {
|
|
return &MIoTSpecError{MIoTError{Code: CodeUnknown, Message: message}}
|
|
}
|
|
|
|
// NewMIoTStorageError creates a new MIoTStorageError.
|
|
func NewMIoTStorageError(message string) *MIoTStorageError {
|
|
return &MIoTStorageError{MIoTError{Code: CodeUnknown, Message: message}}
|
|
}
|
|
|
|
// NewMIoTCertError creates a new MIoTCertError.
|
|
func NewMIoTCertError(message string) *MIoTCertError {
|
|
return &MIoTCertError{MIoTError{Code: CodeCertInvalidCert, Message: message}}
|
|
}
|
|
|
|
// NewMIoTClientError creates a new MIoTClientError.
|
|
func NewMIoTClientError(message string) *MIoTClientError {
|
|
return &MIoTClientError{MIoTError{Code: CodeUnknown, Message: message}}
|
|
}
|
|
|
|
// NewMIoTEvError creates a new MIoTEvError.
|
|
func NewMIoTEvError(message string) *MIoTEvError {
|
|
return &MIoTEvError{MIoTError{Code: CodeUnknown, Message: message}}
|
|
}
|
|
|
|
// NewMipsServiceError creates a new MipsServiceError.
|
|
func NewMipsServiceError(message string) *MipsServiceError {
|
|
return &MipsServiceError{MIoTError{Code: CodeUnknown, Message: message}}
|
|
}
|
|
|
|
// NewMIoTConfigError creates a new MIoTConfigError.
|
|
func NewMIoTConfigError(message string) *MIoTConfigError {
|
|
return &MIoTConfigError{MIoTError{Code: CodeConfigInvalidInput, Message: message}}
|
|
}
|
|
|
|
// NewMIoTOptionsError creates a new MIoTOptionsError.
|
|
func NewMIoTOptionsError(message string) *MIoTOptionsError {
|
|
return &MIoTOptionsError{MIoTError{Code: CodeUnknown, Message: message}}
|
|
}
|
|
|
|
// NewMIoTLanError creates a new MIoTLanError.
|
|
func NewMIoTLanError(message string) *MIoTLanError {
|
|
return &MIoTLanError{MIoTError{Code: CodeLanUnavailable, Message: message}}
|
|
}
|