From 578f3d34a12352f7399e01115d3b81355222113a Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Wed, 18 Oct 2023 08:30:42 +0200 Subject: [PATCH] refactor(custom-templates): return parse error --- ...-create-custom-template-view.controller.js | 2 +- ...be-edit-custom-template-view.controller.js | 2 +- .../createCustomTemplateViewController.js | 2 +- .../editCustomTemplateViewController.js | 2 +- .../custom-templates/components/utils.ts | 39 ++++++++++++------- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/app/kubernetes/custom-templates/kube-create-custom-template-view/kube-create-custom-template-view.controller.js b/app/kubernetes/custom-templates/kube-create-custom-template-view/kube-create-custom-template-view.controller.js index 2535d1ae3..ac65f2d04 100644 --- a/app/kubernetes/custom-templates/kube-create-custom-template-view/kube-create-custom-template-view.controller.js +++ b/app/kubernetes/custom-templates/kube-create-custom-template-view/kube-create-custom-template-view.controller.js @@ -83,7 +83,7 @@ class KubeCreateCustomTemplateViewController { return; } - const variables = getTemplateVariables(templateStr); + const [variables] = getTemplateVariables(templateStr); const isValid = !!variables; diff --git a/app/kubernetes/custom-templates/kube-edit-custom-template-view/kube-edit-custom-template-view.controller.js b/app/kubernetes/custom-templates/kube-edit-custom-template-view/kube-edit-custom-template-view.controller.js index 100e16dc5..a5ef19ae9 100644 --- a/app/kubernetes/custom-templates/kube-edit-custom-template-view/kube-edit-custom-template-view.controller.js +++ b/app/kubernetes/custom-templates/kube-edit-custom-template-view/kube-edit-custom-template-view.controller.js @@ -112,7 +112,7 @@ class KubeEditCustomTemplateViewController { return; } - const variables = getTemplateVariables(templateStr); + const [variables] = getTemplateVariables(templateStr); const isValid = !!variables; diff --git a/app/portainer/views/custom-templates/create-custom-template-view/createCustomTemplateViewController.js b/app/portainer/views/custom-templates/create-custom-template-view/createCustomTemplateViewController.js index 068491039..9d4d76182 100644 --- a/app/portainer/views/custom-templates/create-custom-template-view/createCustomTemplateViewController.js +++ b/app/portainer/views/custom-templates/create-custom-template-view/createCustomTemplateViewController.js @@ -206,7 +206,7 @@ class CreateCustomTemplateViewController { return; } - const variables = getTemplateVariables(templateStr); + const [variables] = getTemplateVariables(templateStr); const isValid = !!variables; diff --git a/app/portainer/views/custom-templates/edit-custom-template-view/editCustomTemplateViewController.js b/app/portainer/views/custom-templates/edit-custom-template-view/editCustomTemplateViewController.js index e19731df2..67171afc8 100644 --- a/app/portainer/views/custom-templates/edit-custom-template-view/editCustomTemplateViewController.js +++ b/app/portainer/views/custom-templates/edit-custom-template-view/editCustomTemplateViewController.js @@ -178,7 +178,7 @@ class EditCustomTemplateViewController { return; } - const variables = getTemplateVariables(templateStr); + const [variables] = getTemplateVariables(templateStr); const isValid = !!variables; diff --git a/app/react/portainer/custom-templates/components/utils.ts b/app/react/portainer/custom-templates/components/utils.ts index 2c2362264..6fca12f39 100644 --- a/app/react/portainer/custom-templates/components/utils.ts +++ b/app/react/portainer/custom-templates/components/utils.ts @@ -4,31 +4,40 @@ import Mustache from 'mustache'; import { VariableDefinition } from './CustomTemplatesVariablesDefinitionField/CustomTemplatesVariablesDefinitionField'; export function getTemplateVariables(templateStr: string) { - const template = validateAndParse(templateStr); + const [template, error] = validateAndParse(templateStr); if (!template) { - return null; + return [null, error] as const; } - return template - .filter(([type, value]) => type === 'name' && value) - .map(([, value]) => ({ - name: value, - label: '', - defaultValue: '', - description: '', - })); + return [ + template + .filter(([type, value]) => type === 'name' && value) + .map(([, value]) => ({ + name: value, + label: '', + defaultValue: '', + description: '', + })), + null, + ] as const; } - -function validateAndParse(templateStr: string) { +type TemplateSpans = ReturnType; +function validateAndParse( + templateStr: string +): readonly [TemplateSpans, null] | readonly [null, string] { if (!templateStr) { - return []; + return [[] as TemplateSpans, null] as const; } try { - return Mustache.parse(templateStr); + return [Mustache.parse(templateStr), null] as const; } catch (e) { - return null; + if (!(e instanceof Error)) { + return [null, 'Parse error'] as const; + } + + return [null, e.message] as const; } }