-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
69 lines (60 loc) · 1.63 KB
/
errors.go
File metadata and controls
69 lines (60 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package shp
import (
"errors"
"fmt"
)
// ErrType 定义错误类型.
type ErrType int
const (
// ErrInvalidFormat 无效的文件格式.
ErrInvalidFormat ErrType = iota + 1
// ErrCorruptedFile 文件损坏.
ErrCorruptedFile
// ErrUnsupportedType 不支持的类型.
ErrUnsupportedType
// ErrInvalidField 无效的字段
ErrInvalidField
// ErrIO IO错误
ErrIO
)
// ShapeError 自定义错误类型
type ShapeError struct {
Type ErrType
Message string
Cause error
}
// Error 实现 error 接口
func (e *ShapeError) Error() string {
if e.Cause != nil {
return fmt.Sprintf("shapefile error: %s (caused by: %v)", e.Message, e.Cause)
}
return fmt.Sprintf("shapefile error: %s", e.Message)
}
// Unwrap 支持错误解包
func (e *ShapeError) Unwrap() error {
return e.Cause
}
// Is 支持错误比较
func (e *ShapeError) Is(target error) bool {
var shapeErr *ShapeError
if errors.As(target, &shapeErr) {
return e.Type == shapeErr.Type
}
return false
}
// NewShapeError 创建新的 ShapeError
func NewShapeError(errType ErrType, message string, cause error) *ShapeError {
return &ShapeError{
Type: errType,
Message: message,
Cause: cause,
}
}
// 预定义的错误变量
var (
ErrInvalidFileExtension = NewShapeError(ErrInvalidFormat, "invalid file extension", nil)
ErrUnsupportedShapeType = NewShapeError(ErrUnsupportedType, "unsupported shape type", nil)
ErrInvalidFileHeader = NewShapeError(ErrCorruptedFile, "invalid file header", nil)
ErrFieldTooLong = NewShapeError(ErrInvalidField, "field value too long", nil)
ErrDbfNotInitialized = NewShapeError(ErrInvalidFormat, "DBF not initialized", nil)
)