chrisk123999 / chris-premades

MIT License
45 stars 49 forks source link

[BUG] Errors in console, on FoundryVTT v12 load, for each token #308

Open morepurplemorebetter opened 3 days ago

morepurplemorebetter commented 3 days ago

Description

I'm getting a warning message in my console from this module for each token on the canvas:

Error: Error thrown in hooked function 'yC' for hook 'preUpdateToken'. e is null
[Detected 3 packages: chris-premades(1.0.14), multilevel-tokens(1.7.5), lib-wrapper(1.13.2.0)]
    onError foundry.js:654
[...]
Caused by: TypeError: e is null
[Detected 3 packages: chris-premades(1.0.14), multilevel-tokens(1.7.5), lib-wrapper(1.13.2.0)]
    getTemplatesInToken templateUtils.js:32
[...]

Looking at the getTemplatesInToken function in 'templateUtils.js', it seems that the variable token is null and the error is from the line let scene = token.document.parent;.

I'm not sure if this has anything to do with the other listed modules, but I have a feeling that this hook is simply being triggered by those modules doing something and the error is with CPR.

Changing line 32 of templateUtils.js to

let scene = token?.document?.parent;

fixes the error messages for me.

Versions

Cauldron of Plentiful Resources: v1.0.14 FoundryVTT: v12.331

chrisk123999 commented 3 days ago

The mulilevel tokens module interacts poorly with midi-qol. IIRC support for that module from Midi-Qol has been dropped. I'll check if I can add any null checks in the update token hook.

chrisk123999 commented 3 days ago

When are you getting this error? I'm unable to reproduce it after adding multi-level tokens.

morepurplemorebetter commented 3 days ago

Good to know about Multilevel Tokens, then my guess was probably wrong about this being a CPR issue.

As it being incompatible with Midi-QoL, this is what I found on the discord here:

Moto Moto — 19/06/2024 02:19 Technically Multilevel tokens clone tokens is incompatible with midiqol, though I had Tposney look at it recently and he thinks the only real issue will be CPR auras but then Chris said he'd look at that too lol. We had thought MLT was dying but it got saved in v12 so, basically long story short: It may not work with midi. Specifically clone tokens.

Thus, the other functions of Multilevel Tokens should still be compatible with Midi.

 

When are you getting this error? I'm unable to reproduce it after adding multi-level tokens.

I get this error when loading a scene. And it doesn't actually seem to be for every token, I get 6 errors on the first load and 3 whenever changing a scene while there are a different number of tokens than 3 or 6.

Upon testing it with a scene with nothing in it, just a background image (i.e. no walls, lighting, tokens, drawings, etc.), I get the same 3 error messages. The module "Token Attacher" is also listed as a culprit with the 3 errors when changing scenes, but disabling that module doesn't remove the error messages on load, but does remove them on scene change. Disabling Multilevel Tokens removes the error messages completely (too bad Multilevel Tokens is required by a subscriber-only module I'm using).

When switching scenes, the 3 error messages appear right after the debug message Multilevel Tokens | Refreshing all, so you are absolutely correct in laying the blame there.

After some more digging I found that Multilevel Tokens is causing this issue only for cloned tokens, of which there are 3 in my world. Removing all cloned tokens from my world (by disabling multilevel tokens for the drawing that cloned them) caused the errors to stop. I guess Token Attacher is causing all tokens on all scenes to be refreshed on any scene load, causing Multilevel Tokens to do something with them, which in turn activates CPR and hence the error.

You can find a relevant excerpt of my console log here. This is from loading Foundry, not from switching scenes.

 

I think my proposed fix is still the best solution, as too many modules are involved for this issue to be addressed at its core, and the solution doesn't break CPR in any way as I'm assuming that the Multilevel Tokens' cloned tokens aren't supported.