Refactor error handling part 2 (#313)

This commit is contained in:
victor-meng
2020-11-03 13:40:44 -08:00
committed by GitHub
parent a009a8ba5f
commit 5f1f7a8266
58 changed files with 229 additions and 336 deletions

View File

@@ -1,3 +1,4 @@
import { ARMError } from "../Utils/arm/request";
import { HttpStatusCodes } from "./Constants";
import { MessageTypes } from "../Contracts/ExplorerContracts";
import { SubscriptionType } from "../Contracts/ViewModels";
@@ -5,28 +6,27 @@ import { logConsoleError } from "../Utils/NotificationConsoleUtils";
import { logError } from "./Logger";
import { sendMessage } from "./MessageHandler";
export interface CosmosError {
code: number;
message?: string;
}
export const handleError = (error: string | CosmosError, consoleErrorPrefix: string, area: string): void => {
export const handleError = (error: string | ARMError | Error, area: string, consoleErrorPrefix?: string): void => {
const errorMessage = getErrorMessage(error);
const errorCode = typeof error === "string" ? undefined : error.code;
const errorCode = error instanceof ARMError ? error.code : undefined;
// logs error to data explorer console
logConsoleError(`${consoleErrorPrefix}:\n ${errorMessage}`);
const consoleErrorMessage = consoleErrorPrefix ? `${consoleErrorPrefix}:\n ${errorMessage}` : errorMessage;
logConsoleError(consoleErrorMessage);
// logs error to both app insight and kusto
logError(errorMessage, area, errorCode);
// checks for errors caused by firewall and sends them to portal to handle
sendNotificationForError(errorMessage, errorCode);
};
export const getErrorMessage = (error: string | CosmosError | Error): string => {
export const getErrorMessage = (error: string | Error): string => {
const errorMessage = typeof error === "string" ? error : error.message;
return replaceKnownError(errorMessage);
};
const sendNotificationForError = (errorMessage: string, errorCode: number): void => {
const sendNotificationForError = (errorMessage: string, errorCode: number | string): void => {
if (errorCode === HttpStatusCodes.Forbidden) {
if (errorMessage?.toLowerCase().indexOf("sharedoffer is disabled for your account") > 0) {
return;