import { ApiManagementModels } from "@azure/arm-apimanagement" ; import { window } from "vscode" ; import { AzureTreeItem , IParsedError , parseError } from "vscode-azureextensionui" ; import { policyFormat , showSavePromptConfigKey } from "../../../constants" ; import { ext } from "../../../extensionVariables" ; import { localize } from "../../../localize" ; import { errorUtil , processError } from "../../../utils/errorUtil" ; import { nameUtil } from "../../../utils/nameUtil" ; import { promptOpenWorkingFolder } from "../../../utils/vscodeUtils" ; import { IServiceTreeRoot } from "../../IServiceTreeRoot" ; import { Editor } from "../Editor" ; export abstract class BasePolicyEditor < TRoot extends IServiceTreeRoot > extends Editor < AzureTreeItem < TRoot > > { constructor ( ) { super ( showSavePromptConfigKey ) ; } public abstract getDefaultPolicy ( ) ; public abstract getPolicy ( context ) ; public abstract updatePolicy ( context , policy ) ; public async getData ( context ) { try { return await this . getPolicy ( context ) ; } catch ( error ) { const err = parseError ( error ) ; if ( err . errorType . toLocaleLowerCase ( ) === 'notfound' || err . errorType . toLowerCase ( ) === 'resourcenotfound' ) { return this . getDefaultPolicy ( ) ; } else { ext . outputChannel . appendLine ( error ) ; let errorMessage = err . message ; if ( err . errorType . toLowerCase ( ) === 'validationerror' ) { errorMessage = errorUtil ( error . response . body ) ; } ext . outputChannel . appendLine ( errorMessage ) ; ext . outputChannel . show ( ) ; throw new Error ( errorMessage ) ; } } } public async getDiffFilename ( context ) { return `${ nameUtil ( context . root ) }.policy.cshtml` ; } public async getFilename ( context ) { return `${ nameUtil ( context . root ) }-tempFile.policy.cshtml` ; } public async updateData ( context , data ) { try { await this . updatePolicy ( context , < ApiManagementModels . PolicyContract > { format : policyFormat , value : data } ) ; window . showInformationMessage ( localize ( "updatePolicySucceded" , `Changes to policy were uploaded to cloud.` ) ) ; return await this . getPolicy ( context ) ; } catch ( error ) { throw new Error ( processError ( error , localize ( "updatePolicyFailed" , `Changes to policy were not uploaded to cloud.` ) ) ) ; } } public async getSize ( ) { throw new Error ( localize ( "" , "Method not implemented." ) ) ; } public async getSaveConfirmationText ( ) { return localize ( "saveConfirmation" , "Do you want to upload changes to cloud?" ) ; } public async showEditor ( context , sizeLimit ? ) { await super . showEditor ( context , sizeLimit ) ; await promptOpenWorkingFolder ( ) ; } }