Can't handle empty data file #12

Closed
opened 2025-04-28 09:02:06 +02:00 by sroth · 1 comment
Owner

If the JSON file is empty, the application crashes.
There is no error handling for an empty data file.

The connector could add new records, but this method can't load anything if the file does not exist

src/connector.py Lines 40 to 42 in ec130fcee9
def get_all(self) -> list:
with open(self.json_file, "r") as f:
return json.load(f)

So this could handle a file without records

src/connector.py Lines 69 to 78 in ec130fcee9
def create_new(self, record: dict) -> int:
data = self.get_all()
if len(data) == 0:
next_id = 0
else:
next_id = max(data, key=lambda x: x["record_id"])["record_id"] + 1
record["record_id"] = next_id
data.append(record)
self._write_to_file(data)
return next_id

it will crash at the get_all method

If the JSON file is empty, the application crashes. There is no error handling for an empty data file. The connector could add new records, but this method can't load anything if the file does not exist https://git.c3000.ch/sroth/Brovski-Adress-Etiketten-Verwaltung/src/commit/ec130fcee9ef3a0500d610abc5cad098cf0ac15c/src/connector.py#L40-L42 So this could handle a file without records https://git.c3000.ch/sroth/Brovski-Adress-Etiketten-Verwaltung/src/commit/ec130fcee9ef3a0500d610abc5cad098cf0ac15c/src/connector.py#L69-L78 it will crash at the get_all method
sroth added the
bug
label 2025-04-28 09:02:06 +02:00
sroth added reference statuszeile_und_filter 2025-04-28 09:04:47 +02:00
sroth changed reference from statuszeile_und_filter to main 2025-05-09 20:52:51 +02:00
sroth referenced this issue from a commit 2025-05-09 21:28:49 +02:00
Author
Owner

6407cf4229 fixes the problem with empty or non existing files by returning empty lists

6407cf4229 fixes the problem with empty or non existing files by returning empty lists
sroth closed this issue 2025-05-09 21:29:40 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sroth/Brovski-Adress-Etiketten-Verwaltung#12
No description provided.