重构:文件系统模块化架构,优化应用启动流程
This commit is contained in:
130
internal/filesystem/errors.go
Normal file
130
internal/filesystem/errors.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// ErrorCode 错误码类型
|
||||
type ErrorCode string
|
||||
|
||||
const (
|
||||
// 通用错误
|
||||
ErrCodeGeneral ErrorCode = "GENERAL_ERROR"
|
||||
ErrCodeInvalid ErrorCode = "INVALID_ARGUMENT"
|
||||
ErrCodeNotFound ErrorCode = "NOT_FOUND"
|
||||
ErrCodePermission ErrorCode = "PERMISSION_DENIED"
|
||||
ErrCodeIO ErrorCode = "IO_ERROR"
|
||||
|
||||
// 路径相关错误
|
||||
ErrCodePathTraversal ErrorCode = "PATH_TRAVERSAL"
|
||||
ErrCodeInvalidPath ErrorCode = "INVALID_PATH"
|
||||
ErrCodeSensitivePath ErrorCode = "SENSITIVE_PATH"
|
||||
|
||||
// 文件操作错误
|
||||
ErrCodeFileNotFound ErrorCode = "FILE_NOT_FOUND"
|
||||
ErrCodeFileExists ErrorCode = "FILE_EXISTS"
|
||||
ErrCodeDirectoryNotEmpty ErrorCode = "DIRECTORY_NOT_EMPTY"
|
||||
|
||||
// 安全相关错误
|
||||
ErrCodeSecurityViolation ErrorCode = "SECURITY_VIOLATION"
|
||||
ErrCodeSizeLimit ErrorCode = "SIZE_LIMIT_EXCEEDED"
|
||||
ErrCodeFileLocked ErrorCode = "FILE_LOCKED"
|
||||
|
||||
// ZIP相关错误
|
||||
ErrCodeZipInvalid ErrorCode = "ZIP_INVALID"
|
||||
ErrCodeZipBomb ErrorCode = "ZIP_BOMB"
|
||||
ErrCodeZipExtract ErrorCode = "ZIP_EXTRACT_FAILED"
|
||||
)
|
||||
|
||||
// FileError 文件系统专用错误类型
|
||||
// 包含详细的错误上下文信息,便于调试和用户提示
|
||||
type FileNotFoundError struct {
|
||||
Path string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *FileNotFoundError) Error() string {
|
||||
return fmt.Sprintf("文件不存在: %s", e.Path)
|
||||
}
|
||||
|
||||
func (e *FileNotFoundError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
// PathValidationError 路径验证错误
|
||||
type PathValidationError struct {
|
||||
Path string
|
||||
Reason string
|
||||
IsSensitive bool
|
||||
}
|
||||
|
||||
func (e *PathValidationError) Error() string {
|
||||
return fmt.Sprintf("路径验证失败: %s - %s", e.Path, e.Reason)
|
||||
}
|
||||
|
||||
// SecurityViolationError 安全违规错误
|
||||
type SecurityViolationError struct {
|
||||
Path string
|
||||
Violation string
|
||||
Suggestion string
|
||||
}
|
||||
|
||||
func (e *SecurityViolationError) Error() string {
|
||||
msg := fmt.Sprintf("安全违规: %s - %s", e.Path, e.Violation)
|
||||
if e.Suggestion != "" {
|
||||
msg += fmt.Sprintf("\n建议: %s", e.Suggestion)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
// SizeLimitError 大小限制错误
|
||||
type SizeLimitError struct {
|
||||
Path string
|
||||
ActualSize int64
|
||||
MaxSize int64
|
||||
SizeType string // "file" or "directory"
|
||||
}
|
||||
|
||||
func (e *SizeLimitError) Error() string {
|
||||
return fmt.Sprintf("%s大小超限: %s (实际: %.2f GB, 限制: %.2f GB)",
|
||||
e.SizeType, e.Path,
|
||||
float64(e.ActualSize)/(1024*1024*1024),
|
||||
float64(e.MaxSize)/(1024*1024*1024),
|
||||
)
|
||||
}
|
||||
|
||||
// FileLockedError 文件锁定错误
|
||||
type FileLockedError struct {
|
||||
Path string
|
||||
ProcessInfo string
|
||||
}
|
||||
|
||||
func (e *FileLockedError) Error() string {
|
||||
msg := fmt.Sprintf("文件被占用: %s", e.Path)
|
||||
if e.ProcessInfo != "" {
|
||||
msg += fmt.Sprintf("\n占用程序: %s", e.ProcessInfo)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
// WrapError 错误包装函数
|
||||
// 添加上下文信息到错误中
|
||||
func WrapError(operation string, path string, err error) error {
|
||||
return fmt.Errorf("%s 失败: %s - %w", operation, path, err)
|
||||
}
|
||||
|
||||
// WrapErrorf 格式化错误包装
|
||||
func WrapErrorf(format string, args ...interface{}) error {
|
||||
return fmt.Errorf(format, args...)
|
||||
}
|
||||
|
||||
// GetStackTrace 获取堆栈跟踪(用于调试)
|
||||
func GetStackTrace(skip int) string {
|
||||
buf := make([]byte, 4096)
|
||||
n := runtime.Stack(buf, false)
|
||||
if n > 0 {
|
||||
return string(buf[:n])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user