@@ -30,11 +30,49 @@ let clientId = ''
3030export class WorkflowStudioEditorProvider implements vscode . CustomTextEditorProvider {
3131 public static readonly viewType = 'workflowStudio.asl'
3232
33+ /**
34+ * Opens a file in the Workflow Studio editor, after validating the document.
35+ * @param uri The URI of the document to open in the Workflow Studio editor.
36+ * @param params Optional parameters to customize the WebView panel.
37+ */
3338 public static async openWithWorkflowStudio (
3439 uri : vscode . Uri ,
3540 params ?: Parameters < typeof vscode . window . createWebviewPanel > [ 2 ]
3641 ) {
37- await vscode . commands . executeCommand ( 'vscode.openWith' , uri , WorkflowStudioEditorProvider . viewType , params )
42+ const document = await vscode . workspace . openTextDocument ( uri )
43+ await telemetry . stepfunctions_openWorkflowStudio . run ( async ( ) => {
44+ await this . validateAndThrowIfInvalid ( document )
45+ await vscode . commands . executeCommand ( 'vscode.openWith' , uri , WorkflowStudioEditorProvider . viewType , params )
46+ } )
47+ }
48+
49+ /**
50+ * Validates the document to ensure it is either valid JSON or YAML.
51+ * If the document is invalid, a warning is shown (if specified) and an error is thrown to stop further execution.
52+ * @param document The document to validate.
53+ * @param alertOnError If true, shows a warning message when the document is invalid. Defaults to `false`.
54+ */
55+ private static async validateAndThrowIfInvalid (
56+ document : vscode . TextDocument ,
57+ alertOnError ?: boolean
58+ ) : Promise < void > {
59+ const isInvalidJson = isInvalidJsonFile ( document )
60+ const isInvalidYaml = isInvalidYamlFile ( document )
61+
62+ if ( isInvalidJson || isInvalidYaml ) {
63+ const language = isInvalidJson ? 'JSON' : 'YAML'
64+ const errorKey = isInvalidJson ? 'InvalidJSONContent' : 'InvalidYAMLContent'
65+
66+ if ( alertOnError ) {
67+ void vscode . window . showErrorMessage ( i18n ( `AWS.stepFunctions.workflowStudio.actions.${ errorKey } ` ) )
68+ }
69+
70+ throw ToolkitError . chain (
71+ `Invalid ${ language } file` ,
72+ `The Workflow Studio editor was not opened because the ${ language } in the file is invalid` ,
73+ { code : errorKey }
74+ )
75+ }
3876 }
3977
4078 /**
@@ -114,20 +152,12 @@ export class WorkflowStudioEditorProvider implements vscode.CustomTextEditorProv
114152 webviewPanel . dispose ( )
115153 }
116154
117- const isInvalidJson = isInvalidJsonFile ( document )
118- const isInvalidYaml = isInvalidYamlFile ( document )
119-
120- if ( isInvalidJson || isInvalidYaml ) {
121- const language = isInvalidJson ? 'JSON' : 'YAML'
122- const errorKey = isInvalidJson ? 'InvalidJSONContent' : 'InvalidYAMLContent'
123-
155+ try {
156+ await WorkflowStudioEditorProvider . validateAndThrowIfInvalid ( document , true )
157+ } catch ( e ) {
158+ // If the document is invalid, reopen it with the default editor and re-throw the error
124159 await reopenWithDefaultEditor ( )
125- void vscode . window . showWarningMessage ( i18n ( `AWS.stepFunctions.workflowStudio.actions.${ errorKey } ` ) )
126- throw ToolkitError . chain (
127- `Invalid ${ language } file` ,
128- `The Workflow Studio editor was not opened because the ${ language } in the file is invalid` ,
129- { code : errorKey }
130- )
160+ throw e
131161 }
132162
133163 if ( ! this . webviewHtml ) {
0 commit comments