Skip to content

Commit d233bc5

Browse files
committed
feat: add error handling
1 parent 8e00fd9 commit d233bc5

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

Diff for: 00-playground/40-cursor-ai-intro/output.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Max

Diff for: 00-playground/40-cursor-ai-intro/writer.py

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
def write_file(filename: str, text: str):
2-
with open(filename, "w") as f:
3-
f.write(text)
2+
try:
3+
with open(filename, "w") as f:
4+
f.write(text)
5+
except IOError as e:
6+
print(f"Error writing to file {filename}: {e}")
7+
except Exception as e:
8+
print(f"Unexpected error occurred: {e}")
49

510
def read_file(filename: str) -> str:
6-
with open(filename, "r") as f:
7-
return f.read()
11+
try:
12+
with open(filename, "r") as f:
13+
return f.read()
14+
except FileNotFoundError:
15+
print(f"File {filename} not found")
16+
return ""
17+
except IOError as e:
18+
print(f"Error reading file {filename}: {e}")
19+
return ""
20+
except Exception as e:
21+
print(f"Unexpected error occurred: {e}")
22+
return ""
823

924
def append_file(filename: str, text: str):
10-
with open(filename, "a") as f:
11-
f.write(text)
25+
try:
26+
with open(filename, "a") as f:
27+
f.write(text)
28+
except IOError as e:
29+
print(f"Error appending to file {filename}: {e}")
30+
except Exception as e:
31+
print(f"Unexpected error occurred: {e}")
1232

1333
def parse_input(text: str) -> str:
1434
return text.strip('\n')

0 commit comments

Comments
 (0)