Skip to content

Commit

Permalink
refactor: Update fetchRelatedFiles to include telemetry for error han…
Browse files Browse the repository at this point in the history
…dling
  • Loading branch information
amitjoshi committed Aug 14, 2024
1 parent 0f7bf45 commit 1f4a1bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class PowerPagesChatParticipant {
switch (activeFileParams.dataverseEntity) {
case 'adx_webpage':
if (activeFileUri) {
const files = await fetchRelatedFiles(activeFileUri, activeFileParams.dataverseEntity, activeFileParams.fieldType);
const files = await fetchRelatedFiles(activeFileUri, activeFileParams.dataverseEntity, activeFileParams.fieldType, this.telemetry);
relatedFiles.push(...files);
}
break;
Expand Down
2 changes: 2 additions & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ export interface IRelatedFiles {
fileContent: string;
fileName: string;
}

export const COPILOT_RELATED_FILES_FETCH_FAILED = "CopilotRelatedFilesFetchFailed";
57 changes: 33 additions & 24 deletions src/common/utilities/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import * as vscode from "vscode";
import { componentTypeSchema, EXTENSION_ID, EXTENSION_NAME, IRelatedFiles, relatedFilesSchema, SETTINGS_EXPERIMENTAL_STORE_NAME } from "../constants";
import { componentTypeSchema, COPILOT_RELATED_FILES_FETCH_FAILED, EXTENSION_ID, EXTENSION_NAME, IRelatedFiles, relatedFilesSchema, SETTINGS_EXPERIMENTAL_STORE_NAME } from "../constants";
import { CUSTOM_TELEMETRY_FOR_POWER_PAGES_SETTING_NAME } from "../OneDSLoggerTelemetry/telemetryConstants";
import { COPILOT_UNAVAILABLE, DataverseEntityNameMap, EntityFieldMap, FieldTypeMap } from "../copilot/constants";
import { IActiveFileData } from "../copilot/model";
Expand Down Expand Up @@ -244,40 +244,49 @@ async function getFileContent(activeFileUri: vscode.Uri, customExtension: string

return { customFileContent, customFileName };
} catch (error) {
// Log the error
return { customFileContent: '', customFileName: '' };
throw new Error(`Error reading the custom file content: ${error}`);
}
}

// Generic function to get file content based on type and component type
async function getFileContentByType(activeFileUri: vscode.Uri, componentType: string, fileType: string): Promise<{ customFileContent: string; customFileName: string; }> {
const extension = componentTypeSchema[componentType]?.[fileType];
if (!extension) {
//log the error
return { customFileContent: '', customFileName: '' };
try {
const extension = componentTypeSchema[componentType]?.[fileType];
if (!extension) {
throw new Error(`File type ${fileType} not found for component type ${componentType}`);
}
return await getFileContent(activeFileUri, extension);
} catch (error) {
const message = (error as Error)?.message;
throw new Error(message);
}
return getFileContent(activeFileUri, extension);
}

//fetchRelatedFiles function based on component type
export async function fetchRelatedFiles(activeFileUri: vscode.Uri, componentType: string, fieldType: string) {
const relatedFileTypes = relatedFilesSchema[componentType]?.[fieldType];
if (!relatedFileTypes) {
export async function fetchRelatedFiles(activeFileUri: vscode.Uri, componentType: string, fieldType: string, telemetry: ITelemetry): Promise<IRelatedFiles[]> {
try {
const relatedFileTypes = relatedFilesSchema[componentType]?.[fieldType];
if (!relatedFileTypes) {
return [];
}

const files: IRelatedFiles[] = await Promise.all(
relatedFileTypes.map(async fileType => {
const fileContentResult = await getFileContentByType(activeFileUri, componentType, fileType);
return {
fileType,
fileContent: fileContentResult.customFileContent,
fileName: fileContentResult.customFileName
};
})
);

return files;
} catch (error) {
const message = (error as Error)?.message;
telemetry.sendTelemetryErrorEvent(COPILOT_RELATED_FILES_FETCH_FAILED, { error: message });
return [];
}

const files: IRelatedFiles[] = await Promise.all(
relatedFileTypes.map(async fileType => {
const fileContentResult = await getFileContentByType(activeFileUri, componentType, fileType);
return {
fileType,
fileContent: fileContentResult.customFileContent,
fileName: fileContentResult.customFileName
};
})
);

return files;
}

export function getFilePathFromUri(uri: vscode.Uri): string {
Expand Down

0 comments on commit 1f4a1bb

Please sign in to comment.