`TypeError` coming through my custom add-on

Hey, I created an addon for my own work and it is not published on anki’s website. The objective of the addon is to populate a field at the time of the answer with text from a different card.

Source code of the add-on: Link

The add-on works through the CID_Index field in cards, if the CID_Index field exists and it is not empty, the add-on will to find the card and populate another field called Index in the card.

The Index field is added to the cards in the following manner:

<div class="index_data">{{edit:Index:}}</div>
<script>
		txt = document.querySelector(".index_data").textContent;
		if (txt == "") {
			document.querySelector(".index_data").innerHTML = "No Index";
		}
</script>

The add-on works fine with cards that have a CID_Index but in other instances it throws the following error:

Error
An error occurred. Please start Anki while holding down the shift key, which will temporarily disable the add-ons you have installed.
If the issue only occurs when add-ons are enabled, please use the Tools > Add-ons menu item to disable some add-ons and restart Anki, repeating until you discover the add-on that is causing the problem.
When you've discovered the add-on that is causing the problem, please report the issue to the add-on author.
Debug info:
Anki 2.1.65 (aa9a734f) Python 3.9.15 Qt 6.4.3 PyQt 6.4.0
Platform: Windows-10-10.0.22621
Flags: frz=True ao=True sv=3
Add-ons, last update check: 2023-07-09 13:18:28

Caught exception:
Traceback (most recent call last):
  File "aqt.webview", line 46, in cmd
  File "aqt.webview", line 153, in _onCmd
  File "aqt.webview", line 662, in _onBridgeCmd
  File "aqt.overview", line 92, in _linkHandler
  File "aqt.main", line 712, in moveToState
  File "aqt.main", line 733, in _reviewState
  File "aqt.reviewer", line 161, in show
  File "aqt.reviewer", line 181, in refresh_if_needed
  File "aqt.reviewer", line 243, in nextCard
  File "aqt.reviewer", line 361, in _showQuestion
  File "anki.cards", line 129, in question
  File "anki.cards", line 145, in render_output
  File "anki.template", line 235, in render
  File "anki.template", line 339, in apply_custom_filters
TypeError: can only concatenate str (not "NoneType") to str

Can someone please help me debug this? I tried multiple thing but couldn’t understand why this is happening.

Thanks in advance!

Regarding the error, your webview seems okay. Try printing some values and hunting down where the not

is.

Thank you so much for the response, FileX.

I added if name is not None and value is not None right after the loop to ensure that they are not None but it didn’t help :frowning:

What values are you referring to? I tried print name and value from the loop to check if everything was fine.

Looking at field: Index
Looking at field with value:
Looking at field: Note
Looking at field with value:
Looking at field: CID_Index
Looking at field with value:
Looking at field: Mnemonics
Looking at field with value:
Looking at field: Extra
Looking at field with value:
Looking at field: Syllabus
Looking at field with value:
Looking at field: Source
Looking at field with value:
Looking at field: Cloze99
Looking at field with value:
Caught exception:
Traceback (most recent call last):
  File "aqt.main", line 274, in on_focus_changed
  File "_aqt.hooks", line 3236, in __call__
  File "aqt.main", line 802, in on_focus_did_change
  File "aqt.reviewer", line 181, in refresh_if_needed
  File "aqt.reviewer", line 243, in nextCard
  File "aqt.reviewer", line 361, in _showQuestion
  File "anki.cards", line 129, in question
  File "anki.cards", line 145, in render_output
  File "anki.template", line 235, in render
  File "anki.template", line 339, in apply_custom_filters
TypeError: can only concatenate str (not "NoneType") to str

It looked like it looped through all the fields but later it gave an error in the end.

I think I found something interesting.

I added a print item in the filter_name at the start of the function

if not filter_name == "Index":
        print("Field: " + str(filter_name))
        return field_text

From the logs:

Field: edit
Caught exception:
Traceback (most recent call last):
  File "aqt.main", line 274, in on_focus_changed
  File "_aqt.hooks", line 3236, in __call__
  File "aqt.main", line 802, in on_focus_did_change
  File "aqt.reviewer", line 181, in refresh_if_needed
  File "aqt.reviewer", line 243, in nextCard
  File "aqt.reviewer", line 361, in _showQuestion
  File "anki.cards", line 129, in question
  File "anki.cards", line 145, in render_output
  File "anki.template", line 235, in render
  File "anki.template", line 339, in apply_custom_filters
TypeError: can only concatenate str (not "NoneType") to str

It looks like that this error is arising from a field called edit in this case but I do not have an edit field in the card. Probably this is causing the issue.

Does anyone know why this could be happening?

PS: I took my inspiration from this demo: https://github.com/ankitects/anki-addons/blob/main/demos/field_filter/__init__.py

Update: Checking the field_text None fixes the issue but it seems hacky. Probably there is a better way to fix this.

if not filter_name == "Index":
        if field_text is None:
              return ""
        return field_text

If you don’t have any further problems then that’s your solution. I guess it was a cause of returning None and the returned-value-handler which caused the error.

Yeah, it was returning None.
I will stick to my current solution for the time being until I come across something else that fixes this.