Skip to content

Commit 1422971

Browse files
committed
issue #145: catch errors when indexing documents
1 parent 7a4c715 commit 1422971

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

classes/search/activity.php

+33-21
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,40 @@ public function get_document($record, $options = []) {
5353
return false;
5454
}
5555

56-
$cms = new cms($cm->instance);
57-
$renderer = new renderer($cms);
58-
$value = $renderer->get_html();
59-
$title = $cms->get('name');
60-
$valueformat = FORMAT_HTML;
61-
62-
// Prepare associative array with data from DB.
63-
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
64-
$doc->set('title', content_to_text($title, false));
65-
$doc->set('content', content_to_text($value, $valueformat));
66-
$doc->set('contextid', $context->id);
67-
$doc->set('courseid', $record->course);
68-
$doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
69-
$doc->set('modified', $record->timemodified);
70-
71-
// Check if this document should be considered new.
72-
if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $record->timecreated)) {
73-
// If the document was created after the last index time, it must be new.
74-
$doc->set_is_new(true);
75-
}
56+
// As mod_cms is a complicated activity that may use data from different modules dynamically,
57+
// there are many moving parts that potentially can break and throw errors/exception while doing global search indexing.
58+
// We would like to prevent that if possible.
59+
try {
60+
$cms = new cms($cm->instance);
61+
$renderer = new renderer($cms);
62+
$value = $renderer->get_html();
63+
$title = $cms->get('name');
64+
$valueformat = FORMAT_HTML;
65+
66+
// Prepare associative array with data from DB.
67+
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
68+
$doc->set('title', content_to_text($title, false));
69+
$doc->set('content', content_to_text($value, $valueformat));
70+
$doc->set('contextid', $context->id);
71+
$doc->set('courseid', $record->course);
72+
$doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
73+
$doc->set('modified', $record->timemodified);
74+
75+
// Check if this document should be considered new.
76+
if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $record->timecreated)) {
77+
// If the document was created after the last index time, it must be new.
78+
$doc->set_is_new(true);
79+
}
7680

77-
return $doc;
81+
return $doc;
82+
83+
} catch (\Throwable $ex) {
84+
debugging('Error getting mod_cms document for global search. '
85+
. ' cmid ' . $cm->id . ' ' . ' courseid ' . $cm->course . ' '
86+
. $ex->getMessage(), DEBUG_DEVELOPER);
87+
88+
return false;
89+
}
7890
}
7991

8092
/**

0 commit comments

Comments
 (0)