Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3e29d2ec65d061e376419ebca48d0481caa33b6d (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# *****************************************************************************
# * Copyright (c) 2011, 2013-2014 Wind River Systems, Inc. and others.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http:#www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# *     Wind River Systems - initial API and implementation
# *****************************************************************************

import cStringIO
import time
import types

# Error report attribute names

ERROR_CODE = "Code"
"""Integer : error code value"""

ERROR_TIME = "Time"
"""Integer : error time"""

ERROR_SERVICE = "Service"
"""String : name of the service the error occured in"""

ERROR_FORMAT = "Format"
"""String : error format"""

ERROR_PARAMS = "Params"
"""List : a list of parameters for the given error"""

ERROR_SEVERITY = "Severity"
"""Integer : error severity"""

ERROR_ALT_CODE = "AltCode"
"""Integer : error alternative code value"""

ERROR_ALT_ORG = "AltOrg"
"""Integer : error alternative org???"""

ERROR_CAUSED_BY = "CausedBy"
"""Object : the cause of the error"""

# Error severity codes
SEVERITY_ERROR = 0
"""Error"""

SEVERITY_WARNING = 1
"""Warning"""

SEVERITY_FATAL = 2
"""Fatal error"""

# Error code ranges
# Standard TCF code range
CODE_STD_MIN = 0
"""Minimum error code value"""
CODE_STD_MAX = 0xffff
"""Maximum error code value"""

# Service specific codes. Decoding requires service ID.
CODE_SERVICE_SPECIFIC_MIN = 0x10000
"""Service specific minimum error code value"""
CODE_SERVICE_SPECIFIC_MAX = 0x1ffff
"""Service specific maximum error code value"""

# Reserved codes - will never be used by the TCF standard
CODE_RESERVED_MIN = 0x20000
"""Service specific reserved minimum error code value"""
CODE_RESERVED_MAX = 0x2ffff
"""Service specific reserved maximum error code value"""

# Standard TCF error codes
TCF_ERROR_OTHER = 1
"""Other TCF errors"""

TCF_ERROR_JSON_SYNTAX = 2
"""JSON syntax error"""

TCF_ERROR_PROTOCOL = 3
"""Protocol error"""

TCF_ERROR_BUFFER_OVERFLOW = 4
"""Buffer overflow"""

TCF_ERROR_CHANNEL_CLOSED = 5
"""Channel closed"""

TCF_ERROR_COMMAND_CANCELLED = 6
"""Command canceled"""

TCF_ERROR_UNKNOWN_PEER = 7
"""Unknown peer"""

TCF_ERROR_BASE64 = 8
"""Base64 error"""

TCF_ERROR_EOF = 9
"""End of file"""

TCF_ERROR_ALREADY_STOPPED = 10
"""Already stopped"""

TCF_ERROR_ALREADY_EXITED = 11
"""Already exited"""

TCF_ERROR_ALREADY_RUNNING = 12
"""Already running"""

TCF_ERROR_ALREADY_ATTACHED = 13
"""Already attached"""

TCF_ERROR_IS_RUNNING = 14
"""Is running"""

TCF_ERROR_INV_DATA_SIZE = 15
"""Invalid data size"""

TCF_ERROR_INV_CONTEXT = 16
"""Invalid context"""

TCF_ERROR_INV_ADDRESS = 17
"""Invalid address"""

TCF_ERROR_INV_EXPRESSION = 18
"""Invalid expression"""

TCF_ERROR_INV_FORMAT = 19
"""Invalid format"""

TCF_ERROR_INV_NUMBER = 20
"""Invalid number"""

TCF_ERROR_INV_DWARF = 21
"""Invalid dwarf"""

TCF_ERROR_SYM_NOT_FOUND = 22
"""Symbol not found"""

TCF_ERROR_UNSUPPORTED = 23
"""Unsupported"""

TCF_ERROR_INV_DATA_TYPE = 24
"""Invalid data type"""

TCF_ERROR_INV_COMMAND = 25
"""Invalid command"""

TCF_ERROR_INV_TRANSPORT = 26
"""Invalid transport"""

TCF_ERROR_CACHE_MISS = 27
"""Cache miss"""

TCF_ERROR_NOT_ACTIVE = 28
"""Not active"""

_timestamp_format = "%Y-%m-%d %H:%M:%S"


class ErrorReport(Exception):
    """TCF error report class.

    :param msg: error report message
    :param attrs: TCF error report attributes to initialise this error report
                  with. See **ERROR_***.
    """
    def __init__(self, msg, attrs):
        super(ErrorReport, self).__init__(msg)
        if type(attrs) is types.IntType:
            attrs = {
                ERROR_CODE: attrs,
                ERROR_TIME: int(time.time() * 1000),
                ERROR_FORMAT: msg,
                ERROR_SEVERITY: SEVERITY_ERROR
            }
        self.attrs = attrs
        caused_by = attrs.get(ERROR_CAUSED_BY)
        if caused_by:
            errMap = caused_by
            bf = cStringIO.StringIO()
            bf.write("TCF error report:")
            bf.write('\n')
            appendErrorProps(bf, errMap)
            self.caused_by = ErrorReport(bf.getvalue(), errMap)

    def getErrorCode(self):
        """Get this exception error code.

        :returns: This error report error code, or **0**
        """
        return self.attrs.get(ERROR_CODE) or 0

    def getAltCode(self):
        """Get this exception alternative error code.

        :returns: This error report alternative error code, or **0**
        """
        return self.attrs.get(ERROR_ALT_CODE) or 0

    def getAltOrg(self):
        """Get this exception alternative org ???

        :returns: This error report alernative org???, or **None**
        """
        return self.attrs.get(ERROR_ALT_ORG)

    def getAttributes(self):
        """Get this error attribute.

        :returns: a :class:`dict` of this error attributes
        """
        return self.attrs


def toErrorString(data):
    if not data:
        return None
    errMap = data
    fmt = errMap.get(ERROR_FORMAT)
    if fmt:
        c = errMap.get(ERROR_PARAMS)
        if c:
            return fmt.format(c)
        return fmt
    code = errMap.get(ERROR_CODE)
    if code is not None:
        if code == TCF_ERROR_OTHER:
            alt_org = errMap.get(ERROR_ALT_ORG)
            alt_code = errMap.get(ERROR_ALT_CODE)
            if alt_org and alt_code:
                return "%s Error %d" % (alt_org, alt_code)
        return "TCF Error %d" % code
    return "Invalid error report format"


def appendErrorProps(bf, errMap):
    timeVal = errMap.get(ERROR_TIME)
    code = errMap.get(ERROR_CODE)
    service = errMap.get(ERROR_SERVICE)
    severity = errMap.get(ERROR_SEVERITY)
    alt_code = errMap.get(ERROR_ALT_CODE)
    alt_org = errMap.get(ERROR_ALT_ORG)
    if timeVal:
        bf.write('\n')
        bf.write("Time: ")
        bf.write(time.strftime(_timestamp_format,
                               time.localtime(timeVal / 1000.)))
    if severity:
        bf.write('\n')
        bf.write("Severity: ")
        if severity == SEVERITY_ERROR:
            bf.write("Error")
        elif severity == SEVERITY_FATAL:
            bf.write("Fatal")
        elif severity == SEVERITY_WARNING:
            bf.write("Warning")
        else:
            bf.write("Unknown")
    bf.write('\n')
    bf.write("Error text: ")
    bf.write(toErrorString(errMap))
    bf.write('\n')
    bf.write("Error code: ")
    bf.write(str(code))
    if service:
        bf.write('\n')
        bf.write("Service: ")
        bf.write(service)
    if alt_code:
        bf.write('\n')
        bf.write("Alt code: ")
        bf.write(str(alt_code))
        if alt_org:
            bf.write('\n')
            bf.write("Alt org: ")
            bf.write(alt_org)

Back to the top