From f52e0c2e24cadf9dd632fb31351c0ccae80ac7a4 Mon Sep 17 00:00:00 2001 From: Richard Hajek Date: Tue, 18 Jun 2024 17:34:27 +0200 Subject: [PATCH] addNotes reports errors, aborts on any error --- README.md | 69 +++++++++++++++++----------------------------- plugin/__init__.py | 12 ++++++-- 2 files changed, 35 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 032e5c5..a8c17c4 100644 --- a/README.md +++ b/README.md @@ -3592,55 +3592,36 @@ 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.
Sample request: ```json { - "action": "addNotes", - "version": 6, - "params": { - "notes": [ - { - "deckName": "Default", - "modelName": "Basic", - "fields": { - "Front": "front content", - "Back": "back content" - }, - "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" - ] - }] + "action":"addNotes", + "version":6, + "params":{ + "notes":[ + { + "deckName":"College::PluginDev", + "modelName":"non_existent_model", + "fields":{ + "Front":"front", + "Back":"bak" } - ] - } + }, + { + "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']" } ``` diff --git a/plugin/__init__.py b/plugin/__init__.py index d996b36..4037c23 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -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