Reputation: 73
I'm trying to get my code to store messages within txt file using json. Each time a new message comes in, it will add the new message to the array.
The structure will be:
{
"Messages": {
"Test Contact 2": {
"0": "\"Message 1"
},
"Test Contact 1": {
"0": "\"Message 1\"",
"1": "\"Message 2\""
}
}
}
And here is my current code:
class PluginOne(IPlugin):
def process(self):
try:
print("Database")
data_store('Test contact', 'Text Message')
pass
except Exception as exc:
print("Error in database: " + exc.args)
def data_store(key_id, key_info):
try:
with open('Plugins/Database/messages.txt', 'r+') as f:
data = json.load(f)
data[key_id] = key_info
f.seek(0)
json.dump(data, f)
f.truncate()
pass
except Exception as exc:
print("Error in data store: " + exc.args)
when I try to run the code, I get the following error
Can't convert 'tuple' object to str implicitly
Upvotes: 0
Views: 3763
Reputation: 19685
In your exception handler, you're adding exc.args
to a string. The args attribute is a tuple, which can't be converted to a string implicitly. You could...
# print it seperately
print("Error in data store")
print(exc.args)
# or alternatively
print("Error in data store: " + str(exc.args))
# or alternatively
print("Error in data store: " + str(exc))
However, this being a problem in the exception handler, the root cause of the problem is something else, and your current exception handler isn't that great at handling it:
It would perhaps be better to only catch
the specific exceptions you know you can recover from.
Upvotes: 1