Repository navigation
Expand file tree
/
Copy patherror_handling.mojo
More file actions
128 lines (104 loc) Β· 4.37 KB
/
Copy patherror_handling.mojo
File metadata and controls
128 lines (104 loc) Β· 4.37 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
# Example 03: Error Handling
#
# Demonstrates: flare's typed error hierarchy, how errors carry context,
# and how callers should handle each error type.
#
# All flare errors implement Writable (print() and String()).
# Every error includes enough context to debug without a stack trace.
#
# Usage:
# pixi run example-errors
from flare.net.error import (
NetworkError,
ConnectionRefused,
ConnectionTimeout,
AddressInUse,
AddressParseError,
BrokenPipe,
DnsError,
)
from flare.net import IpAddr
from flare.dns import resolve
def main() raises:
print("=== flare Example 03: Error Handling ===")
print()
# ββ Error construction and display ββββββββββββββββββββββββββββββββββββββββββββ
print("ββ NetworkError ββ")
var net_err = NetworkError("connection reset by peer", 104)
print(" print():", net_err)
print(" String():", String(net_err))
print()
print("ββ ConnectionRefused (includes the address) ββ")
var refused = ConnectionRefused("127.0.0.1:8080")
print(" print():", refused)
# Includes the address so you know which server was unreachable
print()
print("ββ ConnectionTimeout (includes address + errno) ββ")
var timeout = ConnectionTimeout("10.0.0.1:443", 110)
print(" print():", timeout)
print()
print("ββ AddressInUse (port already bound) ββ")
var in_use = AddressInUse("0.0.0.0:8080")
print(" print():", in_use)
print()
print("ββ AddressParseError (includes the bad input) ββ")
var parse_err = AddressParseError("256.0.0.1")
print(" print():", parse_err)
print()
print("ββ BrokenPipe (peer closed connection) ββ")
var pipe_no_addr = BrokenPipe()
var pipe_with_addr = BrokenPipe("10.0.0.5:9000")
print(" no addr: ", pipe_no_addr)
print(" with addr:", pipe_with_addr)
print()
print("ββ DnsError (includes host + resolver message) ββ")
var dns_err = DnsError(
"nonexistent.example.com", 8, "Name or service not known"
)
print(" print():", dns_err)
print()
# ββ Error handling patterns βββββββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ Catching specific error types ββ")
print()
# Pattern: catch the most specific error first
def try_connect(host: String) raises -> String:
"""Simulate a connection attempt that validates the address first."""
# This will raise AddressParseError for invalid IPs
_ = IpAddr.parse(host)
# In a real implementation, this would also raise ConnectionRefused,
# ConnectionTimeout, BrokenPipe, etc. at the TCP layer.
return "connected"
# Valid address
try:
var result = try_connect("127.0.0.1")
print(" Valid IP: connect succeeded (address parsed OK)")
except e:
print(" Unexpected error:", e)
# Invalid address
try:
var result = try_connect("not.an.ip.address")
print(" ERROR: should have raised!")
except e:
# In production you might match on error type and retry,
# log with context, or convert to a user-facing message.
print(" Caught AddressParseError (invalid IP):", e)
print()
# ββ DNS error from real resolution βββββββββββββββββββββββββββββββββββββββββββ
print("ββ DNS error from real resolution ββ")
try:
_ = resolve("this.definitely.does.not.exist.flare.test")
print(" ERROR: should have raised!")
except e:
print(" Caught:", e)
# The error message includes the hostname so log scraping can correlate it.
print()
# ββ AddressParseError from injection attempt ββββββββββββββββββββββββββββββββββ
print("ββ AddressParseError preserves the bad input safely ββ")
try:
_ = IpAddr.parse("127.0.0.1\x00evil")
except e:
print(" Caught:", e)
# Note: the null byte is preserved in the error message so you can log
# the exact input that was rejected, aiding security audit trails.
print()
print("=== Example 03 complete ===")