File: error.go

package info (click to toggle)
golang-github-linuxdeepin-go-x11-client 0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,656 kB
  • sloc: python: 944; sh: 38; makefile: 17
file content (85 lines) | stat: -rw-r--r-- 1,816 bytes parent folder | download | duplicates (2)
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
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
}