chg: [metaTemplate:update] Gracefully handle case when template on disk is not readable

refacto/CRUDComponent
Sami Mokaddem 2023-03-13 11:37:58 +01:00
parent 5b07c57037
commit 47bebe5b68
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
3 changed files with 30 additions and 1 deletions

View File

@ -404,8 +404,18 @@ class MetaTemplatesController extends AppController
$metaTemplate = $this->MetaTemplates->get($template_id, [ $metaTemplate = $this->MetaTemplates->get($template_id, [
'contain' => ['MetaTemplateFields'] 'contain' => ['MetaTemplateFields']
]); ]);
$templateOnDisk = $this->MetaTemplates->readTemplateFromDisk($metaTemplate->uuid); $error = '';
$errorMessage = '';
$templateOnDisk = $this->MetaTemplates->readTemplateFromDisk($metaTemplate->uuid, $error);
if (is_null($templateOnDisk)) {
$errorMessage = __('Could not retreive template\'s status. Reason: {0}', $error);
$this->Flash->error($errorMessage);
$templateOnDisk = [];
}
$templateStatus = $this->MetaTemplates->getStatusForMetaTemplate($templateOnDisk, $metaTemplate); $templateStatus = $this->MetaTemplates->getStatusForMetaTemplate($templateOnDisk, $metaTemplate);
if (!empty($errorMessage)) {
$templateStatus['error'] = $errorMessage;
}
$this->set('templateOnDisk', $templateOnDisk); $this->set('templateOnDisk', $templateOnDisk);
$this->set('templateStatus', $templateStatus); $this->set('templateStatus', $templateStatus);
return [ return [

View File

@ -555,6 +555,10 @@ class MetaTemplatesTable extends AppTable
if (substr($file, -5) === '.json') { if (substr($file, -5) === '.json') {
$errorMessage = ''; $errorMessage = '';
$template = $this->decodeTemplateFromDisk($path . $file, $errorMessage); $template = $this->decodeTemplateFromDisk($path . $file, $errorMessage);
if (!empty($errorMessage)) {
$error = $errorMessage;
return null;
}
if (!empty($template) && $template['uuid'] == $uuid) { if (!empty($template) && $template['uuid'] == $uuid) {
return $template; return $template;
} }
@ -1318,6 +1322,13 @@ class MetaTemplatesTable extends AppTable
$updateStatus['next_version'] = $template['version']; $updateStatus['next_version'] = $template['version'];
$updateStatus['new'] = false; $updateStatus['new'] = false;
$updateStatus['automatically-updateable'] = false; $updateStatus['automatically-updateable'] = false;
$updateStatus['conflicts'] = [];
if (empty($template)) {
$updateStatus['up-to-date'] = false;
$updateStatus['automatically-updateable'] = false;
$updateStatus['can-be-removed'] = false;
return $updateStatus;
}
if (intval($metaTemplate->version) >= intval($template['version'])) { if (intval($metaTemplate->version) >= intval($template['version'])) {
$updateStatus['up-to-date'] = true; $updateStatus['up-to-date'] = true;
$updateStatus['conflicts'][] = __('Could not update the template. Local version is equal or newer.'); $updateStatus['conflicts'][] = __('Could not update the template. Local version is equal or newer.');

View File

@ -11,6 +11,14 @@ if ($updateStatus['up-to-date']) {
'dismissible' => false, 'dismissible' => false,
]); ]);
$modalType = 'ok-only'; $modalType = 'ok-only';
} else if (empty($updateStatus['templateOnDisk'])) {
$diskTemplateError = $templateStatus['error'] ?? __('Unknown');
$bodyHtml .= $this->Bootstrap->alert([
'variant' => 'danger',
'html' => sprintf('<strong>%s</strong> %s<p>%s</p>', __('Could not get template on disk.'), __('Reason:'), h($diskTemplateError)),
'dismissible' => false,
]);
$modalType = 'ok-only';
} else { } else {
if ($updateStatus['automatically-updateable']) { if ($updateStatus['automatically-updateable']) {
$bodyHtml .= $this->Bootstrap->alert([ $bodyHtml .= $this->Bootstrap->alert([