KamWithK / AnkiconnectAndroid

Unofficial Ankiconnect for Android (i.e. for Yomichan)
GNU General Public License v3.0
98 stars 14 forks source link

Addnote api crashes when some note fields are missing #18

Closed mikavilpas closed 1 year ago

mikavilpas commented 1 year ago

I discovered a weird thing that doesn't happen with the desktop ankiconnect app but happens with AnkiconnectAndroid. With the addNote api, if I try to add a note and leave out any optional field, the api crashes with a pretty cryptic error message.

Here's a failed call:

{
    "action": "addNote",
    "version": 6,
    "params": {
        "note": {
            "deckName": "reserve",
            "modelName": "Japanese 2022 new accent note",
            "fields": {
                "Expression": "ユウキさんのお母さんは、いい人ですか。",
                "Meaning": "ゆう‐き【勇気】\n<mark>ゆう‐き</mark>【<mark>勇気</mark>】<br />いさましい意気。物に恐れない気概。<span class=\"quote border border-dark rounded mr-3\">\n              「―をふるう」\n              <span class=\"quote-actions\" data-quote=\"―をふるう\"></span>\n            </span><span class=\"quote border border-dark rounded mr-3\">\n              「―がわく」\n              <span class=\"quote-actions\" data-quote=\"―がわく\"></span>\n            </span><br />⇒ゆうき‐づ・ける【勇気付ける】<br />⇒ゆうき‐りんりん【勇気{{zb93e}}{{zb93e}}】<br />",
                "Audio": "",
                "Focus": "勇気",
                "Meaning2": "Is Yuki's mother a good person?"
                //
                // NOTE: a field is left out:
                //, "Snapshot": ""
                //
            },
            "tags": [
                "hare"
            ],
            "options": {
                "allowDuplicate": true
            },
            "audio": [
                {
                    "url": "https://receptomanijalogi.web.app/audio/bunpro/ユウキさんのお母さんは、いい人ですか。.mp3",
                    "filename": "hare_勇気_2023-01-03T08:45:36.052Z",
                    "fields": [
                        "Audio"
                    ]
                }
            ]
        }
    }
}

The response:

{
  "result": null,
  "error": "java.lang.IllegalArgumentException: Incorrect flds argument : ユウキさんのお母さんは、いい人ですか。\u001fゆう‐き【勇気】\n\u003cmark\u003eゆう‐き\u003c/mark\u003e【\u003cmark\u003e勇気\u003c/mark\u003e】\u003cbr /\u003eいさましい意気。物に恐れない気概。\u003cspan class\u003d\"quote border border-dark rounded mr-3\"\u003e\n              「―をふるう」\n              \u003cspan class\u003d\"quote-actions\" data-quote\u003d\"―をふるう\"\u003e\u003c/span\u003e\n            \u003c/span\u003e\u003cspan class\u003d\"quote border border-dark rounded mr-3\"\u003e\n              「―がわく」\n              \u003cspan class\u003d\"quote-actions\" data-quote\u003d\"―がわく\"\u003e\u003c/span\u003e\n            \u003c/span\u003e\u003cbr /\u003e⇒ゆうき‐づ・ける【勇気付ける】\u003cbr /\u003e⇒ゆうき‐りんりん【勇気{{zb93e}}{{zb93e}}】\u003cbr /\u003e\u001f\u001fIs Yuki\u0027s mother a good person?\u001fnull"
}

I can work around this issue by having any empty fields be set to the empty string.

KamWithK commented 1 year ago

Thanks for pointing this out Error handling doesn't really happen very well here but since it's an easy one for you to avoid I don't think this is a priority issue for me to fix Going to close for now because off that but feel free to comment further/reopen or PR for this to be fixed 👍