-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherror_reporting_demo.py
More file actions
executable file
·283 lines (222 loc) · 8.67 KB
/
error_reporting_demo.py
File metadata and controls
executable file
·283 lines (222 loc) · 8.67 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
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
279
280
281
282
283
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Error Reporting Demo
This example demonstrates how to use the automated error reporting system
in various scenarios.
Author: IPFS Accelerate Python Framework Team
"""
import sys
import os
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utils.error_reporter import ErrorReporter, get_error_reporter, report_error, install_global_exception_handler
def demo_basic_error_reporting():
"""Demonstrate basic error reporting"""
print("=" * 60)
print("Demo 1: Basic Error Reporting")
print("=" * 60)
# Create error reporter (will use env vars if available)
reporter = ErrorReporter(
github_token=os.environ.get('GITHUB_TOKEN', 'test_token'),
github_repo=os.environ.get('GITHUB_REPO', 'test_owner/test_repo'),
enabled=bool(os.environ.get('GITHUB_TOKEN')) # Only enable if token is set
)
print(f"Reporter enabled: {reporter.enabled}")
print(f"GitHub repo: {reporter.github_repo}")
print()
# Report a simple error
try:
# Simulate an error
raise ValueError("This is a demonstration error")
except Exception as e:
print(f"Caught exception: {e}")
issue_url = reporter.report_error(
exception=e,
source_component='demo-script',
context={
'demo': 'basic_error_reporting',
'user': os.environ.get('USER', 'unknown')
}
)
if issue_url:
print(f"✓ Error reported to GitHub: {issue_url}")
else:
print("✗ Error not reported (may be duplicate or disabled)")
print()
def demo_manual_error_reporting():
"""Demonstrate manual error reporting without exception object"""
print("=" * 60)
print("Demo 2: Manual Error Reporting")
print("=" * 60)
# Use convenience function
issue_url = report_error(
error_type='CustomError',
error_message='This is a manually created error report',
traceback_str='Stack trace line 1\nStack trace line 2',
source_component='demo-manual',
context={
'operation': 'manual_reporting',
'severity': 'low'
}
)
if issue_url:
print(f"✓ Error reported to GitHub: {issue_url}")
else:
print("✗ Error not reported (may be duplicate or disabled)")
print()
def demo_global_exception_handler():
"""Demonstrate global exception handler"""
print("=" * 60)
print("Demo 3: Global Exception Handler")
print("=" * 60)
# Install global exception handler
install_global_exception_handler('demo-global-handler')
print("✓ Global exception handler installed")
print(" Any uncaught exceptions will now be reported automatically")
print()
# Note: We can't actually trigger an uncaught exception in this demo
# without terminating the script, so we'll just show it's installed
print(" To test: run a script that raises an uncaught exception")
print()
def demo_context_information():
"""Demonstrate adding context to error reports"""
print("=" * 60)
print("Demo 4: Adding Context Information")
print("=" * 60)
reporter = get_error_reporter()
try:
# Simulate an error with lots of context
user_input = {'field1': 'value1', 'field2': 'value2'}
raise TypeError(f"Invalid input: expected string, got {type(user_input)}")
except Exception as e:
issue_url = reporter.report_error(
exception=e,
source_component='demo-context',
context={
'user_input': str(user_input),
'operation': 'process_user_data',
'step': 'validation',
'environment': {
'python_version': sys.version,
'os': os.name,
'cwd': os.getcwd()
}
}
)
if issue_url:
print(f"✓ Error with context reported: {issue_url}")
else:
print("✗ Error not reported")
print()
def demo_duplicate_prevention():
"""Demonstrate duplicate error prevention"""
print("=" * 60)
print("Demo 5: Duplicate Error Prevention")
print("=" * 60)
reporter = get_error_reporter()
# Report the same error multiple times
for i in range(3):
try:
raise RuntimeError("This is a duplicate error for testing")
except Exception as e:
issue_url = reporter.report_error(
exception=e,
source_component='demo-duplicate',
context={'attempt': i + 1}
)
if issue_url:
print(f"Attempt {i+1}: ✓ Error reported: {issue_url}")
else:
print(f"Attempt {i+1}: ✗ Duplicate detected, not reported")
print()
def demo_different_components():
"""Demonstrate reporting errors from different components"""
print("=" * 60)
print("Demo 6: Errors from Different Components")
print("=" * 60)
reporter = get_error_reporter()
components = ['mcp-server', 'dashboard', 'docker-container', 'cli']
for component in components:
try:
raise Exception(f"Error in {component}")
except Exception as e:
issue_url = reporter.report_error(
exception=e,
source_component=component,
context={'component': component}
)
if issue_url:
print(f"{component}: ✓ Reported")
else:
print(f"{component}: ✗ Not reported")
print()
def demo_check_status():
"""Demonstrate checking error reporter status"""
print("=" * 60)
print("Demo 7: Check Error Reporter Status")
print("=" * 60)
reporter = get_error_reporter()
print(f"Enabled: {reporter.enabled}")
print(f"GitHub Token: {'Set' if reporter.github_token else 'Not set'}")
print(f"GitHub Repo: {reporter.github_repo or 'Not set'}")
print(f"Include System Info: {reporter.include_system_info}")
print(f"Auto Label: {reporter.auto_label}")
print(f"Reported Errors Count: {len(reporter.reported_errors)}")
print(f"Cache File: {reporter.error_cache_file}")
if reporter.error_cache_file.exists():
print(f"Cache File Size: {reporter.error_cache_file.stat().st_size} bytes")
else:
print("Cache File: Does not exist yet")
print()
def main():
"""Run all demos"""
print()
print("╔" + "=" * 58 + "╗")
print("║" + " " * 58 + "║")
print("║" + " IPFS Accelerate Error Reporting System - Demo".center(58) + "║")
print("║" + " " * 58 + "║")
print("╚" + "=" * 58 + "╝")
print()
# Check if GitHub credentials are set
if not os.environ.get('GITHUB_TOKEN'):
print("⚠️ WARNING: GITHUB_TOKEN environment variable not set")
print(" Error reporting will be in demo mode only")
print(" To enable actual reporting, set GITHUB_TOKEN and GITHUB_REPO")
print()
else:
print("✓ GitHub credentials detected")
print(f" Repository: {os.environ.get('GITHUB_REPO', 'not set')}")
print()
input("Press Enter to continue...")
print()
# Run demos
try:
demo_basic_error_reporting()
input("Press Enter to continue...")
demo_manual_error_reporting()
input("Press Enter to continue...")
demo_global_exception_handler()
input("Press Enter to continue...")
demo_context_information()
input("Press Enter to continue...")
demo_duplicate_prevention()
input("Press Enter to continue...")
demo_different_components()
input("Press Enter to continue...")
demo_check_status()
except KeyboardInterrupt:
print("\n\nDemo interrupted by user")
return
print()
print("=" * 60)
print("All demos completed!")
print("=" * 60)
print()
print("To enable actual error reporting:")
print("1. Set GITHUB_TOKEN environment variable")
print("2. Set GITHUB_REPO environment variable (e.g., 'owner/repo')")
print("3. Run this demo again")
print()
if __name__ == '__main__':
main()