From dd199e65652756d365f1144f5e969c99f141d7ba Mon Sep 17 00:00:00 2001 From: vchske Date: Mon, 29 Jun 2020 16:02:31 -0700 Subject: [PATCH] Fixing errors in mongo document tab (#58) * This fixes an issue where errors when editing documents in an API for MongoDB endpoint would not be presented in the UI. * Changing null to undefined in several places * Fixed style issue. Unignored MongoProxyClient.ts from full lint * More linter issues since the removal from lint ignore --- .eslintignore | 1 - src/Common/MongoProxyClient.ts | 66 ++++++++++---------------- src/Explorer/Tabs/MongoDocumentsTab.ts | 3 +- 3 files changed, 26 insertions(+), 44 deletions(-) diff --git a/.eslintignore b/.eslintignore index fd69a4831..cb0a35631 100644 --- a/.eslintignore +++ b/.eslintignore @@ -26,7 +26,6 @@ src/Common/Logger.test.ts src/Common/MessageHandler.test.ts src/Common/MessageHandler.ts src/Common/MongoProxyClient.test.ts -src/Common/MongoProxyClient.ts src/Common/MongoUtility.ts src/Common/NotificationsClientBase.ts src/Common/ObjectCache.test.ts diff --git a/src/Common/MongoProxyClient.ts b/src/Common/MongoProxyClient.ts index 4e988d093..3bd56b643 100644 --- a/src/Common/MongoProxyClient.ts +++ b/src/Common/MongoProxyClient.ts @@ -31,13 +31,13 @@ function authHeaders(): any { } } -export function queryIterator(databaseId: string, collection: Collection, query: string) { - let continuationToken: string = null; +export function queryIterator(databaseId: string, collection: Collection, query: string): any { + let continuationToken: string; return { fetchNext: () => { return queryDocuments(databaseId, collection, false, query).then(response => { continuationToken = response.continuationToken; - let headers = {} as any; + const headers = {} as any; response.headers.forEach((value: any, key: any) => { headers[key] = value; }); @@ -114,14 +114,7 @@ export function queryDocuments( headers: response.headers }; } - const errorMessage = await response.text(); - if (response.status === HttpStatusCodes.Forbidden) { - MessageHandler.sendMessage({ - type: MessageTypes.ForbiddenError, - reason: errorMessage - }); - } - throw new Error(errorMessage); + return errorHandling(response, "querying documents", params); }); } @@ -160,11 +153,11 @@ export function readDocument( ) } }) - .then(async response => { + .then(response => { if (response.ok) { return response.json(); } - errorHandling(response); + return errorHandling(response, "reading document", params); }); } @@ -199,11 +192,11 @@ export function createDocument( ...authHeaders() } }) - .then(async response => { + .then(response => { if (response.ok) { return response.json(); } - errorHandling(response); + return errorHandling(response, "creating document", params); }); } @@ -243,11 +236,11 @@ export function updateDocument( [CosmosSDKConstants.HttpHeaders.PartitionKey]: JSON.stringify(documentId.partitionKeyHeader()) } }) - .then(async response => { + .then(response => { if (response.ok) { return response.json(); } - errorHandling(response); + return errorHandling(response, "updating document", params); }); } @@ -285,11 +278,11 @@ export function deleteDocument( [CosmosSDKConstants.HttpHeaders.PartitionKey]: JSON.stringify(documentId.partitionKeyHeader()) } }) - .then(async response => { + .then(response => { if (response.ok) { - return; + return undefined; } - errorHandling(response); + return errorHandling(response, "deleting document", params); }); } @@ -340,15 +333,11 @@ export function createMongoCollectionWithProxy( } } ) - .then(async response => { + .then(response => { if (response.ok) { - return; + return undefined; } - NotificationConsoleUtils.logConsoleMessage( - ConsoleDataType.Error, - `Error creating collection: ${await response.json()}, Payload: ${params}` - ); - errorHandling(response); + return errorHandling(response, "creating collection", params); }); } @@ -407,13 +396,16 @@ export function getEndpoint(databaseAccount: ViewModels.DatabaseAccount): string return url; } -async function errorHandling(response: any): Promise { +async function errorHandling(response: any, action: string, params: any): Promise { const errorMessage = await response.text(); + // Log the error where the user can see it + NotificationConsoleUtils.logConsoleMessage( + ConsoleDataType.Error, + `Error ${action}: ${errorMessage}, Payload: ${JSON.stringify(params)}` + ); if (response.status === HttpStatusCodes.Forbidden) { - MessageHandler.sendMessage({ - type: MessageTypes.ForbiddenError, - reason: errorMessage - }); + MessageHandler.sendMessage({ type: MessageTypes.ForbiddenError, reason: errorMessage }); + return; } throw new Error(errorMessage); } @@ -462,14 +454,6 @@ export async function _createMongoCollectionWithARM( rpPayloadToCreateCollection ); } catch (response) { - NotificationConsoleUtils.logConsoleMessage( - ConsoleDataType.Error, - `Error creating collection: ${JSON.stringify(response)}` - ); - if (response.status === HttpStatusCodes.Forbidden) { - MessageHandler.sendMessage({ type: MessageTypes.ForbiddenError }); - return; - } - throw new Error(`Error creating collection`); + return errorHandling(response, "creating collection", undefined); } } diff --git a/src/Explorer/Tabs/MongoDocumentsTab.ts b/src/Explorer/Tabs/MongoDocumentsTab.ts index 1bcabfbfe..393166176 100644 --- a/src/Explorer/Tabs/MongoDocumentsTab.ts +++ b/src/Explorer/Tabs/MongoDocumentsTab.ts @@ -177,10 +177,9 @@ export default class MongoDocumentsTab extends DocumentsTab implements ViewModel ); }, reason => { + this.isExecutionError(true); const message = ErrorParserUtility.parse(reason)[0].message; window.alert(message); - this.isExecutionError(true); - console.error(reason); TelemetryProcessor.traceFailure( Action.UpdateDocument, {