Skip to content

Commit

Permalink
fix: error handling for when array_combine would fail
Browse files Browse the repository at this point in the history
Add message to make the error more actionable and include context in question

Resolves #842
  • Loading branch information
keevan committed Oct 16, 2023
1 parent 94c9704 commit 4e19237
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion classes/local/execution/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,14 @@ public function abort(?\Throwable $reason = null) {
}

$this->set_current_step(null);
$this->log('Engine: aborting steps', $message ? ['reason' => $message] : [], Logger::NOTICE);
$context = [];
if ($reason->debuginfo) {
$context = (array) json_decode($reason->debuginfo);
if (json_last_error() !== JSON_ERROR_NONE) {
$context = [];
}
}
$this->log('Engine: aborting steps', $message ? array_merge(['reason' => $message], $context) : [], Logger::NOTICE);
$this->exception = $reason;
foreach ($this->enginesteps as $enginestep) {
$status = $enginestep->status;
Expand Down
11 changes: 11 additions & 0 deletions classes/local/step/reader_csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,18 @@ public function csv_contents_generator() {

// Convert header string to an actual headers array.
$headers = str_getcsv($strheaders, $delimiter);
$numheaders = count($headers);
while (($data = fgetcsv($handle, $maxlinelength, $delimiter)) !== false) {
$numfields = count($data);
if ($numfields !== $numheaders) {
throw new \moodle_exception('reader_csv:header_field_count_mismatch', 'tool_dataflows', '', (object) [
'numfields' => $numfields,
'numheaders' => $numheaders,
], json_encode([
'fields' => $data,
'headers' => $headers,
]));
}
$record = array_combine($headers, $data);
yield (object) $record;
}
Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_dataflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@
$string['reader_csv:headers_help'] = 'If populated, then this will act as the header to map field to keys. If left blank, it will be populated automatically using the first read row.';
$string['reader_csv:overwriteheaders'] = 'Overwrite existing headers';
$string['reader_csv:overwriteheaders_help'] = 'If checked, the headers supplied above will be used instead of the ones in the file, effectively ignoring the first row.';
$string['reader_csv:header_field_count_mismatch'] = 'Number of headers ({$a->numheaders}) do not match number of fields ({$a->numfields})';

// Reader JSON.
$string['reader_json:arrayexpression_help'] = 'Nested array to extract from JSON. For example, {$a->expression} will return the users array from the following JSON (If empty it is assumed the starting point of the JSON file is an array):{$a->jsonexample}';
Expand Down

0 comments on commit 4e19237

Please sign in to comment.