forked from linuxdeepin/go-x11-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
89 lines (73 loc) · 1.89 KB
/
error.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package x
import "fmt"
type Error struct {
conn *Conn
Code uint8 // ErrorCode
Sequence uint16 // Sequence number
ResourceID uint32 // Resource ID for requests with side effects only
MinorCode uint16 // Minor opcode of the failed request
MajorCode uint8 // Major opcode of the failed request
}
func (err *Error) Error() string {
var errDesc string
var errName string
if 1 <= err.Code && err.Code <= 127 {
// core error code in range [1,127]
errName = errorCodeNameMap[err.Code]
} else {
// is ext error
errName = err.conn.ext.getExtErrName(err.Code)
}
if errName != "" {
errDesc = " (" + errName + ")"
}
var majorCodeDesc, minorCodeDesc string
if 1 <= err.MajorCode && err.MajorCode <= 127 {
// is core request
reqName := requestOpcodeNameMap[uint(err.MajorCode)]
if reqName != "" {
majorCodeDesc = " (" + reqName + ")"
}
} else {
// is ext request
ext := err.conn.ext.getExtByMajorOpcode(err.MajorCode)
if ext != nil {
reqName := ext.reqOpcodeNameMap[uint(err.MinorCode)]
if reqName != "" {
minorCodeDesc = " (" + reqName + ")"
}
majorCodeDesc = " (" + ext.name + ")"
}
}
return fmt.Sprintf("x.Error: %d%s, sequence: %d, resource id: %d,"+
" major code: %d%s, minor code: %d%s",
err.Code, errDesc, err.Sequence, err.ResourceID,
err.MajorCode, majorCodeDesc,
err.MinorCode, minorCodeDesc)
}
func newError(data []byte) *Error {
var v Error
responseType := data[0]
if responseType != ResponseTypeError {
panic("not error")
}
b := 1
v.Code = data[b]
b += 1
v.Sequence = Get16(data[b:])
b += 2
v.ResourceID = Get32(data[b:])
b += 4
v.MinorCode = Get16(data[b:])
b += 2
v.MajorCode = data[b]
return &v
}
func (c *Conn) NewError(data []byte) *Error {
err := newError(data)
err.conn = c
return err
}