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,52 +3592,33 @@ Search parameters are passed to Anki, check the docs for more information: https
#### `addNotes`
* 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
documentation for `addNote` for an explanation of objects in the `notes` array.
identifiers of the created notes. In the event of any errors, all errors are gathered and returned.
* Please see the documentation for `addNote` for an explanation of objects in the `notes` array.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "addNotes",
"version": 6,
"params": {
"notes": [
"action":"addNotes",
"version":6,
"params":{
"notes":[
{
"deckName": "Default",
"modelName": "Basic",
"fields": {
"Front": "front content",
"Back": "back content"
"deckName":"College::PluginDev",
"modelName":"non_existent_model",
"fields":{
"Front":"front",
"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"
]
}]
{
"deckName":"College::PluginDev",
"modelName":"Basic",
"fields":{
"Front":"front",
"Back":"bak"
}
}
]
}
@ -3650,8 +3631,8 @@ Search parameters are passed to Anki, check the docs for more information: https
```json
{
"result": [1496198395707, null],
"error": null
"result":null,
"error":"['model was not found: non_existent_model']"
}
```
</details>

View File

@ -2018,11 +2018,19 @@ class AnkiConnect:
@util.api()
def addNotes(self, notes):
results = []
errs = []
for note in notes:
try:
results.append(self.addNote(note))
except:
results.append(None)
except Exception as e:
# 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