Skip to content

Commit

Permalink
[BUG] Multiple errors are getting thrown and swallowed during initial…
Browse files Browse the repository at this point in the history
…ization when no instrumentation Key is provided #1691 (#1695)
  • Loading branch information
MSNev authored Oct 20, 2021
1 parent 649d471 commit 4602eea
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export interface IDiagnosticLogger {
*/
warnToConsole(message: string): void;

/**
* This will write an error to the console if possible.
* Provided by the default DiagnosticLogger instance, and internally the SDK will fall back to warnToConsole, however,
* direct callers MUST check for its existence on the logger as you can provide your own IDiagnosticLogger instance.
* @param message {string} - The error message
*/
errorToConsole?(message: string): void;

/**
* Resets the internal message count
*/
Expand Down
12 changes: 5 additions & 7 deletions shared/AppInsightsCore/src/JavaScriptSDK/BaseCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ICookieMgr } from "../JavaScriptSDK.Interfaces/ICookieMgr";
import { createCookieMgr } from "./CookieMgr";
import { arrForEach, isNullOrUndefined, toISOString, getSetValue, setValue, throwError, isNotTruthy, isFunction } from "./HelperFuncs";
import { strExtensionConfig, strIKey } from "./Constants";
import { DiagnosticLogger } from "./DiagnosticLogger";

const validationError = "Extensions must provide callback to initialize";

Expand Down Expand Up @@ -56,11 +57,8 @@ export class BaseCore implements IAppInsightsCore {
dynamicProto(BaseCore, this, (_self) => {
_self._extensions = new Array<IPlugin>();
_channelController = new ChannelController();
_self.logger = objCreateFn({
throwInternal: (severity: LoggingSeverity, msgId: _InternalMessageId, msg: string, properties?: Object, isUserAct = false) => { },
warnToConsole: (message: string) => { },
resetInternalMessageCount: () => { }
});
// Use a default logger so initialization errors are not dropped on the floor with full logging
_self.logger = new DiagnosticLogger({ loggingLevelConsole: LoggingSeverity.CRITICAL });

_eventQueue = [];
_self.isInitialized = () => _isInitialized;
Expand All @@ -74,7 +72,7 @@ export class BaseCore implements IAppInsightsCore {
if (!config || isNullOrUndefined(config.instrumentationKey)) {
throwError("Please provide instrumentation key");
}

_notificationManager = notificationManager;

// For backward compatibility only
Expand All @@ -95,7 +93,7 @@ export class BaseCore implements IAppInsightsCore {
if (logger) {
_self.logger = logger;
}

// Concat all available extensions
let allExtensions = [];
allExtensions.push(...extensions, ...config.extensions);
Expand Down
55 changes: 40 additions & 15 deletions shared/AppInsightsCore/src/JavaScriptSDK/DiagnosticLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const AiUserActionablePrefix = "AI: ";
*/
const AIInternalMessagePrefix = "AITR_";

const strErrorToConsole = "errorToConsole";
const strWarnToConsole = "warnToConsole";

function _sanitizeDiagnosticText(text: string) {
if (text) {
Expand All @@ -33,6 +35,20 @@ function _sanitizeDiagnosticText(text: string) {
return "";
}

function _logToConsole(func: string, message: string) {
let theConsole = getConsole();
if (!!theConsole) {
let logFunc = "log";
if (theConsole[func]) {
logFunc = "func";
}

if (isFunction(theConsole[logFunc])) {
theConsole[logFunc](message);
}
}
}

export class _InternalLogMessage{
public static dataType: string = "MessageData";

Expand Down Expand Up @@ -107,20 +123,23 @@ export class DiagnosticLogger implements IDiagnosticLogger {
if (_self.enableDebugExceptions()) {
throw message;
} else {
// Get the logging function and fallback to warnToConsole of for some reason errorToConsole doesn't exist
let logFunc = severity === LoggingSeverity.CRITICAL ? strErrorToConsole : strWarnToConsole;

if (!isUndefined(message.message)) {
const logLevel = _self.consoleLoggingLevel();
if (isUserAct) {
// check if this message type was already logged to console for this page view and if so, don't log it again
const messageKey: number = +message.messageId;

if (!_messageLogged[messageKey] && logLevel >= LoggingSeverity.WARNING) {
_self.warnToConsole(message.message);
if (!_messageLogged[messageKey] && logLevel >= severity) {
_self[logFunc](message.message);
_messageLogged[messageKey] = true;
}
} else {
// don't log internal AI traces in the console, unless the verbose logging is enabled
if (logLevel >= LoggingSeverity.WARNING) {
_self.warnToConsole(message.message);
// Only log traces if the console Logging Level is >= the throwInternal severity level
if (logLevel >= severity) {
_self[logFunc](message.message);
}
}

Expand All @@ -134,17 +153,15 @@ export class DiagnosticLogger implements IDiagnosticLogger {
* @param message {string} - The warning message
*/
_self.warnToConsole = (message: string) => {
let theConsole = getConsole();
if (!!theConsole) {
let logFunc = "log";
if (theConsole.warn) {
logFunc = "warn";
}
_logToConsole("warn", message);
}

if (isFunction(theConsole[logFunc])) {
theConsole[logFunc](message);
}
}
/**
* This will write an error to the console if possible
* @param message {string} - The error message
*/
_self.errorToConsole = (message: string) => {
_logToConsole("error", message);
}

/**
Expand Down Expand Up @@ -261,6 +278,14 @@ export class DiagnosticLogger implements IDiagnosticLogger {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}

/**
* This will write an error to the console if possible
* @param message {string} - The warning message
*/
public errorToConsole(message: string) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}

/**
* Resets the internal message count
*/
Expand Down

0 comments on commit 4602eea

Please sign in to comment.