Files
xiaomihome/miot/miot_error.go
T
4566704 3a44cb9e6a feat: 初始化小米 IoT (MIoT) 智能家居 Go 库
- 实现 MIoT 客户端核心功能(MQTT 连接、设备管理、属性读写)
- 支持云端 API 调用与局域网设备发现(mDNS)
- 集成国际化(i18n)多语言支持
- 添加 MIoT 设备规约解析器(spec_parser)
- 包含单元测试与使用示例
2026-06-28 22:05:49 +08:00

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}}
}