i could recommend starting here
https://github.com/moodle/moodle/blob/6153be6850869cdc3a6ae925dcf6e688ac481333/availability/classes/tree.php
from there on, you could backtrace what's broken. I had the coding error but with a different exception_thrown, and that helps me greatly in finally identifying it. Normally your error is caused by a broken record in the prefix_course_modules table. Check https://moodle.org/mod/forum/discuss.php?d=406776&parent=1641899 for additional info, although you might have to adapt the query to your specific needs.
Hope that helps.
https://github.com/moodle/moodle/blob/6153be6850869cdc3a6ae925dcf6e688ac481333/availability/classes/tree.php
from there on, you could backtrace what's broken. I had the coding error but with a different exception_thrown, and that helps me greatly in finally identifying it. Normally your error is caused by a broken record in the prefix_course_modules table. Check https://moodle.org/mod/forum/discuss.php?d=406776&parent=1641899 for additional info, although you might have to adapt the query to your specific needs.
Hope that helps.