-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic.lua
More file actions
267 lines (218 loc) · 8.62 KB
/
basic.lua
File metadata and controls
267 lines (218 loc) · 8.62 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
-- NOTE: Before running this example, compile the Teal files into Lua
package.path = "build/?.lua;build/?/init.lua;" .. package.path
local sentry = require("sentry.init")
-- Initialize Sentry with your DSN
sentry.init({
dsn = "https://e247e6e48f8f482499052a65adaa9f6b@o117736.ingest.us.sentry.io/4504930623356928",
environment = "production",
release = "wrap-demo@1.0",
debug = true,
-- file_transport = true,
-- file_path = "sentry-events.log",
-- append_mode = true
})
-- Set user context
sentry.set_user({
id = "user123",
email = "user@example.com",
username = "testuser"
})
-- Add tags for filtering
sentry.set_tag("server", "web-1")
sentry.set_tag("feature", "checkout")
-- Add extra context
sentry.set_extra("session_id", "abc123")
sentry.set_extra("request_id", "req456")
-- Add breadcrumbs for debugging context
sentry.add_breadcrumb({
message = "User started checkout process",
category = "navigation",
level = "info"
})
-- Helper functions to create deeper stack traces with parameters
local function send_confirmation_email(user_email, order_id, amount)
sentry.capture_message("Purchase confirmation sent to " .. user_email .. " for order #" .. order_id, "info")
end
local function process_payment(payment_method, amount, currency)
local order_id = "ORD-" .. math.random(1000, 9999)
send_confirmation_email("customer@example.com", order_id, amount)
end
local function validate_cart(items_count, total_price)
if items_count > 0 then
process_payment("credit_card", total_price, "USD")
end
end
local function checkout_handler(user_id, session_token)
local cart_items = 3
local total = 149.99
validate_cart(cart_items, total)
end
-- Capture a message with multiple stack frames and parameters
checkout_handler("user_12345", "sess_abcdef123456")
-- Functions that demonstrate common Lua pitfalls and errors
-- Common pitfall: Using 0-based indexing (Lua uses 1-based)
local function array_access_error(department, min_users_required)
local users = {"alice", "bob", "charlie"}
local debug_info = "Checking " .. department .. " (need " .. min_users_required .. " users)"
-- This will access nil (common mistake from other languages)
local first_user = users[0] -- Should be users[1]
-- This will cause an error when trying to use nil
return string.upper(first_user) .. " in " .. debug_info
end
-- Common pitfall: Using + for string concatenation instead of ..
local function string_concat_error(greeting_type, username, user_id)
local timestamp = os.time()
local session_id = "sess_" .. math.random(1000, 9999)
-- Wrong: using + instead of .. for concatenation
local message = greeting_type + " " + username + " (ID: " + user_id + ") at " + timestamp
return message .. " session: " .. session_id
end
-- Common pitfall: Calling methods on nil values
local function nil_method_call(service_name, environment)
local config = nil -- Simulate missing configuration
local default_timeout = 30
local retry_count = 3
-- This will error: attempt to index a nil value
local timeout = config.timeout or default_timeout
return "Service " .. service_name .. " (" .. environment .. ") timeout: " .. timeout
end
-- Common pitfall: Incorrect table iteration
local function table_iteration_error(record_type, max_items)
local data = {name = "test_record", value = 42, priority = "high"}
local result = ""
local processed_count = 0
-- Wrong: using ipairs on a hash table (should use pairs)
for i, v in ipairs(data) do
result = result .. tostring(v)
processed_count = processed_count + 1
end
return record_type .. ": processed " .. processed_count .. "/" .. max_items .. " -> " .. result
end
-- Demonstrate each error type with proper context
sentry.add_breadcrumb({
message = "Starting error demonstration",
category = "demo",
level = "info"
})
-- Test array indexing error
sentry.with_scope(function(scope)
scope:set_tag("error_type", "array_indexing")
scope:set_extra("expected_behavior", "Lua arrays are 1-indexed, not 0-indexed")
local function safe_array_access()
array_access_error("engineering", 2)
end
xpcall(safe_array_access, function(err)
sentry.capture_exception({
type = "IndexError",
message = "Array indexing error: " .. tostring(err)
})
return err
end)
end)
-- Test string concatenation error
sentry.with_scope(function(scope)
scope:set_tag("error_type", "string_concatenation")
scope:set_extra("expected_behavior", "Lua uses .. for string concatenation, not +")
local function safe_string_concat()
string_concat_error("Hello", "john_doe", 42)
end
xpcall(safe_string_concat, function(err)
sentry.capture_exception({
type = "TypeError",
message = "String concatenation error: " .. tostring(err)
})
return err
end)
end)
-- Test nil method call error
sentry.with_scope(function(scope)
scope:set_tag("error_type", "nil_access")
scope:set_extra("expected_behavior", "Always check for nil before accessing table fields")
local function safe_nil_access()
nil_method_call("database", "production")
end
xpcall(safe_nil_access, function(err)
sentry.capture_exception({
type = "NilAccessError",
message = "Nil access error: " .. tostring(err)
})
return err
end)
end)
-- Test table iteration error (this one might not error but produces wrong results)
sentry.with_scope(function(scope)
scope:set_tag("error_type", "iteration_logic")
scope:set_extra("expected_behavior", "Use pairs() for hash tables, ipairs() for arrays")
local result = table_iteration_error("user_profile", 10)
if result:find("processed 0/") then
sentry.capture_message("Table iteration produced empty result - likely using wrong iterator", "warning")
end
end)
-- Original database error for comparison with parameters
local function database_query(query_type, table_name, timeout_ms)
local connection_attempts = 3
local last_error = "Connection timeout after " .. timeout_ms .. "ms"
error("Database query failed: " .. query_type .. " on " .. table_name .. " (" .. last_error .. ")")
end
local function fetch_user_data(user_id, include_preferences)
database_query("SELECT", "users", 5000)
end
local function authenticate_user(username, password_hash)
fetch_user_data("user_12345", true)
end
local function handle_request(request_id, client_ip)
authenticate_user("john.doe", "sha256_abc123")
end
-- Capture the original authentication error
local function error_handler(err)
sentry.capture_exception({
type = "AuthenticationError",
message = err
})
return err
end
xpcall(function() handle_request("req_789", "192.168.1.100") end, error_handler)
-- Demonstrate automatic error capture vs manual handling
sentry.add_breadcrumb({
message = "About to demonstrate automatic vs manual error handling",
category = "demo",
level = "info"
})
-- Example 1: Manual error handling with xpcall (traditional approach)
print("\n=== Manual Error Handling ===")
sentry.with_scope(function(scope)
scope:set_tag("handling_method", "manual")
local function manual_error_demo(operation_type, resource_id)
local data = nil
local context = "Processing " .. operation_type .. " for resource " .. resource_id
return data.missing_field .. " (" .. context .. ")" -- Will cause nil access error
end
xpcall(function() manual_error_demo("update", "res_456") end, function(err)
sentry.capture_exception({
type = "ManuallyHandledError",
message = "Manually captured: " .. tostring(err)
})
print("[Manual] Error captured and handled gracefully")
return err
end)
end)
-- Example 2: Automatic error capture (new functionality)
-- NOTE: This will terminate the program after capturing the error
print("\n=== Automatic Error Handling ===")
print("The following error will be automatically captured by Sentry:")
sentry.with_scope(function(scope)
scope:set_tag("handling_method", "automatic")
scope:set_extra("note", "This error is automatically captured without xpcall")
-- Uncomment the next line to test automatic capture
-- WARNING: This will terminate the program!
-- error("This error is automatically captured!")
print("[Automatic] Error capture is enabled - any unhandled error() calls are automatically sent to Sentry")
end)
-- Use scoped context for temporary changes
sentry.with_scope(function(scope)
scope:set_tag("temporary", "value")
sentry.capture_message("OS Detection Test Message", "warning")
-- Temporary context is automatically restored
end)
-- Clean up
sentry.close()