@@ -3,13 +3,14 @@ import * as path from "path"
33import fs from "fs/promises"
44
55import * as vscode from "vscode"
6- import { z } from "zod"
6+ import { z , ZodError } from "zod"
77
88import { globalSettingsSchema } from "../../schemas"
99
1010import { ProviderSettingsManager , providerProfilesSchema } from "./ProviderSettingsManager"
1111import { ContextProxy } from "./ContextProxy"
1212import { CustomModesManager } from "./CustomModesManager"
13+ import { telemetryService } from "../../services/telemetry/TelemetryService"
1314
1415type ImportOptions = {
1516 providerSettingsManager : ProviderSettingsManager
@@ -34,15 +35,14 @@ export const importSettings = async ({ providerSettingsManager, contextProxy, cu
3435
3536 const schema = z . object ( {
3637 providerProfiles : providerProfilesSchema ,
37- globalSettings : globalSettingsSchema ,
38+ globalSettings : globalSettingsSchema . optional ( ) ,
3839 } )
3940
4041 try {
4142 const previousProviderProfiles = await providerSettingsManager . export ( )
4243
43- const { providerProfiles : newProviderProfiles , globalSettings } = schema . parse (
44- JSON . parse ( await fs . readFile ( uris [ 0 ] . fsPath , "utf-8" ) ) ,
45- )
44+ const data = JSON . parse ( await fs . readFile ( uris [ 0 ] . fsPath , "utf-8" ) )
45+ const { providerProfiles : newProviderProfiles , globalSettings = { } } = schema . parse ( data )
4646
4747 const providerProfiles = {
4848 currentApiConfigName : newProviderProfiles . currentApiConfigName ,
@@ -79,7 +79,16 @@ export const importSettings = async ({ providerSettingsManager, contextProxy, cu
7979
8080 return { providerProfiles, globalSettings, success : true }
8181 } catch ( e ) {
82- return { success : false }
82+ let error = "Unknown error"
83+
84+ if ( e instanceof ZodError ) {
85+ error = e . message
86+ telemetryService . captureSchemaValidationError ( { schemaName : "ImportExport" , error : e } )
87+ } else if ( e instanceof Error ) {
88+ error = e . message
89+ }
90+
91+ return { success : false , error }
8392 }
8493}
8594
@@ -97,6 +106,14 @@ export const exportSettings = async ({ providerSettingsManager, contextProxy }:
97106 const providerProfiles = await providerSettingsManager . export ( )
98107 const globalSettings = await contextProxy . export ( )
99108
109+ // It's okay if there are no global settings, but if there are no
110+ // provider profile configured then don't export. If we wanted to
111+ // support this case then the `importSettings` function would need to
112+ // be updated to handle the case where there are no provider profiles.
113+ if ( typeof providerProfiles === "undefined" ) {
114+ return
115+ }
116+
100117 const dirname = path . dirname ( uri . fsPath )
101118 await fs . mkdir ( dirname , { recursive : true } )
102119 await fs . writeFile ( uri . fsPath , JSON . stringify ( { providerProfiles, globalSettings } , null , 2 ) , "utf-8" )
0 commit comments