addNotes reports errors, aborts on any error

This commit is contained in:
Richard Hajek 2024-06-18 17:34:27 +02:00 committed by Alex Yatskov
parent aab9346deb
commit f52e0c2e24
2 changed files with 35 additions and 46 deletions

View File

@ -3592,8 +3592,8 @@ Search parameters are passed to Anki, check the docs for more information: https
#### `addNotes` #### `addNotes`
* Creates multiple notes using the given deck and model, with the provided field values and tags. Returns an array of * Creates multiple notes using the given deck and model, with the provided field values and tags. Returns an array of
identifiers of the created notes (notes that could not be created will have a `null` identifier). Please see the identifiers of the created notes. In the event of any errors, all errors are gathered and returned.
documentation for `addNote` for an explanation of objects in the `notes` array. * Please see the documentation for `addNote` for an explanation of objects in the `notes` array.
<details> <details>
<summary><i>Sample request:</i></summary> <summary><i>Sample request:</i></summary>
@ -3605,39 +3605,20 @@ Search parameters are passed to Anki, check the docs for more information: https
"params":{ "params":{
"notes":[ "notes":[
{ {
"deckName": "Default", "deckName":"College::PluginDev",
"modelName":"non_existent_model",
"fields":{
"Front":"front",
"Back":"bak"
}
},
{
"deckName":"College::PluginDev",
"modelName":"Basic", "modelName":"Basic",
"fields":{ "fields":{
"Front": "front content", "Front":"front",
"Back": "back content" "Back":"bak"
}, }
"tags": [
"yomichan"
],
"audio": [{
"url": "https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=猫&kana=ねこ",
"filename": "yomichan_ねこ_猫.mp3",
"skipHash": "7e2c2f954ef6051373ba916f000168dc",
"fields": [
"Front"
]
}],
"video": [{
"url": "https://cdn.videvo.net/videvo_files/video/free/2015-06/small_watermarked/Contador_Glam_preview.mp4",
"filename": "countdown.mp4",
"skipHash": "4117e8aab0d37534d9c8eac362388bbe",
"fields": [
"Back"
]
}],
"picture": [{
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/A_black_cat_named_Tilly.jpg/220px-A_black_cat_named_Tilly.jpg",
"filename": "black_cat.jpg",
"skipHash": "8d6e4646dfae812bf39651b59d7429ce",
"fields": [
"Back"
]
}]
} }
] ]
} }
@ -3650,8 +3631,8 @@ Search parameters are passed to Anki, check the docs for more information: https
```json ```json
{ {
"result": [1496198395707, null], "result":null,
"error": null "error":"['model was not found: non_existent_model']"
} }
``` ```
</details> </details>

View File

@ -2018,11 +2018,19 @@ class AnkiConnect:
@util.api() @util.api()
def addNotes(self, notes): def addNotes(self, notes):
results = [] results = []
errs = []
for note in notes: for note in notes:
try: try:
results.append(self.addNote(note)) results.append(self.addNote(note))
except: except Exception as e:
results.append(None) # I specifically chose to continue, so we gather all the errors of all notes (ie not break)
errs.append(str(e))
if errs:
# Roll back the changes so on error nothing happens
self.deleteNotes(results)
raise Exception(str(errs))
return results return results