@fluidframework/telemetry-utils 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,9 +36,8 @@ export declare function normalizeError(error: unknown, annotations?: IFluidError
36
36
  export declare function generateErrorWithStack(): Error;
37
37
  export declare function generateStack(): string | undefined;
38
38
  /**
39
- * Create a new error, wrapping and caused by the given unknown error.
40
- * Copies the inner error's message and stack over but otherwise uses newErrorFn to define the error.
41
- * The inner error's instance id will also be logged for telemetry analysis.
39
+ * Create a new error using newErrorFn, wrapping and caused by the given unknown error.
40
+ * Copies the inner error's stack, errorInstanceId and telemetry props over to the new error if present
42
41
  * @param innerError - An error from untrusted/unknown origins
43
42
  * @param newErrorFn - callback that will create a new error given the original error's message
44
43
  * @returns A new error object "wrapping" the given error
@@ -46,11 +45,6 @@ export declare function generateStack(): string | undefined;
46
45
  export declare function wrapError<T extends LoggingError>(innerError: unknown, newErrorFn: (message: string) => T): T;
47
46
  /** The same as wrapError, but also logs the innerError, including the wrapping error's instance id */
48
47
  export declare function wrapErrorAndLog<T extends LoggingError>(innerError: unknown, newErrorFn: (message: string) => T, logger: ITelemetryLogger): T;
49
- /**
50
- * True for any error object that is either external itself or is a wrapped/normalized external error
51
- * False for any error we created and raised within the FF codebase.
52
- */
53
- export declare function originatedAsExternalError(e: any): boolean;
54
48
  /**
55
49
  * True for any error object that is an (optionally normalized) external error
56
50
  * False for any error we created and raised within the FF codebase, or wrapped in a well-known error type
@@ -69,12 +63,12 @@ export declare function isTaggedTelemetryPropertyValue(x: any): x is ITaggedTele
69
63
  export declare const getCircularReplacer: () => (key: string, value: any) => any;
70
64
  /**
71
65
  * Base class for "trusted" errors we create, whose properties can generally be logged to telemetry safely.
72
- * All properties set on the object, or passed in (via the constructor or getTelemetryProperties),
66
+ * All properties set on the object, or passed in (via the constructor or addTelemetryProperties),
73
67
  * will be logged in accordance with their tag, if present.
74
68
  *
75
69
  * PLEASE take care to avoid setting sensitive data on this object without proper tagging!
76
70
  */
77
- export declare class LoggingError extends Error implements ILoggingError, Pick<IFluidErrorBase, "errorInstanceId"> {
71
+ export declare class LoggingError extends Error implements ILoggingError, Omit<IFluidErrorBase, "errorType"> {
78
72
  private readonly omitPropsFromLogging;
79
73
  private _errorInstanceId;
80
74
  get errorInstanceId(): string;
@@ -1 +1 @@
1
- {"version":3,"file":"errorLogging.d.ts","sourceRoot":"","sources":["../src/errorLogging.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACH,aAAa,EACb,4BAA4B,EAC5B,gBAAgB,EAChB,oBAAoB,EACvB,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EAEH,eAAe,EAGlB,MAAM,kBAAkB,CAAC;AAO1B,sEAAsE;AACtE,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,GAAG,EAAE,aAAa,EAAE,OAAO;aAiBhD,MAAM;;;EAkBrC;AAED,6CAA6C;AAC7C,eAAO,MAAM,eAAe,MAAO,GAAG,uBAAwE,CAAC;AAW/G,6EAA6E;AAC7E,MAAM,WAAW,sBAAsB;IACnC,4CAA4C;IAC5C,KAAK,CAAC,EAAE,oBAAoB,CAAC;CAChC;AAgBD;;;;;GAKG;AACH,wBAAgB,cAAc,CAC1B,KAAK,EAAE,OAAO,EACd,WAAW,GAAE,sBAA2B,GACzC,eAAe,CAsCjB;AAID;;;;;;;;GAQG;AACF,wBAAgB,sBAAsB,IAAI,KAAK,CAgB/C;AAED,wBAAgB,aAAa,IAAI,MAAM,GAAG,SAAS,CAElD;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,YAAY,EAC5C,UAAU,EAAE,OAAO,EACnB,UAAU,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,CAAC,GACnC,CAAC,CA0BH;AAED,sGAAsG;AACtG,wBAAgB,eAAe,CAAC,CAAC,SAAS,YAAY,EAClD,UAAU,EAAE,OAAO,EACnB,UAAU,EAAE,CAAC,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AC,EAAE,EAAE,MAAM;IAEnC,2FAA2F;IAE3F,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAY;IAE3C;;;;;OAKG;gBAEC,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,oBAAoB,EACX,oBAAoB,GAAE,GAAG,CAAC,MAAM,CAAa;IAalE;;OAEG;IACI,sBAAsB,CAAC,KAAK,EAAE,oBAAoB;IAIzD;;OAEG;IACI,sBAAsB,IAAI,oBAAoB;CAUxD"}
@@ -4,7 +4,7 @@
4
4
  * Licensed under the MIT License.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.LoggingError = exports.getCircularReplacer = exports.isTaggedTelemetryPropertyValue = exports.isExternalError = exports.originatedAsExternalError = exports.wrapErrorAndLog = exports.wrapError = exports.generateStack = exports.generateErrorWithStack = exports.normalizeError = exports.isILoggingError = exports.extractLogSafeErrorProperties = void 0;
7
+ exports.LoggingError = exports.getCircularReplacer = exports.isTaggedTelemetryPropertyValue = exports.isExternalError = exports.wrapErrorAndLog = exports.wrapError = exports.generateStack = exports.generateErrorWithStack = exports.normalizeError = exports.isILoggingError = exports.extractLogSafeErrorProperties = void 0;
8
8
  const uuid_1 = require("uuid");
9
9
  const fluidErrorBase_1 = require("./fluidErrorBase");
10
10
  /** @returns true if value is an object but neither null nor an array */
@@ -61,9 +61,6 @@ function patchLegacyError(legacyError) {
61
61
  patchMe.errorInstanceId = (0, uuid_1.v4)();
62
62
  }
63
63
  }
64
- // errorType "genericError" is used as a default value throughout the code.
65
- // Note that this matches ContainerErrorType/DriverErrorType's genericError
66
- const defaultErrorTypeForNormalize = "genericError";
67
64
  /**
68
65
  * Normalize the given error yielding a valid Fluid Error
69
66
  * @returns A valid Fluid Error with any provided annotations applied
@@ -83,12 +80,10 @@ function normalizeError(error, annotations = {}) {
83
80
  }
84
81
  // We have to construct a new Fluid Error, copying safe properties over
85
82
  const { message, stack } = extractLogSafeErrorProperties(error, false /* sanitizeStack */);
86
- const fluidError = new SimpleFluidError({
87
- errorType: defaultErrorTypeForNormalize,
83
+ const fluidError = new NormalizedExternalError({
88
84
  message,
89
85
  stack,
90
86
  });
91
- fluidError.addTelemetryProperties(Object.assign(Object.assign({}, annotations.props), { untrustedOrigin: 1 }));
92
87
  // We need to preserve these properties which are used in a non-typesafe way throughout driver code (see #8743)
93
88
  // Anywhere they are set should be on a valid Fluid Error that would have been returned above,
94
89
  // but we can't prove it with the types, so adding this defensive measure.
@@ -100,6 +95,8 @@ function normalizeError(error, annotations = {}) {
100
95
  // This is only interesting for non-objects
101
96
  fluidError.addTelemetryProperties({ typeofError: typeof (error) });
102
97
  }
98
+ const originalErrorTelemetryProps = (0, exports.isILoggingError)(error) ? error.getTelemetryProperties() : {};
99
+ fluidError.addTelemetryProperties(Object.assign(Object.assign(Object.assign({}, originalErrorTelemetryProps), annotations.props), { untrustedOrigin: 1 }));
103
100
  return fluidError;
104
101
  }
105
102
  exports.normalizeError = normalizeError;
@@ -134,9 +131,8 @@ function generateStack() {
134
131
  }
135
132
  exports.generateStack = generateStack;
136
133
  /**
137
- * Create a new error, wrapping and caused by the given unknown error.
138
- * Copies the inner error's message and stack over but otherwise uses newErrorFn to define the error.
139
- * The inner error's instance id will also be logged for telemetry analysis.
134
+ * Create a new error using newErrorFn, wrapping and caused by the given unknown error.
135
+ * Copies the inner error's stack, errorInstanceId and telemetry props over to the new error if present
140
136
  * @param innerError - An error from untrusted/unknown origins
141
137
  * @param newErrorFn - callback that will create a new error given the original error's message
142
138
  * @returns A new error object "wrapping" the given error
@@ -148,7 +144,7 @@ function wrapError(innerError, newErrorFn) {
148
144
  overwriteStack(newError, stack);
149
145
  }
150
146
  // Mark external errors with untrustedOrigin flag
151
- if (originatedAsExternalError(innerError)) {
147
+ if (isExternalError(innerError)) {
152
148
  newError.addTelemetryProperties({ untrustedOrigin: 1 });
153
149
  }
154
150
  // Reuse errorInstanceId
@@ -157,6 +153,11 @@ function wrapError(innerError, newErrorFn) {
157
153
  // For "back-compat" in the logs
158
154
  newError.addTelemetryProperties({ innerErrorInstanceId: innerError.errorInstanceId });
159
155
  }
156
+ // Lastly, copy over all other telemetry properties. Note these will not overwrite existing properties
157
+ // This will include the untrustedOrigin property if the inner error itself was created from an external error
158
+ if ((0, exports.isILoggingError)(innerError)) {
159
+ newError.addTelemetryProperties(innerError.getTelemetryProperties());
160
+ }
160
161
  return newError;
161
162
  }
162
163
  exports.wrapError = wrapError;
@@ -184,14 +185,6 @@ function overwriteStack(error, stack) {
184
185
  error.addTelemetryProperties({ stack2: stack });
185
186
  }
186
187
  }
187
- /**
188
- * True for any error object that is either external itself or is a wrapped/normalized external error
189
- * False for any error we created and raised within the FF codebase.
190
- */
191
- function originatedAsExternalError(e) {
192
- return !(0, fluidErrorBase_1.isValidLegacyError)(e) || (e.getTelemetryProperties().untrustedOrigin === 1);
193
- }
194
- exports.originatedAsExternalError = originatedAsExternalError;
195
188
  /**
196
189
  * True for any error object that is an (optionally normalized) external error
197
190
  * False for any error we created and raised within the FF codebase, or wrapped in a well-known error type
@@ -199,7 +192,7 @@ exports.originatedAsExternalError = originatedAsExternalError;
199
192
  function isExternalError(e) {
200
193
  return !(0, fluidErrorBase_1.isValidLegacyError)(e) ||
201
194
  (e.getTelemetryProperties().untrustedOrigin === 1 &&
202
- e.errorType === defaultErrorTypeForNormalize);
195
+ e.errorType === NormalizedExternalError.normalizedErrorType);
203
196
  }
204
197
  exports.isExternalError = isExternalError;
205
198
  /**
@@ -255,14 +248,13 @@ const getCircularReplacer = () => {
255
248
  }
256
249
  seen.add(value);
257
250
  }
258
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
259
251
  return value;
260
252
  };
261
253
  };
262
254
  exports.getCircularReplacer = getCircularReplacer;
263
255
  /**
264
256
  * Base class for "trusted" errors we create, whose properties can generally be logged to telemetry safely.
265
- * All properties set on the object, or passed in (via the constructor or getTelemetryProperties),
257
+ * All properties set on the object, or passed in (via the constructor or addTelemetryProperties),
266
258
  * will be logged in accordance with their tag, if present.
267
259
  *
268
260
  * PLEASE take care to avoid setting sensitive data on this object without proper tagging!
@@ -306,14 +298,17 @@ class LoggingError extends Error {
306
298
  }
307
299
  }
308
300
  exports.LoggingError = LoggingError;
309
- /** Simple implementation of IFluidErrorBase, extending LoggingError */
310
- class SimpleFluidError extends LoggingError {
301
+ /** The Error class used when normalizing an external error */
302
+ class NormalizedExternalError extends LoggingError {
311
303
  constructor(errorProps) {
312
304
  super(errorProps.message);
313
- this.errorType = errorProps.errorType;
305
+ this.errorType = NormalizedExternalError.normalizedErrorType;
314
306
  if (errorProps.stack !== undefined) {
315
307
  overwriteStack(this, errorProps.stack);
316
308
  }
317
309
  }
318
310
  }
311
+ // errorType "genericError" is used as a default value throughout the code.
312
+ // Note that this matches ContainerErrorType/DriverErrorType's genericError
313
+ NormalizedExternalError.normalizedErrorType = "genericError";
319
314
  //# sourceMappingURL=errorLogging.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"errorLogging.js","sourceRoot":"","sources":["../src/errorLogging.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAQH,+BAAkC;AAClC,qDAK0B;AAE1B,wEAAwE;AACxE,MAAM,eAAe,GAAG,CAAC,KAAU,EAAW,EAAE;IAC5C,OAAO,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAC;AAChF,CAAC,CAAC;AAEF,sEAAsE;AACtE,SAAgB,6BAA6B,CAAC,KAAU,EAAE,aAAsB;IAC5E,MAAM,sBAAsB,GAAG,CAAC,KAAa,EAAE,SAAkB,EAAE,EAAE;QACjE,IAAI,CAAC,aAAa,EAAE;YAChB,OAAO,KAAK,CAAC;SAChB;QACD,MAAM,WAAW,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACtC,WAAW,CAAC,KAAK,EAAE,CAAC,CAAC,uCAAuC;QAC5D,IAAI,SAAS,KAAK,SAAS,EAAE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AC;IAC3B,CAAC;IAED;;OAEG;IACI,sBAAsB;QACzB,MAAM,aAAa,GAAG,sBAAsB,CAAC,IAAI,EAAE,IAAI,CAAC,oBAAoB,CAAC,CAAC;QAC9E,+EAA+E;QAC/E,uCACO,aAAa,KAChB,KAAK,EAAE,IAAI,CAAC,KAAK,EACjB,OAAO,EAAE,IAAI,CAAC,OAAO,EACrB,eAAe,EAAE,IAAI,CAAC,gBAAgB,IACxC;IACN,CAAC;CACJ;AAnDD,oCAmDC;AAED,uEAAuE;AACvE,MAAM,gBAAiB,SAAQ,YAAY;IAGvC,YACI,UAIC;QAED,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAC1B,IAAI,CAAC,SAAS,GAAG,UAAU,CAAC,SAAS,CAAC;QACtC,IAAI,UAAU,CAAC,KAAK,KAAK,SAAS,EAAE;YAChC,cAAc,CAAC,IAAI,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;SAC1C;IACL,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n ILoggingError,\n ITaggedTelemetryPropertyType,\n ITelemetryLogger,\n ITelemetryProperties,\n} from \"@fluidframework/common-definitions\";\nimport { v4 as uuid } from \"uuid\";\nimport {\n hasErrorInstanceId,\n IFluidErrorBase,\n isFluidError,\n isValidLegacyError,\n} from \"./fluidErrorBase\";\n\n/** @returns true if value is an object but neither null nor an array */\nconst isRegularObject = (value: any): boolean => {\n return value !== null && !Array.isArray(value) && typeof value === \"object\";\n};\n\n/** Inspect the given error for common \"safe\" props and return them */\nexport function extractLogSafeErrorProperties(error: any, sanitizeStack: boolean) {\n const removeMessageFromStack = (stack: string, errorName?: string) => {\n if (!sanitizeStack) {\n return stack;\n }\n const stackFrames = stack.split(\"\\n\");\n stackFrames.shift(); // Remove \"[ErrorName]: [ErrorMessage]\"\n if (errorName !== undefined) {\n stackFrames.unshift(errorName); // Add \"[ErrorName]\"\n }\n return stackFrames.join(\"\\n\");\n };\n\n const message = (typeof error?.message === \"string\")\n ? error.message as string\n : String(error);\n\n const safeProps: { message: string; errorType?: string; stack?: string; } = {\n message,\n };\n\n if (isRegularObject(error)) {\n const { errorType, stack, name } = error;\n\n if (typeof errorType === \"string\") {\n safeProps.errorType = errorType;\n }\n\n if (typeof stack === \"string\") {\n const errorName = (typeof name === \"string\") ? name : undefined;\n safeProps.stack = removeMessageFromStack(stack, errorName);\n }\n }\n\n return safeProps;\n}\n\n/** type guard for ILoggingError interface */\nexport const isILoggingError = (x: any): x is ILoggingError => typeof x?.getTelemetryProperties === \"function\";\n\n/** Copy props from source onto target, but do not overwrite an existing prop that matches */\nfunction copyProps(target: ITelemetryProperties | LoggingError, source: ITelemetryProperties) {\n for (const key of Object.keys(source)) {\n if (target[key] === undefined) {\n target[key] = source[key];\n }\n }\n}\n\n/** Metadata to annotate an error object when annotating or normalizing it */\nexport interface IFluidErrorAnnotations {\n /** Telemetry props to log with the error */\n props?: ITelemetryProperties;\n}\n\n/** For backwards compatibility with pre-errorInstanceId valid errors */\nfunction patchLegacyError(\n legacyError: Omit<IFluidErrorBase, \"errorInstanceId\">,\n): asserts legacyError is IFluidErrorBase {\n const patchMe: { -readonly [P in \"errorInstanceId\"]?: IFluidErrorBase[P] } = legacyError as any;\n if (patchMe.errorInstanceId === undefined) {\n patchMe.errorInstanceId = uuid();\n }\n}\n\n// errorType \"genericError\" is used as a default value throughout the code.\n// Note that this matches ContainerErrorType/DriverErrorType's genericError\nconst defaultErrorTypeForNormalize = \"genericError\";\n\n/**\n * Normalize the given error yielding a valid Fluid Error\n * @returns A valid Fluid Error with any provided annotations applied\n * @param error - The error to normalize\n * @param annotations - Annotations to apply to the normalized error\n */\nexport function normalizeError(\n error: unknown,\n annotations: IFluidErrorAnnotations = {},\n): IFluidErrorBase {\n // Back-compat, while IFluidErrorBase is rolled out\n if (isValidLegacyError(error)) {\n patchLegacyError(error);\n }\n\n if (isFluidError(error)) {\n // We can simply add the telemetry props to the error and return it\n error.addTelemetryProperties(annotations.props ?? {});\n return error;\n }\n\n // We have to construct a new Fluid Error, copying safe properties over\n const { message, stack } = extractLogSafeErrorProperties(error, false /* sanitizeStack */);\n const fluidError: IFluidErrorBase = new SimpleFluidError({\n errorType: defaultErrorTypeForNormalize,\n message,\n stack,\n });\n\n fluidError.addTelemetryProperties({\n ...annotations.props,\n untrustedOrigin: 1, // This will let us filter to errors not originated by our own code\n });\n\n // We need to preserve these properties which are used in a non-typesafe way throughout driver code (see #8743)\n // Anywhere they are set should be on a valid Fluid Error that would have been returned above,\n // but we can't prove it with the types, so adding this defensive measure.\n if (typeof error === \"object\" && error !== null) {\n const { canRetry, retryAfterSeconds } = error as any;\n Object.assign(normalizeError, { canRetry, retryAfterSeconds });\n }\n\n if (typeof (error) !== \"object\") {\n // This is only interesting for non-objects\n fluidError.addTelemetryProperties({ typeofError: typeof (error) });\n }\n return fluidError;\n}\n\nlet stackPopulatedOnCreation: boolean | undefined;\n\n/**\n * The purpose of this function is to provide ability to capture stack context quickly.\n * Accessing new Error().stack is slow, and the slowest part is accessing stack property itself.\n * There are scenarios where we generate error with stack, but error is handled in most cases and\n * stack property is not accessed.\n * For such cases it's better to not read stack property right away, but rather delay it until / if it's needed\n * Some browsers will populate stack right away, others require throwing Error, so we do auto-detection on the fly.\n * @returns Error object that has stack populated.\n */\n export function generateErrorWithStack(): Error {\n const err = new Error(\"<<generated stack>>\");\n\n if (stackPopulatedOnCreation === undefined) {\n stackPopulatedOnCreation = (err.stack !== undefined);\n }\n\n if (stackPopulatedOnCreation) {\n return err;\n }\n\n try {\n throw err;\n } catch (e) {\n return e as Error;\n }\n}\n\nexport function generateStack(): string | undefined {\n return generateErrorWithStack().stack;\n}\n\n/**\n * Create a new error, wrapping and caused by the given unknown error.\n * Copies the inner error's message and stack over but otherwise uses newErrorFn to define the error.\n * The inner error's instance id will also be logged for telemetry analysis.\n * @param innerError - An error from untrusted/unknown origins\n * @param newErrorFn - callback that will create a new error given the original error's message\n * @returns A new error object \"wrapping\" the given error\n */\nexport function wrapError<T extends LoggingError>(\n innerError: unknown,\n newErrorFn: (message: string) => T,\n): T {\n const {\n message,\n stack,\n } = extractLogSafeErrorProperties(innerError, false /* sanitizeStack */);\n\n const newError = newErrorFn(message);\n\n if (stack !== undefined) {\n overwriteStack(newError, stack);\n }\n\n // Mark external errors with untrustedOrigin flag\n if (originatedAsExternalError(innerError)) {\n newError.addTelemetryProperties({ untrustedOrigin: 1 });\n }\n\n // Reuse errorInstanceId\n if (hasErrorInstanceId(innerError)) {\n newError.overwriteErrorInstanceId(innerError.errorInstanceId);\n\n // For \"back-compat\" in the logs\n newError.addTelemetryProperties({ innerErrorInstanceId: innerError.errorInstanceId });\n }\n\n return newError;\n}\n\n/** The same as wrapError, but also logs the innerError, including the wrapping error's instance id */\nexport function wrapErrorAndLog<T extends LoggingError>(\n innerError: unknown,\n newErrorFn: (message: string) => T,\n logger: ITelemetryLogger,\n) {\n const newError = wrapError(innerError, newErrorFn);\n\n // This will match innerError.errorInstanceId if present (see wrapError)\n const errorInstanceId = newError.errorInstanceId;\n\n // For \"back-compat\" in the logs\n const wrappedByErrorInstanceId = errorInstanceId;\n\n logger.sendTelemetryEvent({\n eventName: \"WrapError\",\n errorInstanceId,\n wrappedByErrorInstanceId,\n }, innerError);\n\n return newError;\n}\n\nfunction overwriteStack(error: IFluidErrorBase | LoggingError, stack: string) {\n // supposedly setting stack on an Error can throw.\n try {\n Object.assign(error, { stack });\n } catch (errorSettingStack) {\n error.addTelemetryProperties({ stack2: stack });\n }\n}\n\n/**\n * True for any error object that is either external itself or is a wrapped/normalized external error\n * False for any error we created and raised within the FF codebase.\n */\nexport function originatedAsExternalError(e: any): boolean {\n return !isValidLegacyError(e) || (e.getTelemetryProperties().untrustedOrigin === 1);\n}\n\n/**\n * True for any error object that is an (optionally normalized) external error\n * False for any error we created and raised within the FF codebase, or wrapped in a well-known error type\n */\nexport function isExternalError(e: any): boolean {\n return !isValidLegacyError(e) ||\n (e.getTelemetryProperties().untrustedOrigin === 1 &&\n e.errorType === defaultErrorTypeForNormalize);\n}\n\n/**\n * Type guard to identify if a particular value (loosely) appears to be a tagged telemetry property\n */\nexport function isTaggedTelemetryPropertyValue(x: any): x is ITaggedTelemetryPropertyType {\n return (typeof (x?.value) !== \"object\" && typeof (x?.tag) === \"string\");\n}\n\n/**\n * Walk an object's enumerable properties to find those fit for telemetry.\n */\nfunction getValidTelemetryProps(obj: any, keysToOmit: Set<string>): ITelemetryProperties {\n const props: ITelemetryProperties = {};\n for (const key of Object.keys(obj)) {\n if (keysToOmit.has(key)) {\n continue;\n }\n const val = obj[key];\n switch (typeof val) {\n case \"string\":\n case \"number\":\n case \"boolean\":\n case \"undefined\":\n props[key] = val;\n break;\n default: {\n if (isTaggedTelemetryPropertyValue(val)) {\n props[key] = val;\n } else {\n // We don't support logging arbitrary objects\n props[key] = \"REDACTED (arbitrary object)\";\n }\n break;\n }\n }\n }\n return props;\n}\n\n/**\n * Borrowed from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#examples\n * Avoids runtime errors with circular references.\n * Not ideal, as will cut values that are not necessarily circular references.\n * Could be improved by implementing Node's util.inspect() for browser (minus all the coloring code)\n*/\nexport const getCircularReplacer = () => {\n const seen = new WeakSet();\n return (key: string, value: any): any => {\n if (typeof value === \"object\" && value !== null) {\n if (seen.has(value)) {\n return \"<removed/circular>\";\n }\n seen.add(value);\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return value;\n };\n};\n\n/**\n * Base class for \"trusted\" errors we create, whose properties can generally be logged to telemetry safely.\n * All properties set on the object, or passed in (via the constructor or getTelemetryProperties),\n * will be logged in accordance with their tag, if present.\n *\n * PLEASE take care to avoid setting sensitive data on this object without proper tagging!\n */\nexport class LoggingError extends Error implements ILoggingError, Pick<IFluidErrorBase, \"errorInstanceId\"> {\n private _errorInstanceId = uuid();\n get errorInstanceId() { return this._errorInstanceId; }\n overwriteErrorInstanceId(id: string) { this._errorInstanceId = id; }\n\n /** Back-compat to appease isFluidError typeguard in old code that may handle this error */\n // @ts-expect-error - This field shouldn't be referenced in the current version, but needs to exist at runtime.\n private readonly fluidErrorCode: \"-\" = \"-\";\n\n /**\n * Create a new LoggingError\n * @param message - Error message to use for Error base class\n * @param props - telemetry props to include on the error for when it's logged\n * @param omitPropsFromLogging - properties by name to omit from telemetry props\n */\n constructor(\n message: string,\n props?: ITelemetryProperties,\n private readonly omitPropsFromLogging: Set<string> = new Set(),\n ) {\n super(message);\n\n // Don't log this list itself, or the private _errorInstanceId\n omitPropsFromLogging.add(\"omitPropsFromLogging\");\n omitPropsFromLogging.add(\"_errorInstanceId\");\n\n if (props) {\n this.addTelemetryProperties(props);\n }\n }\n\n /**\n * Add additional properties to be logged\n */\n public addTelemetryProperties(props: ITelemetryProperties) {\n copyProps(this, props);\n }\n\n /**\n * Get all properties fit to be logged to telemetry for this error\n */\n public getTelemetryProperties(): ITelemetryProperties {\n const taggableProps = getValidTelemetryProps(this, this.omitPropsFromLogging);\n // Include non-enumerable props that are not returned by getValidTelemetryProps\n return {\n ...taggableProps,\n stack: this.stack,\n message: this.message,\n errorInstanceId: this._errorInstanceId,\n };\n }\n}\n\n/** Simple implementation of IFluidErrorBase, extending LoggingError */\nclass SimpleFluidError extends LoggingError implements IFluidErrorBase {\n readonly errorType: string;\n\n constructor(\n errorProps: Pick<IFluidErrorBase,\n | \"message\"\n | \"stack\"\n | \"errorType\"\n >,\n ) {\n super(errorProps.message);\n this.errorType = errorProps.errorType;\n if (errorProps.stack !== undefined) {\n overwriteStack(this, errorProps.stack);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"errorLogging.js","sourceRoot":"","sources":["../src/errorLogging.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAQH,+BAAkC;AAClC,qDAK0B;AAE1B,wEAAwE;AACxE,MAAM,eAAe,GAAG,CAAC,KAAU,EAAW,EAAE;IAC5C,OAAO,KAAK,KAA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CAAC;QAevC,8DAA8D;QAC9D,oBAAoB,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;QACjD,oBAAoB,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;QAE7C,IAAI,KAAK,EAAE;YACP,IAAI,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC;SACtC;IACL,CAAC;IA3BD,IAAI,eAAe,KAAK,OAAO,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC;IACvD,wBAAwB,CAAC,EAAU,IAAI,IAAI,CAAC,gBAAgB,GAAG,EAAE,CAAC,CAAC,CAAC;IA4BpE;;OAEG;IACI,sBAAsB,CAAC,KAA2B;QACrD,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC3B,CAAC;IAED;;OAEG;IACI,sBAAsB;QACzB,MAAM,aAAa,GAAG,sBAAsB,CAAC,IAAI,EAAE,IAAI,CAAC,oBAAoB,CAAC,CAAC;QAC9E,+EAA+E;QAC/E,uCACO,aAAa,KAChB,KAAK,EAAE,IAAI,CAAC,KAAK,EACjB,OAAO,EAAE,IAAI,CAAC,OAAO,EACrB,eAAe,EAAE,IAAI,CAAC,gBAAgB,IACxC;IACN,CAAC;CACJ;AAnDD,oCAmDC;AAED,8DAA8D;AAC9D,MAAM,uBAAwB,SAAQ,YAAY;IAO9C,YACI,UAGC;QAED,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAR9B,cAAS,GAAG,uBAAuB,CAAC,mBAAmB,CAAC;QAUpD,IAAI,UAAU,CAAC,KAAK,KAAK,SAAS,EAAE;YAChC,cAAc,CAAC,IAAI,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;SAC1C;IACL,CAAC;;AAjBD,2EAA2E;AAC3E,2EAA2E;AAC3D,2CAAmB,GAAG,cAAc,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n ILoggingError,\n ITaggedTelemetryPropertyType,\n ITelemetryLogger,\n ITelemetryProperties,\n} from \"@fluidframework/common-definitions\";\nimport { v4 as uuid } from \"uuid\";\nimport {\n hasErrorInstanceId,\n IFluidErrorBase,\n isFluidError,\n isValidLegacyError,\n} from \"./fluidErrorBase\";\n\n/** @returns true if value is an object but neither null nor an array */\nconst isRegularObject = (value: any): boolean => {\n return value !== null && !Array.isArray(value) && typeof value === \"object\";\n};\n\n/** Inspect the given error for common \"safe\" props and return them */\nexport function extractLogSafeErrorProperties(error: any, sanitizeStack: boolean) {\n const removeMessageFromStack = (stack: string, errorName?: string) => {\n if (!sanitizeStack) {\n return stack;\n }\n const stackFrames = stack.split(\"\\n\");\n stackFrames.shift(); // Remove \"[ErrorName]: [ErrorMessage]\"\n if (errorName !== undefined) {\n stackFrames.unshift(errorName); // Add \"[ErrorName]\"\n }\n return stackFrames.join(\"\\n\");\n };\n\n const message = (typeof error?.message === \"string\")\n ? error.message as string\n : String(error);\n\n const safeProps: { message: string; errorType?: string; stack?: string; } = {\n message,\n };\n\n if (isRegularObject(error)) {\n const { errorType, stack, name } = error;\n\n if (typeof errorType === \"string\") {\n safeProps.errorType = errorType;\n }\n\n if (typeof stack === \"string\") {\n const errorName = (typeof name === \"string\") ? name : undefined;\n safeProps.stack = removeMessageFromStack(stack, errorName);\n }\n }\n\n return safeProps;\n}\n\n/** type guard for ILoggingError interface */\nexport const isILoggingError = (x: any): x is ILoggingError => typeof x?.getTelemetryProperties === \"function\";\n\n/** Copy props from source onto target, but do not overwrite an existing prop that matches */\nfunction copyProps(target: ITelemetryProperties | LoggingError, source: ITelemetryProperties) {\n for (const key of Object.keys(source)) {\n if (target[key] === undefined) {\n target[key] = source[key];\n }\n }\n}\n\n/** Metadata to annotate an error object when annotating or normalizing it */\nexport interface IFluidErrorAnnotations {\n /** Telemetry props to log with the error */\n props?: ITelemetryProperties;\n}\n\n/** For backwards compatibility with pre-errorInstanceId valid errors */\nfunction patchLegacyError(\n legacyError: Omit<IFluidErrorBase, \"errorInstanceId\">,\n): asserts legacyError is IFluidErrorBase {\n const patchMe: { -readonly [P in \"errorInstanceId\"]?: IFluidErrorBase[P] } = legacyError as any;\n if (patchMe.errorInstanceId === undefined) {\n patchMe.errorInstanceId = uuid();\n }\n}\n\n/**\n * Normalize the given error yielding a valid Fluid Error\n * @returns A valid Fluid Error with any provided annotations applied\n * @param error - The error to normalize\n * @param annotations - Annotations to apply to the normalized error\n */\nexport function normalizeError(\n error: unknown,\n annotations: IFluidErrorAnnotations = {},\n): IFluidErrorBase {\n // Back-compat, while IFluidErrorBase is rolled out\n if (isValidLegacyError(error)) {\n patchLegacyError(error);\n }\n\n if (isFluidError(error)) {\n // We can simply add the telemetry props to the error and return it\n error.addTelemetryProperties(annotations.props ?? {});\n return error;\n }\n\n // We have to construct a new Fluid Error, copying safe properties over\n const { message, stack } = extractLogSafeErrorProperties(error, false /* sanitizeStack */);\n const fluidError: IFluidErrorBase = new NormalizedExternalError({\n message,\n stack,\n });\n\n // We need to preserve these properties which are used in a non-typesafe way throughout driver code (see #8743)\n // Anywhere they are set should be on a valid Fluid Error that would have been returned above,\n // but we can't prove it with the types, so adding this defensive measure.\n if (typeof error === \"object\" && error !== null) {\n const { canRetry, retryAfterSeconds } = error as any;\n Object.assign(normalizeError, { canRetry, retryAfterSeconds });\n }\n\n if (typeof (error) !== \"object\") {\n // This is only interesting for non-objects\n fluidError.addTelemetryProperties({ typeofError: typeof (error) });\n }\n\n const originalErrorTelemetryProps = isILoggingError(error) ? error.getTelemetryProperties() : {};\n fluidError.addTelemetryProperties({\n ...originalErrorTelemetryProps,\n ...annotations.props,\n untrustedOrigin: 1, // This will let us filter to errors not originated by our own code\n });\n\n return fluidError;\n}\n\nlet stackPopulatedOnCreation: boolean | undefined;\n\n/**\n * The purpose of this function is to provide ability to capture stack context quickly.\n * Accessing new Error().stack is slow, and the slowest part is accessing stack property itself.\n * There are scenarios where we generate error with stack, but error is handled in most cases and\n * stack property is not accessed.\n * For such cases it's better to not read stack property right away, but rather delay it until / if it's needed\n * Some browsers will populate stack right away, others require throwing Error, so we do auto-detection on the fly.\n * @returns Error object that has stack populated.\n */\n export function generateErrorWithStack(): Error {\n const err = new Error(\"<<generated stack>>\");\n\n if (stackPopulatedOnCreation === undefined) {\n stackPopulatedOnCreation = (err.stack !== undefined);\n }\n\n if (stackPopulatedOnCreation) {\n return err;\n }\n\n try {\n throw err;\n } catch (e) {\n return e as Error;\n }\n}\n\nexport function generateStack(): string | undefined {\n return generateErrorWithStack().stack;\n}\n\n/**\n * Create a new error using newErrorFn, wrapping and caused by the given unknown error.\n * Copies the inner error's stack, errorInstanceId and telemetry props over to the new error if present\n * @param innerError - An error from untrusted/unknown origins\n * @param newErrorFn - callback that will create a new error given the original error's message\n * @returns A new error object \"wrapping\" the given error\n */\nexport function wrapError<T extends LoggingError>(\n innerError: unknown,\n newErrorFn: (message: string) => T,\n): T {\n const {\n message,\n stack,\n } = extractLogSafeErrorProperties(innerError, false /* sanitizeStack */);\n\n const newError = newErrorFn(message);\n\n if (stack !== undefined) {\n overwriteStack(newError, stack);\n }\n\n // Mark external errors with untrustedOrigin flag\n if (isExternalError(innerError)) {\n newError.addTelemetryProperties({ untrustedOrigin: 1 });\n }\n\n // Reuse errorInstanceId\n if (hasErrorInstanceId(innerError)) {\n newError.overwriteErrorInstanceId(innerError.errorInstanceId);\n\n // For \"back-compat\" in the logs\n newError.addTelemetryProperties({ innerErrorInstanceId: innerError.errorInstanceId });\n }\n\n // Lastly, copy over all other telemetry properties. Note these will not overwrite existing properties\n // This will include the untrustedOrigin property if the inner error itself was created from an external error\n if (isILoggingError(innerError)) {\n newError.addTelemetryProperties(innerError.getTelemetryProperties());\n }\n\n return newError;\n}\n\n/** The same as wrapError, but also logs the innerError, including the wrapping error's instance id */\nexport function wrapErrorAndLog<T extends LoggingError>(\n innerError: unknown,\n newErrorFn: (message: string) => T,\n logger: ITelemetryLogger,\n) {\n const newError = wrapError(innerError, newErrorFn);\n\n // This will match innerError.errorInstanceId if present (see wrapError)\n const errorInstanceId = newError.errorInstanceId;\n\n // For \"back-compat\" in the logs\n const wrappedByErrorInstanceId = errorInstanceId;\n\n logger.sendTelemetryEvent({\n eventName: \"WrapError\",\n errorInstanceId,\n wrappedByErrorInstanceId,\n }, innerError);\n\n return newError;\n}\n\nfunction overwriteStack(error: IFluidErrorBase | LoggingError, stack: string) {\n // supposedly setting stack on an Error can throw.\n try {\n Object.assign(error, { stack });\n } catch (errorSettingStack) {\n error.addTelemetryProperties({ stack2: stack });\n }\n}\n\n/**\n * True for any error object that is an (optionally normalized) external error\n * False for any error we created and raised within the FF codebase, or wrapped in a well-known error type\n */\nexport function isExternalError(e: any): boolean {\n return !isValidLegacyError(e) ||\n (e.getTelemetryProperties().untrustedOrigin === 1 &&\n e.errorType === NormalizedExternalError.normalizedErrorType);\n}\n\n/**\n * Type guard to identify if a particular value (loosely) appears to be a tagged telemetry property\n */\nexport function isTaggedTelemetryPropertyValue(x: any): x is ITaggedTelemetryPropertyType {\n return (typeof (x?.value) !== \"object\" && typeof (x?.tag) === \"string\");\n}\n\n/**\n * Walk an object's enumerable properties to find those fit for telemetry.\n */\nfunction getValidTelemetryProps(obj: any, keysToOmit: Set<string>): ITelemetryProperties {\n const props: ITelemetryProperties = {};\n for (const key of Object.keys(obj)) {\n if (keysToOmit.has(key)) {\n continue;\n }\n const val = obj[key];\n switch (typeof val) {\n case \"string\":\n case \"number\":\n case \"boolean\":\n case \"undefined\":\n props[key] = val;\n break;\n default: {\n if (isTaggedTelemetryPropertyValue(val)) {\n props[key] = val;\n } else {\n // We don't support logging arbitrary objects\n props[key] = \"REDACTED (arbitrary object)\";\n }\n break;\n }\n }\n }\n return props;\n}\n\n/**\n * Borrowed from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#examples\n * Avoids runtime errors with circular references.\n * Not ideal, as will cut values that are not necessarily circular references.\n * Could be improved by implementing Node's util.inspect() for browser (minus all the coloring code)\n*/\nexport const getCircularReplacer = () => {\n const seen = new WeakSet();\n return (key: string, value: any): any => {\n if (typeof value === \"object\" && value !== null) {\n if (seen.has(value)) {\n return \"<removed/circular>\";\n }\n seen.add(value);\n }\n return value;\n };\n};\n\n/**\n * Base class for \"trusted\" errors we create, whose properties can generally be logged to telemetry safely.\n * All properties set on the object, or passed in (via the constructor or addTelemetryProperties),\n * will be logged in accordance with their tag, if present.\n *\n * PLEASE take care to avoid setting sensitive data on this object without proper tagging!\n */\nexport class LoggingError extends Error implements ILoggingError, Omit<IFluidErrorBase, \"errorType\"> {\n private _errorInstanceId = uuid();\n get errorInstanceId() { return this._errorInstanceId; }\n overwriteErrorInstanceId(id: string) { this._errorInstanceId = id; }\n\n /** Back-compat to appease isFluidError typeguard in old code that may handle this error */\n // @ts-expect-error - This field shouldn't be referenced in the current version, but needs to exist at runtime.\n private readonly fluidErrorCode: \"-\" = \"-\";\n\n /**\n * Create a new LoggingError\n * @param message - Error message to use for Error base class\n * @param props - telemetry props to include on the error for when it's logged\n * @param omitPropsFromLogging - properties by name to omit from telemetry props\n */\n constructor(\n message: string,\n props?: ITelemetryProperties,\n private readonly omitPropsFromLogging: Set<string> = new Set(),\n ) {\n super(message);\n\n // Don't log this list itself, or the private _errorInstanceId\n omitPropsFromLogging.add(\"omitPropsFromLogging\");\n omitPropsFromLogging.add(\"_errorInstanceId\");\n\n if (props) {\n this.addTelemetryProperties(props);\n }\n }\n\n /**\n * Add additional properties to be logged\n */\n public addTelemetryProperties(props: ITelemetryProperties) {\n copyProps(this, props);\n }\n\n /**\n * Get all properties fit to be logged to telemetry for this error\n */\n public getTelemetryProperties(): ITelemetryProperties {\n const taggableProps = getValidTelemetryProps(this, this.omitPropsFromLogging);\n // Include non-enumerable props that are not returned by getValidTelemetryProps\n return {\n ...taggableProps,\n stack: this.stack,\n message: this.message,\n errorInstanceId: this._errorInstanceId,\n };\n }\n}\n\n/** The Error class used when normalizing an external error */\nclass NormalizedExternalError extends LoggingError {\n // errorType \"genericError\" is used as a default value throughout the code.\n // Note that this matches ContainerErrorType/DriverErrorType's genericError\n static readonly normalizedErrorType = \"genericError\";\n\n errorType = NormalizedExternalError.normalizedErrorType;\n\n constructor(\n errorProps: Pick<IFluidErrorBase,\n | \"message\"\n | \"stack\"\n >,\n ) {\n super(errorProps.message);\n\n if (errorProps.stack !== undefined) {\n overwriteStack(this, errorProps.stack);\n }\n }\n}\n"]}
@@ -5,5 +5,5 @@
5
5
  * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
6
6
  */
7
7
  export declare const pkgName = "@fluidframework/telemetry-utils";
8
- export declare const pkgVersion = "1.0.1";
8
+ export declare const pkgVersion = "1.1.0";
9
9
  //# sourceMappingURL=packageVersion.d.ts.map
@@ -8,5 +8,5 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.pkgVersion = exports.pkgName = void 0;
10
10
  exports.pkgName = "@fluidframework/telemetry-utils";
11
- exports.pkgVersion = "1.0.1";
11
+ exports.pkgVersion = "1.1.0";
12
12
  //# sourceMappingURL=packageVersion.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,iCAAiC,CAAC;AAC5C,QAAA,UAAU,GAAG,OAAO,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/telemetry-utils\";\nexport const pkgVersion = \"1.0.1\";\n"]}
1
+ {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,iCAAiC,CAAC;AAC5C,QAAA,UAAU,GAAG,OAAO,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/telemetry-utils\";\nexport const pkgVersion = \"1.1.0\";\n"]}
@@ -36,9 +36,8 @@ export declare function normalizeError(error: unknown, annotations?: IFluidError
36
36
  export declare function generateErrorWithStack(): Error;
37
37
  export declare function generateStack(): string | undefined;
38
38
  /**
39
- * Create a new error, wrapping and caused by the given unknown error.
40
- * Copies the inner error's message and stack over but otherwise uses newErrorFn to define the error.
41
- * The inner error's instance id will also be logged for telemetry analysis.
39
+ * Create a new error using newErrorFn, wrapping and caused by the given unknown error.
40
+ * Copies the inner error's stack, errorInstanceId and telemetry props over to the new error if present
42
41
  * @param innerError - An error from untrusted/unknown origins
43
42
  * @param newErrorFn - callback that will create a new error given the original error's message
44
43
  * @returns A new error object "wrapping" the given error
@@ -46,11 +45,6 @@ export declare function generateStack(): string | undefined;
46
45
  export declare function wrapError<T extends LoggingError>(innerError: unknown, newErrorFn: (message: string) => T): T;
47
46
  /** The same as wrapError, but also logs the innerError, including the wrapping error's instance id */
48
47
  export declare function wrapErrorAndLog<T extends LoggingError>(innerError: unknown, newErrorFn: (message: string) => T, logger: ITelemetryLogger): T;
49
- /**
50
- * True for any error object that is either external itself or is a wrapped/normalized external error
51
- * False for any error we created and raised within the FF codebase.
52
- */
53
- export declare function originatedAsExternalError(e: any): boolean;
54
48
  /**
55
49
  * True for any error object that is an (optionally normalized) external error
56
50
  * False for any error we created and raised within the FF codebase, or wrapped in a well-known error type
@@ -69,12 +63,12 @@ export declare function isTaggedTelemetryPropertyValue(x: any): x is ITaggedTele
69
63
  export declare const getCircularReplacer: () => (key: string, value: any) => any;
70
64
  /**
71
65
  * Base class for "trusted" errors we create, whose properties can generally be logged to telemetry safely.
72
- * All properties set on the object, or passed in (via the constructor or getTelemetryProperties),
66
+ * All properties set on the object, or passed in (via the constructor or addTelemetryProperties),
73
67
  * will be logged in accordance with their tag, if present.
74
68
  *
75
69
  * PLEASE take care to avoid setting sensitive data on this object without proper tagging!
76
70
  */
77
- export declare class LoggingError extends Error implements ILoggingError, Pick<IFluidErrorBase, "errorInstanceId"> {
71
+ export declare class LoggingError extends Error implements ILoggingError, Omit<IFluidErrorBase, "errorType"> {
78
72
  private readonly omitPropsFromLogging;
79
73
  private _errorInstanceId;
80
74
  get errorInstanceId(): string;
@@ -1 +1 @@
1
- {"version":3,"file":"errorLogging.d.ts","sourceRoot":"","sources":["../src/errorLogging.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACH,aAAa,EACb,4BAA4B,EAC5B,gBAAgB,EAChB,oBAAoB,EACvB,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EAEH,eAAe,EAGlB,MAAM,kBAAkB,CAAC;AAO1B,sEAAsE;AACtE,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,GAAG,EAAE,aAAa,EAAE,OAAO;aAiBhD,MAAM;;;EAkBrC;AAED,6CAA6C;AAC7C,eAAO,MAAM,eAAe,MAAO,GAAG,uBAAwE,CAAC;AAW/G,6EAA6E;AAC7E,MAAM,WAAW,sBAAsB;IACnC,4CAA4C;IAC5C,KAAK,CAAC,EAAE,oBAAoB,CAAC;CAChC;AAgBD;;;;;GAKG;AACH,wBAAgB,cAAc,CAC1B,KAAK,EAAE,OAAO,EACd,WAAW,GAAE,sBAA2B,GACzC,eAAe,CAsCjB;AAID;;;;;;;;GAQG;AACF,wBAAgB,sBAAsB,IAAI,KAAK,CAgB/C;AAED,wBAAgB,aAAa,IAAI,MAAM,GAAG,SAAS,CAElD;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,YAAY,EAC5C,UAAU,EAAE,OAAO,EACnB,UAAU,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,CAAC,GACnC,CAAC,CA0BH;AAED,sGAAsG;AACtG,wBAAgB,eAAe,CAAC,CAAC,SAAS,YA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gB,CAAU;IAClC,IAAI,eAAe,WAAoC;IACvD,wBAAwB,CAAC,EAAE,EAAE,MAAM;IAEnC,2FAA2F;IAE3F,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAY;IAE3C;;;;;OAKG;gBAEC,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,oBAAoB,EACX,oBAAoB,GAAE,GAAG,CAAC,MAAM,CAAa;IAalE;;OAEG;IACI,sBAAsB,CAAC,KAAK,EAAE,oBAAoB;IAIzD;;OAEG;IACI,sBAAsB,IAAI,oBAAoB;CAUxD"}
@@ -56,9 +56,6 @@ function patchLegacyError(legacyError) {
56
56
  patchMe.errorInstanceId = uuid();
57
57
  }
58
58
  }
59
- // errorType "genericError" is used as a default value throughout the code.
60
- // Note that this matches ContainerErrorType/DriverErrorType's genericError
61
- const defaultErrorTypeForNormalize = "genericError";
62
59
  /**
63
60
  * Normalize the given error yielding a valid Fluid Error
64
61
  * @returns A valid Fluid Error with any provided annotations applied
@@ -78,12 +75,10 @@ export function normalizeError(error, annotations = {}) {
78
75
  }
79
76
  // We have to construct a new Fluid Error, copying safe properties over
80
77
  const { message, stack } = extractLogSafeErrorProperties(error, false /* sanitizeStack */);
81
- const fluidError = new SimpleFluidError({
82
- errorType: defaultErrorTypeForNormalize,
78
+ const fluidError = new NormalizedExternalError({
83
79
  message,
84
80
  stack,
85
81
  });
86
- fluidError.addTelemetryProperties(Object.assign(Object.assign({}, annotations.props), { untrustedOrigin: 1 }));
87
82
  // We need to preserve these properties which are used in a non-typesafe way throughout driver code (see #8743)
88
83
  // Anywhere they are set should be on a valid Fluid Error that would have been returned above,
89
84
  // but we can't prove it with the types, so adding this defensive measure.
@@ -95,6 +90,8 @@ export function normalizeError(error, annotations = {}) {
95
90
  // This is only interesting for non-objects
96
91
  fluidError.addTelemetryProperties({ typeofError: typeof (error) });
97
92
  }
93
+ const originalErrorTelemetryProps = isILoggingError(error) ? error.getTelemetryProperties() : {};
94
+ fluidError.addTelemetryProperties(Object.assign(Object.assign(Object.assign({}, originalErrorTelemetryProps), annotations.props), { untrustedOrigin: 1 }));
98
95
  return fluidError;
99
96
  }
100
97
  let stackPopulatedOnCreation;
@@ -126,9 +123,8 @@ export function generateStack() {
126
123
  return generateErrorWithStack().stack;
127
124
  }
128
125
  /**
129
- * Create a new error, wrapping and caused by the given unknown error.
130
- * Copies the inner error's message and stack over but otherwise uses newErrorFn to define the error.
131
- * The inner error's instance id will also be logged for telemetry analysis.
126
+ * Create a new error using newErrorFn, wrapping and caused by the given unknown error.
127
+ * Copies the inner error's stack, errorInstanceId and telemetry props over to the new error if present
132
128
  * @param innerError - An error from untrusted/unknown origins
133
129
  * @param newErrorFn - callback that will create a new error given the original error's message
134
130
  * @returns A new error object "wrapping" the given error
@@ -140,7 +136,7 @@ export function wrapError(innerError, newErrorFn) {
140
136
  overwriteStack(newError, stack);
141
137
  }
142
138
  // Mark external errors with untrustedOrigin flag
143
- if (originatedAsExternalError(innerError)) {
139
+ if (isExternalError(innerError)) {
144
140
  newError.addTelemetryProperties({ untrustedOrigin: 1 });
145
141
  }
146
142
  // Reuse errorInstanceId
@@ -149,6 +145,11 @@ export function wrapError(innerError, newErrorFn) {
149
145
  // For "back-compat" in the logs
150
146
  newError.addTelemetryProperties({ innerErrorInstanceId: innerError.errorInstanceId });
151
147
  }
148
+ // Lastly, copy over all other telemetry properties. Note these will not overwrite existing properties
149
+ // This will include the untrustedOrigin property if the inner error itself was created from an external error
150
+ if (isILoggingError(innerError)) {
151
+ newError.addTelemetryProperties(innerError.getTelemetryProperties());
152
+ }
152
153
  return newError;
153
154
  }
154
155
  /** The same as wrapError, but also logs the innerError, including the wrapping error's instance id */
@@ -174,13 +175,6 @@ function overwriteStack(error, stack) {
174
175
  error.addTelemetryProperties({ stack2: stack });
175
176
  }
176
177
  }
177
- /**
178
- * True for any error object that is either external itself or is a wrapped/normalized external error
179
- * False for any error we created and raised within the FF codebase.
180
- */
181
- export function originatedAsExternalError(e) {
182
- return !isValidLegacyError(e) || (e.getTelemetryProperties().untrustedOrigin === 1);
183
- }
184
178
  /**
185
179
  * True for any error object that is an (optionally normalized) external error
186
180
  * False for any error we created and raised within the FF codebase, or wrapped in a well-known error type
@@ -188,7 +182,7 @@ export function originatedAsExternalError(e) {
188
182
  export function isExternalError(e) {
189
183
  return !isValidLegacyError(e) ||
190
184
  (e.getTelemetryProperties().untrustedOrigin === 1 &&
191
- e.errorType === defaultErrorTypeForNormalize);
185
+ e.errorType === NormalizedExternalError.normalizedErrorType);
192
186
  }
193
187
  /**
194
188
  * Type guard to identify if a particular value (loosely) appears to be a tagged telemetry property
@@ -242,13 +236,12 @@ export const getCircularReplacer = () => {
242
236
  }
243
237
  seen.add(value);
244
238
  }
245
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
246
239
  return value;
247
240
  };
248
241
  };
249
242
  /**
250
243
  * Base class for "trusted" errors we create, whose properties can generally be logged to telemetry safely.
251
- * All properties set on the object, or passed in (via the constructor or getTelemetryProperties),
244
+ * All properties set on the object, or passed in (via the constructor or addTelemetryProperties),
252
245
  * will be logged in accordance with their tag, if present.
253
246
  *
254
247
  * PLEASE take care to avoid setting sensitive data on this object without proper tagging!
@@ -291,14 +284,17 @@ export class LoggingError extends Error {
291
284
  return Object.assign(Object.assign({}, taggableProps), { stack: this.stack, message: this.message, errorInstanceId: this._errorInstanceId });
292
285
  }
293
286
  }
294
- /** Simple implementation of IFluidErrorBase, extending LoggingError */
295
- class SimpleFluidError extends LoggingError {
287
+ /** The Error class used when normalizing an external error */
288
+ class NormalizedExternalError extends LoggingError {
296
289
  constructor(errorProps) {
297
290
  super(errorProps.message);
298
- this.errorType = errorProps.errorType;
291
+ this.errorType = NormalizedExternalError.normalizedErrorType;
299
292
  if (errorProps.stack !== undefined) {
300
293
  overwriteStack(this, errorProps.stack);
301
294
  }
302
295
  }
303
296
  }
297
+ // errorType "genericError" is used as a default value throughout the code.
298
+ // Note that this matches ContainerErrorType/DriverErrorType's genericError
299
+ NormalizedExternalError.normalizedErrorType = "genericError";
304
300
  //# sourceMappingURL=errorLogging.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"errorLogging.js","sourceRoot":"","sources":["../src/errorLogging.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EACH,kBAAkB,EAElB,YAAY,EACZ,kBAAkB,GACrB,MAAM,kBAAkB,CAAC;AAE1B,wEAAwE;AACxE,MAAM,eAAe,GAAG,CAAC,KAAU,EAAW,EAAE;IAC5C,OAAO,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAC;AAChF,CAAC,CAAC;AAEF,sEAAsE;AACtE,MAAM,UAAU,6BAA6B,CAAC,KAAU,EAAE,aAAsB;IAC5E,MAAM,sBAAsB,GAAG,CAAC,KAAa,EAAE,SAAkB,EAAE,EAAE;QACjE,IAAI,CAAC,aAAa,EAAE;YAChB,OAAO,KAAK,CAAC;SAChB;QACD,MAAM,WAAW,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACtC,WAAW,CAAC,KAAK,EAAE,CAAC,CAAC,uCAAuC;QAC5D,IAAI,SAAS,KAAK,SAAS,EAAE;YACzB,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,oBAAoB;SACvD;QACD,OAAO,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC,CAAC;IAEF,MAAM,OAAO,GAAG,CAAC,OAAO,CAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,OAAO,CAAA,KAAK,QAAQ,CAAC;QAChD,CAAC,CAAC,KAAK,CAAC,OAAiB;QACzB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAEpB,MAAM,SAAS,GAA6D;QACxE,OAAO;KACV,CAAC;IAEF,IAAI,e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AC,CAAC;SAC1C;IACL,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n ILoggingError,\n ITaggedTelemetryPropertyType,\n ITelemetryLogger,\n ITelemetryProperties,\n} from \"@fluidframework/common-definitions\";\nimport { v4 as uuid } from \"uuid\";\nimport {\n hasErrorInstanceId,\n IFluidErrorBase,\n isFluidError,\n isValidLegacyError,\n} from \"./fluidErrorBase\";\n\n/** @returns true if value is an object but neither null nor an array */\nconst isRegularObject = (value: any): boolean => {\n return value !== null && !Array.isArray(value) && typeof value === \"object\";\n};\n\n/** Inspect the given error for common \"safe\" props and return them */\nexport function extractLogSafeErrorProperties(error: any, sanitizeStack: boolean) {\n const removeMessageFromStack = (stack: string, errorName?: string) => {\n if (!sanitizeStack) {\n return stack;\n }\n const stackFrames = stack.split(\"\\n\");\n stackFrames.shift(); // Remove \"[ErrorName]: [ErrorMessage]\"\n if (errorName !== undefined) {\n stackFrames.unshift(errorName); // Add \"[ErrorName]\"\n }\n return stackFrames.join(\"\\n\");\n };\n\n const message = (typeof error?.message === \"string\")\n ? error.message as string\n : String(error);\n\n const safeProps: { message: string; errorType?: string; stack?: string; } = {\n message,\n };\n\n if (isRegularObject(error)) {\n const { errorType, stack, name } = error;\n\n if (typeof errorType === \"string\") {\n safeProps.errorType = errorType;\n }\n\n if (typeof stack === \"string\") {\n const errorName = (typeof name === \"string\") ? name : undefined;\n safeProps.stack = removeMessageFromStack(stack, errorName);\n }\n }\n\n return safeProps;\n}\n\n/** type guard for ILoggingError interface */\nexport const isILoggingError = (x: any): x is ILoggingError => typeof x?.getTelemetryProperties === \"function\";\n\n/** Copy props from source onto target, but do not overwrite an existing prop that matches */\nfunction copyProps(target: ITelemetryProperties | LoggingError, source: ITelemetryProperties) {\n for (const key of Object.keys(source)) {\n if (target[key] === undefined) {\n target[key] = source[key];\n }\n }\n}\n\n/** Metadata to annotate an error object when annotating or normalizing it */\nexport interface IFluidErrorAnnotations {\n /** Telemetry props to log with the error */\n props?: ITelemetryProperties;\n}\n\n/** For backwards compatibility with pre-errorInstanceId valid errors */\nfunction patchLegacyError(\n legacyError: Omit<IFluidErrorBase, \"errorInstanceId\">,\n): asserts legacyError is IFluidErrorBase {\n const patchMe: { -readonly [P in \"errorInstanceId\"]?: IFluidErrorBase[P] } = legacyError as any;\n if (patchMe.errorInstanceId === undefined) {\n patchMe.errorInstanceId = uuid();\n }\n}\n\n// errorType \"genericError\" is used as a default value throughout the code.\n// Note that this matches ContainerErrorType/DriverErrorType's genericError\nconst defaultErrorTypeForNormalize = \"genericError\";\n\n/**\n * Normalize the given error yielding a valid Fluid Error\n * @returns A valid Fluid Error with any provided annotations applied\n * @param error - The error to normalize\n * @param annotations - Annotations to apply to the normalized error\n */\nexport function normalizeError(\n error: unknown,\n annotations: IFluidErrorAnnotations = {},\n): IFluidErrorBase {\n // Back-compat, while IFluidErrorBase is rolled out\n if (isValidLegacyError(error)) {\n patchLegacyError(error);\n }\n\n if (isFluidError(error)) {\n // We can simply add the telemetry props to the error and return it\n error.addTelemetryProperties(annotations.props ?? {});\n return error;\n }\n\n // We have to construct a new Fluid Error, copying safe properties over\n const { message, stack } = extractLogSafeErrorProperties(error, false /* sanitizeStack */);\n const fluidError: IFluidErrorBase = new SimpleFluidError({\n errorType: defaultErrorTypeForNormalize,\n message,\n stack,\n });\n\n fluidError.addTelemetryProperties({\n ...annotations.props,\n untrustedOrigin: 1, // This will let us filter to errors not originated by our own code\n });\n\n // We need to preserve these properties which are used in a non-typesafe way throughout driver code (see #8743)\n // Anywhere they are set should be on a valid Fluid Error that would have been returned above,\n // but we can't prove it with the types, so adding this defensive measure.\n if (typeof error === \"object\" && error !== null) {\n const { canRetry, retryAfterSeconds } = error as any;\n Object.assign(normalizeError, { canRetry, retryAfterSeconds });\n }\n\n if (typeof (error) !== \"object\") {\n // This is only interesting for non-objects\n fluidError.addTelemetryProperties({ typeofError: typeof (error) });\n }\n return fluidError;\n}\n\nlet stackPopulatedOnCreation: boolean | undefined;\n\n/**\n * The purpose of this function is to provide ability to capture stack context quickly.\n * Accessing new Error().stack is slow, and the slowest part is accessing stack property itself.\n * There are scenarios where we generate error with stack, but error is handled in most cases and\n * stack property is not accessed.\n * For such cases it's better to not read stack property right away, but rather delay it until / if it's needed\n * Some browsers will populate stack right away, others require throwing Error, so we do auto-detection on the fly.\n * @returns Error object that has stack populated.\n */\n export function generateErrorWithStack(): Error {\n const err = new Error(\"<<generated stack>>\");\n\n if (stackPopulatedOnCreation === undefined) {\n stackPopulatedOnCreation = (err.stack !== undefined);\n }\n\n if (stackPopulatedOnCreation) {\n return err;\n }\n\n try {\n throw err;\n } catch (e) {\n return e as Error;\n }\n}\n\nexport function generateStack(): string | undefined {\n return generateErrorWithStack().stack;\n}\n\n/**\n * Create a new error, wrapping and caused by the given unknown error.\n * Copies the inner error's message and stack over but otherwise uses newErrorFn to define the error.\n * The inner error's instance id will also be logged for telemetry analysis.\n * @param innerError - An error from untrusted/unknown origins\n * @param newErrorFn - callback that will create a new error given the original error's message\n * @returns A new error object \"wrapping\" the given error\n */\nexport function wrapError<T extends LoggingError>(\n innerError: unknown,\n newErrorFn: (message: string) => T,\n): T {\n const {\n message,\n stack,\n } = extractLogSafeErrorProperties(innerError, false /* sanitizeStack */);\n\n const newError = newErrorFn(message);\n\n if (stack !== undefined) {\n overwriteStack(newError, stack);\n }\n\n // Mark external errors with untrustedOrigin flag\n if (originatedAsExternalError(innerError)) {\n newError.addTelemetryProperties({ untrustedOrigin: 1 });\n }\n\n // Reuse errorInstanceId\n if (hasErrorInstanceId(innerError)) {\n newError.overwriteErrorInstanceId(innerError.errorInstanceId);\n\n // For \"back-compat\" in the logs\n newError.addTelemetryProperties({ innerErrorInstanceId: innerError.errorInstanceId });\n }\n\n return newError;\n}\n\n/** The same as wrapError, but also logs the innerError, including the wrapping error's instance id */\nexport function wrapErrorAndLog<T extends LoggingError>(\n innerError: unknown,\n newErrorFn: (message: string) => T,\n logger: ITelemetryLogger,\n) {\n const newError = wrapError(innerError, newErrorFn);\n\n // This will match innerError.errorInstanceId if present (see wrapError)\n const errorInstanceId = newError.errorInstanceId;\n\n // For \"back-compat\" in the logs\n const wrappedByErrorInstanceId = errorInstanceId;\n\n logger.sendTelemetryEvent({\n eventName: \"WrapError\",\n errorInstanceId,\n wrappedByErrorInstanceId,\n }, innerError);\n\n return newError;\n}\n\nfunction overwriteStack(error: IFluidErrorBase | LoggingError, stack: string) {\n // supposedly setting stack on an Error can throw.\n try {\n Object.assign(error, { stack });\n } catch (errorSettingStack) {\n error.addTelemetryProperties({ stack2: stack });\n }\n}\n\n/**\n * True for any error object that is either external itself or is a wrapped/normalized external error\n * False for any error we created and raised within the FF codebase.\n */\nexport function originatedAsExternalError(e: any): boolean {\n return !isValidLegacyError(e) || (e.getTelemetryProperties().untrustedOrigin === 1);\n}\n\n/**\n * True for any error object that is an (optionally normalized) external error\n * False for any error we created and raised within the FF codebase, or wrapped in a well-known error type\n */\nexport function isExternalError(e: any): boolean {\n return !isValidLegacyError(e) ||\n (e.getTelemetryProperties().untrustedOrigin === 1 &&\n e.errorType === defaultErrorTypeForNormalize);\n}\n\n/**\n * Type guard to identify if a particular value (loosely) appears to be a tagged telemetry property\n */\nexport function isTaggedTelemetryPropertyValue(x: any): x is ITaggedTelemetryPropertyType {\n return (typeof (x?.value) !== \"object\" && typeof (x?.tag) === \"string\");\n}\n\n/**\n * Walk an object's enumerable properties to find those fit for telemetry.\n */\nfunction getValidTelemetryProps(obj: any, keysToOmit: Set<string>): ITelemetryProperties {\n const props: ITelemetryProperties = {};\n for (const key of Object.keys(obj)) {\n if (keysToOmit.has(key)) {\n continue;\n }\n const val = obj[key];\n switch (typeof val) {\n case \"string\":\n case \"number\":\n case \"boolean\":\n case \"undefined\":\n props[key] = val;\n break;\n default: {\n if (isTaggedTelemetryPropertyValue(val)) {\n props[key] = val;\n } else {\n // We don't support logging arbitrary objects\n props[key] = \"REDACTED (arbitrary object)\";\n }\n break;\n }\n }\n }\n return props;\n}\n\n/**\n * Borrowed from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#examples\n * Avoids runtime errors with circular references.\n * Not ideal, as will cut values that are not necessarily circular references.\n * Could be improved by implementing Node's util.inspect() for browser (minus all the coloring code)\n*/\nexport const getCircularReplacer = () => {\n const seen = new WeakSet();\n return (key: string, value: any): any => {\n if (typeof value === \"object\" && value !== null) {\n if (seen.has(value)) {\n return \"<removed/circular>\";\n }\n seen.add(value);\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return value;\n };\n};\n\n/**\n * Base class for \"trusted\" errors we create, whose properties can generally be logged to telemetry safely.\n * All properties set on the object, or passed in (via the constructor or getTelemetryProperties),\n * will be logged in accordance with their tag, if present.\n *\n * PLEASE take care to avoid setting sensitive data on this object without proper tagging!\n */\nexport class LoggingError extends Error implements ILoggingError, Pick<IFluidErrorBase, \"errorInstanceId\"> {\n private _errorInstanceId = uuid();\n get errorInstanceId() { return this._errorInstanceId; }\n overwriteErrorInstanceId(id: string) { this._errorInstanceId = id; }\n\n /** Back-compat to appease isFluidError typeguard in old code that may handle this error */\n // @ts-expect-error - This field shouldn't be referenced in the current version, but needs to exist at runtime.\n private readonly fluidErrorCode: \"-\" = \"-\";\n\n /**\n * Create a new LoggingError\n * @param message - Error message to use for Error base class\n * @param props - telemetry props to include on the error for when it's logged\n * @param omitPropsFromLogging - properties by name to omit from telemetry props\n */\n constructor(\n message: string,\n props?: ITelemetryProperties,\n private readonly omitPropsFromLogging: Set<string> = new Set(),\n ) {\n super(message);\n\n // Don't log this list itself, or the private _errorInstanceId\n omitPropsFromLogging.add(\"omitPropsFromLogging\");\n omitPropsFromLogging.add(\"_errorInstanceId\");\n\n if (props) {\n this.addTelemetryProperties(props);\n }\n }\n\n /**\n * Add additional properties to be logged\n */\n public addTelemetryProperties(props: ITelemetryProperties) {\n copyProps(this, props);\n }\n\n /**\n * Get all properties fit to be logged to telemetry for this error\n */\n public getTelemetryProperties(): ITelemetryProperties {\n const taggableProps = getValidTelemetryProps(this, this.omitPropsFromLogging);\n // Include non-enumerable props that are not returned by getValidTelemetryProps\n return {\n ...taggableProps,\n stack: this.stack,\n message: this.message,\n errorInstanceId: this._errorInstanceId,\n };\n }\n}\n\n/** Simple implementation of IFluidErrorBase, extending LoggingError */\nclass SimpleFluidError extends LoggingError implements IFluidErrorBase {\n readonly errorType: string;\n\n constructor(\n errorProps: Pick<IFluidErrorBase,\n | \"message\"\n | \"stack\"\n | \"errorType\"\n >,\n ) {\n super(errorProps.message);\n this.errorType = errorProps.errorType;\n if (errorProps.stack !== undefined) {\n overwriteStack(this, errorProps.stack);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"errorLogging.js","sourceRoot":"","sources":["../src/errorLogging.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EACH,kBAAkB,EAElB,YAAY,EACZ,kBAAkB,GACrB,MAAM,kBAAkB,CAAC;AAE1B,wEAAwE;AACxE,MAAM,eAAe,GAAG,CAAC,KAAU,EAAW,EAAE;IAC5C,OAAO,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAC;AAChF,CAAC,CAAC;AAEF,sEAAsE;AACtE,MAAM,UAAU,6BAA6B,CAAC,KAAU,EAAE,aAAsB;IAC5E,MAAM,sBAAsB,GAAG,CAAC,KAAa,EAAE,SAAkB,EAAE,EAAE;QACjE,IAAI,CAAC,aAAa,EAAE;YAChB,OAAO,KAAK,CAAC;SAChB;QACD,MAAM,WAAW,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACtC,WAAW,CAAC,KAAK,EAAE,CAAC,CAAC,uCAAuC;QAC5D,IAAI,SAAS,KAAK,SAAS,EAAE;YACzB,WAAW,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,KAAK,EAAE,IAAI,CAAC,KAAK,EACjB,OAAO,EAAE,IAAI,CAAC,OAAO,EACrB,eAAe,EAAE,IAAI,CAAC,gBAAgB,IACxC;IACN,CAAC;CACJ;AAED,8DAA8D;AAC9D,MAAM,uBAAwB,SAAQ,YAAY;IAO9C,YACI,UAGC;QAED,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAR9B,cAAS,GAAG,uBAAuB,CAAC,mBAAmB,CAAC;QAUpD,IAAI,UAAU,CAAC,KAAK,KAAK,SAAS,EAAE;YAChC,cAAc,CAAC,IAAI,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;SAC1C;IACL,CAAC;;AAjBD,2EAA2E;AAC3E,2EAA2E;AAC3D,2CAAmB,GAAG,cAAc,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n ILoggingError,\n ITaggedTelemetryPropertyType,\n ITelemetryLogger,\n ITelemetryProperties,\n} from \"@fluidframework/common-definitions\";\nimport { v4 as uuid } from \"uuid\";\nimport {\n hasErrorInstanceId,\n IFluidErrorBase,\n isFluidError,\n isValidLegacyError,\n} from \"./fluidErrorBase\";\n\n/** @returns true if value is an object but neither null nor an array */\nconst isRegularObject = (value: any): boolean => {\n return value !== null && !Array.isArray(value) && typeof value === \"object\";\n};\n\n/** Inspect the given error for common \"safe\" props and return them */\nexport function extractLogSafeErrorProperties(error: any, sanitizeStack: boolean) {\n const removeMessageFromStack = (stack: string, errorName?: string) => {\n if (!sanitizeStack) {\n return stack;\n }\n const stackFrames = stack.split(\"\\n\");\n stackFrames.shift(); // Remove \"[ErrorName]: [ErrorMessage]\"\n if (errorName !== undefined) {\n stackFrames.unshift(errorName); // Add \"[ErrorName]\"\n }\n return stackFrames.join(\"\\n\");\n };\n\n const message = (typeof error?.message === \"string\")\n ? error.message as string\n : String(error);\n\n const safeProps: { message: string; errorType?: string; stack?: string; } = {\n message,\n };\n\n if (isRegularObject(error)) {\n const { errorType, stack, name } = error;\n\n if (typeof errorType === \"string\") {\n safeProps.errorType = errorType;\n }\n\n if (typeof stack === \"string\") {\n const errorName = (typeof name === \"string\") ? name : undefined;\n safeProps.stack = removeMessageFromStack(stack, errorName);\n }\n }\n\n return safeProps;\n}\n\n/** type guard for ILoggingError interface */\nexport const isILoggingError = (x: any): x is ILoggingError => typeof x?.getTelemetryProperties === \"function\";\n\n/** Copy props from source onto target, but do not overwrite an existing prop that matches */\nfunction copyProps(target: ITelemetryProperties | LoggingError, source: ITelemetryProperties) {\n for (const key of Object.keys(source)) {\n if (target[key] === undefined) {\n target[key] = source[key];\n }\n }\n}\n\n/** Metadata to annotate an error object when annotating or normalizing it */\nexport interface IFluidErrorAnnotations {\n /** Telemetry props to log with the error */\n props?: ITelemetryProperties;\n}\n\n/** For backwards compatibility with pre-errorInstanceId valid errors */\nfunction patchLegacyError(\n legacyError: Omit<IFluidErrorBase, \"errorInstanceId\">,\n): asserts legacyError is IFluidErrorBase {\n const patchMe: { -readonly [P in \"errorInstanceId\"]?: IFluidErrorBase[P] } = legacyError as any;\n if (patchMe.errorInstanceId === undefined) {\n patchMe.errorInstanceId = uuid();\n }\n}\n\n/**\n * Normalize the given error yielding a valid Fluid Error\n * @returns A valid Fluid Error with any provided annotations applied\n * @param error - The error to normalize\n * @param annotations - Annotations to apply to the normalized error\n */\nexport function normalizeError(\n error: unknown,\n annotations: IFluidErrorAnnotations = {},\n): IFluidErrorBase {\n // Back-compat, while IFluidErrorBase is rolled out\n if (isValidLegacyError(error)) {\n patchLegacyError(error);\n }\n\n if (isFluidError(error)) {\n // We can simply add the telemetry props to the error and return it\n error.addTelemetryProperties(annotations.props ?? {});\n return error;\n }\n\n // We have to construct a new Fluid Error, copying safe properties over\n const { message, stack } = extractLogSafeErrorProperties(error, false /* sanitizeStack */);\n const fluidError: IFluidErrorBase = new NormalizedExternalError({\n message,\n stack,\n });\n\n // We need to preserve these properties which are used in a non-typesafe way throughout driver code (see #8743)\n // Anywhere they are set should be on a valid Fluid Error that would have been returned above,\n // but we can't prove it with the types, so adding this defensive measure.\n if (typeof error === \"object\" && error !== null) {\n const { canRetry, retryAfterSeconds } = error as any;\n Object.assign(normalizeError, { canRetry, retryAfterSeconds });\n }\n\n if (typeof (error) !== \"object\") {\n // This is only interesting for non-objects\n fluidError.addTelemetryProperties({ typeofError: typeof (error) });\n }\n\n const originalErrorTelemetryProps = isILoggingError(error) ? error.getTelemetryProperties() : {};\n fluidError.addTelemetryProperties({\n ...originalErrorTelemetryProps,\n ...annotations.props,\n untrustedOrigin: 1, // This will let us filter to errors not originated by our own code\n });\n\n return fluidError;\n}\n\nlet stackPopulatedOnCreation: boolean | undefined;\n\n/**\n * The purpose of this function is to provide ability to capture stack context quickly.\n * Accessing new Error().stack is slow, and the slowest part is accessing stack property itself.\n * There are scenarios where we generate error with stack, but error is handled in most cases and\n * stack property is not accessed.\n * For such cases it's better to not read stack property right away, but rather delay it until / if it's needed\n * Some browsers will populate stack right away, others require throwing Error, so we do auto-detection on the fly.\n * @returns Error object that has stack populated.\n */\n export function generateErrorWithStack(): Error {\n const err = new Error(\"<<generated stack>>\");\n\n if (stackPopulatedOnCreation === undefined) {\n stackPopulatedOnCreation = (err.stack !== undefined);\n }\n\n if (stackPopulatedOnCreation) {\n return err;\n }\n\n try {\n throw err;\n } catch (e) {\n return e as Error;\n }\n}\n\nexport function generateStack(): string | undefined {\n return generateErrorWithStack().stack;\n}\n\n/**\n * Create a new error using newErrorFn, wrapping and caused by the given unknown error.\n * Copies the inner error's stack, errorInstanceId and telemetry props over to the new error if present\n * @param innerError - An error from untrusted/unknown origins\n * @param newErrorFn - callback that will create a new error given the original error's message\n * @returns A new error object \"wrapping\" the given error\n */\nexport function wrapError<T extends LoggingError>(\n innerError: unknown,\n newErrorFn: (message: string) => T,\n): T {\n const {\n message,\n stack,\n } = extractLogSafeErrorProperties(innerError, false /* sanitizeStack */);\n\n const newError = newErrorFn(message);\n\n if (stack !== undefined) {\n overwriteStack(newError, stack);\n }\n\n // Mark external errors with untrustedOrigin flag\n if (isExternalError(innerError)) {\n newError.addTelemetryProperties({ untrustedOrigin: 1 });\n }\n\n // Reuse errorInstanceId\n if (hasErrorInstanceId(innerError)) {\n newError.overwriteErrorInstanceId(innerError.errorInstanceId);\n\n // For \"back-compat\" in the logs\n newError.addTelemetryProperties({ innerErrorInstanceId: innerError.errorInstanceId });\n }\n\n // Lastly, copy over all other telemetry properties. Note these will not overwrite existing properties\n // This will include the untrustedOrigin property if the inner error itself was created from an external error\n if (isILoggingError(innerError)) {\n newError.addTelemetryProperties(innerError.getTelemetryProperties());\n }\n\n return newError;\n}\n\n/** The same as wrapError, but also logs the innerError, including the wrapping error's instance id */\nexport function wrapErrorAndLog<T extends LoggingError>(\n innerError: unknown,\n newErrorFn: (message: string) => T,\n logger: ITelemetryLogger,\n) {\n const newError = wrapError(innerError, newErrorFn);\n\n // This will match innerError.errorInstanceId if present (see wrapError)\n const errorInstanceId = newError.errorInstanceId;\n\n // For \"back-compat\" in the logs\n const wrappedByErrorInstanceId = errorInstanceId;\n\n logger.sendTelemetryEvent({\n eventName: \"WrapError\",\n errorInstanceId,\n wrappedByErrorInstanceId,\n }, innerError);\n\n return newError;\n}\n\nfunction overwriteStack(error: IFluidErrorBase | LoggingError, stack: string) {\n // supposedly setting stack on an Error can throw.\n try {\n Object.assign(error, { stack });\n } catch (errorSettingStack) {\n error.addTelemetryProperties({ stack2: stack });\n }\n}\n\n/**\n * True for any error object that is an (optionally normalized) external error\n * False for any error we created and raised within the FF codebase, or wrapped in a well-known error type\n */\nexport function isExternalError(e: any): boolean {\n return !isValidLegacyError(e) ||\n (e.getTelemetryProperties().untrustedOrigin === 1 &&\n e.errorType === NormalizedExternalError.normalizedErrorType);\n}\n\n/**\n * Type guard to identify if a particular value (loosely) appears to be a tagged telemetry property\n */\nexport function isTaggedTelemetryPropertyValue(x: any): x is ITaggedTelemetryPropertyType {\n return (typeof (x?.value) !== \"object\" && typeof (x?.tag) === \"string\");\n}\n\n/**\n * Walk an object's enumerable properties to find those fit for telemetry.\n */\nfunction getValidTelemetryProps(obj: any, keysToOmit: Set<string>): ITelemetryProperties {\n const props: ITelemetryProperties = {};\n for (const key of Object.keys(obj)) {\n if (keysToOmit.has(key)) {\n continue;\n }\n const val = obj[key];\n switch (typeof val) {\n case \"string\":\n case \"number\":\n case \"boolean\":\n case \"undefined\":\n props[key] = val;\n break;\n default: {\n if (isTaggedTelemetryPropertyValue(val)) {\n props[key] = val;\n } else {\n // We don't support logging arbitrary objects\n props[key] = \"REDACTED (arbitrary object)\";\n }\n break;\n }\n }\n }\n return props;\n}\n\n/**\n * Borrowed from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#examples\n * Avoids runtime errors with circular references.\n * Not ideal, as will cut values that are not necessarily circular references.\n * Could be improved by implementing Node's util.inspect() for browser (minus all the coloring code)\n*/\nexport const getCircularReplacer = () => {\n const seen = new WeakSet();\n return (key: string, value: any): any => {\n if (typeof value === \"object\" && value !== null) {\n if (seen.has(value)) {\n return \"<removed/circular>\";\n }\n seen.add(value);\n }\n return value;\n };\n};\n\n/**\n * Base class for \"trusted\" errors we create, whose properties can generally be logged to telemetry safely.\n * All properties set on the object, or passed in (via the constructor or addTelemetryProperties),\n * will be logged in accordance with their tag, if present.\n *\n * PLEASE take care to avoid setting sensitive data on this object without proper tagging!\n */\nexport class LoggingError extends Error implements ILoggingError, Omit<IFluidErrorBase, \"errorType\"> {\n private _errorInstanceId = uuid();\n get errorInstanceId() { return this._errorInstanceId; }\n overwriteErrorInstanceId(id: string) { this._errorInstanceId = id; }\n\n /** Back-compat to appease isFluidError typeguard in old code that may handle this error */\n // @ts-expect-error - This field shouldn't be referenced in the current version, but needs to exist at runtime.\n private readonly fluidErrorCode: \"-\" = \"-\";\n\n /**\n * Create a new LoggingError\n * @param message - Error message to use for Error base class\n * @param props - telemetry props to include on the error for when it's logged\n * @param omitPropsFromLogging - properties by name to omit from telemetry props\n */\n constructor(\n message: string,\n props?: ITelemetryProperties,\n private readonly omitPropsFromLogging: Set<string> = new Set(),\n ) {\n super(message);\n\n // Don't log this list itself, or the private _errorInstanceId\n omitPropsFromLogging.add(\"omitPropsFromLogging\");\n omitPropsFromLogging.add(\"_errorInstanceId\");\n\n if (props) {\n this.addTelemetryProperties(props);\n }\n }\n\n /**\n * Add additional properties to be logged\n */\n public addTelemetryProperties(props: ITelemetryProperties) {\n copyProps(this, props);\n }\n\n /**\n * Get all properties fit to be logged to telemetry for this error\n */\n public getTelemetryProperties(): ITelemetryProperties {\n const taggableProps = getValidTelemetryProps(this, this.omitPropsFromLogging);\n // Include non-enumerable props that are not returned by getValidTelemetryProps\n return {\n ...taggableProps,\n stack: this.stack,\n message: this.message,\n errorInstanceId: this._errorInstanceId,\n };\n }\n}\n\n/** The Error class used when normalizing an external error */\nclass NormalizedExternalError extends LoggingError {\n // errorType \"genericError\" is used as a default value throughout the code.\n // Note that this matches ContainerErrorType/DriverErrorType's genericError\n static readonly normalizedErrorType = \"genericError\";\n\n errorType = NormalizedExternalError.normalizedErrorType;\n\n constructor(\n errorProps: Pick<IFluidErrorBase,\n | \"message\"\n | \"stack\"\n >,\n ) {\n super(errorProps.message);\n\n if (errorProps.stack !== undefined) {\n overwriteStack(this, errorProps.stack);\n }\n }\n}\n"]}
@@ -5,5 +5,5 @@
5
5
  * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
6
6
  */
7
7
  export declare const pkgName = "@fluidframework/telemetry-utils";
8
- export declare const pkgVersion = "1.0.1";
8
+ export declare const pkgVersion = "1.1.0";
9
9
  //# sourceMappingURL=packageVersion.d.ts.map
@@ -5,5 +5,5 @@
5
5
  * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
6
6
  */
7
7
  export const pkgName = "@fluidframework/telemetry-utils";
8
- export const pkgVersion = "1.0.1";
8
+ export const pkgVersion = "1.1.0";
9
9
  //# sourceMappingURL=packageVersion.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,iCAAiC,CAAC;AACzD,MAAM,CAAC,MAAM,UAAU,GAAG,OAAO,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/telemetry-utils\";\nexport const pkgVersion = \"1.0.1\";\n"]}
1
+ {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,iCAAiC,CAAC;AACzD,MAAM,CAAC,MAAM,UAAU,GAAG,OAAO,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/telemetry-utils\";\nexport const pkgVersion = \"1.1.0\";\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluidframework/telemetry-utils",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Collection of telemetry relates utilities for Fluid",
5
5
  "homepage": "https://fluidframework.com",
6
6
  "repository": {
@@ -71,11 +71,11 @@
71
71
  "uuid": "^8.3.1"
72
72
  },
73
73
  "devDependencies": {
74
- "@fluidframework/build-common": "^0.23.0",
75
- "@fluidframework/build-tools": "^0.2.71273",
74
+ "@fluidframework/build-common": "^0.24.0",
75
+ "@fluidframework/build-tools": "^0.2.74327",
76
76
  "@fluidframework/eslint-config-fluid": "^0.28.2000",
77
- "@fluidframework/mocha-test-setup": "^1.0.1",
78
- "@fluidframework/telemetry-utils-previous": "npm:@fluidframework/telemetry-utils@^0.59.0",
77
+ "@fluidframework/mocha-test-setup": "^1.1.0",
78
+ "@fluidframework/telemetry-utils-previous": "npm:@fluidframework/telemetry-utils@^1.0.0",
79
79
  "@microsoft/api-extractor": "^7.22.2",
80
80
  "@rushstack/eslint-config": "^2.5.1",
81
81
  "@types/debug": "^4.1.5",
@@ -93,9 +93,10 @@
93
93
  "typescript": "~4.5.5"
94
94
  },
95
95
  "typeValidation": {
96
- "version": "1.0.0",
96
+ "version": "1.1.0",
97
97
  "broken": {
98
- "EnumDeclaration_TelemetryDataTag": {
98
+ "RemovedFunctionDeclaration_originatedAsExternalError": {
99
+ "forwardCompat": false,
99
100
  "backCompat": false
100
101
  }
101
102
  }
@@ -88,10 +88,6 @@ function patchLegacyError(
88
88
  }
89
89
  }
90
90
 
91
- // errorType "genericError" is used as a default value throughout the code.
92
- // Note that this matches ContainerErrorType/DriverErrorType's genericError
93
- const defaultErrorTypeForNormalize = "genericError";
94
-
95
91
  /**
96
92
  * Normalize the given error yielding a valid Fluid Error
97
93
  * @returns A valid Fluid Error with any provided annotations applied
@@ -115,17 +111,11 @@ export function normalizeError(
115
111
 
116
112
  // We have to construct a new Fluid Error, copying safe properties over
117
113
  const { message, stack } = extractLogSafeErrorProperties(error, false /* sanitizeStack */);
118
- const fluidError: IFluidErrorBase = new SimpleFluidError({
119
- errorType: defaultErrorTypeForNormalize,
114
+ const fluidError: IFluidErrorBase = new NormalizedExternalError({
120
115
  message,
121
116
  stack,
122
117
  });
123
118
 
124
- fluidError.addTelemetryProperties({
125
- ...annotations.props,
126
- untrustedOrigin: 1, // This will let us filter to errors not originated by our own code
127
- });
128
-
129
119
  // We need to preserve these properties which are used in a non-typesafe way throughout driver code (see #8743)
130
120
  // Anywhere they are set should be on a valid Fluid Error that would have been returned above,
131
121
  // but we can't prove it with the types, so adding this defensive measure.
@@ -138,6 +128,14 @@ export function normalizeError(
138
128
  // This is only interesting for non-objects
139
129
  fluidError.addTelemetryProperties({ typeofError: typeof (error) });
140
130
  }
131
+
132
+ const originalErrorTelemetryProps = isILoggingError(error) ? error.getTelemetryProperties() : {};
133
+ fluidError.addTelemetryProperties({
134
+ ...originalErrorTelemetryProps,
135
+ ...annotations.props,
136
+ untrustedOrigin: 1, // This will let us filter to errors not originated by our own code
137
+ });
138
+
141
139
  return fluidError;
142
140
  }
143
141
 
@@ -175,9 +173,8 @@ export function generateStack(): string | undefined {
175
173
  }
176
174
 
177
175
  /**
178
- * Create a new error, wrapping and caused by the given unknown error.
179
- * Copies the inner error's message and stack over but otherwise uses newErrorFn to define the error.
180
- * The inner error's instance id will also be logged for telemetry analysis.
176
+ * Create a new error using newErrorFn, wrapping and caused by the given unknown error.
177
+ * Copies the inner error's stack, errorInstanceId and telemetry props over to the new error if present
181
178
  * @param innerError - An error from untrusted/unknown origins
182
179
  * @param newErrorFn - callback that will create a new error given the original error's message
183
180
  * @returns A new error object "wrapping" the given error
@@ -198,7 +195,7 @@ export function wrapError<T extends LoggingError>(
198
195
  }
199
196
 
200
197
  // Mark external errors with untrustedOrigin flag
201
- if (originatedAsExternalError(innerError)) {
198
+ if (isExternalError(innerError)) {
202
199
  newError.addTelemetryProperties({ untrustedOrigin: 1 });
203
200
  }
204
201
 
@@ -210,6 +207,12 @@ export function wrapError<T extends LoggingError>(
210
207
  newError.addTelemetryProperties({ innerErrorInstanceId: innerError.errorInstanceId });
211
208
  }
212
209
 
210
+ // Lastly, copy over all other telemetry properties. Note these will not overwrite existing properties
211
+ // This will include the untrustedOrigin property if the inner error itself was created from an external error
212
+ if (isILoggingError(innerError)) {
213
+ newError.addTelemetryProperties(innerError.getTelemetryProperties());
214
+ }
215
+
213
216
  return newError;
214
217
  }
215
218
 
@@ -245,14 +248,6 @@ function overwriteStack(error: IFluidErrorBase | LoggingError, stack: string) {
245
248
  }
246
249
  }
247
250
 
248
- /**
249
- * True for any error object that is either external itself or is a wrapped/normalized external error
250
- * False for any error we created and raised within the FF codebase.
251
- */
252
- export function originatedAsExternalError(e: any): boolean {
253
- return !isValidLegacyError(e) || (e.getTelemetryProperties().untrustedOrigin === 1);
254
- }
255
-
256
251
  /**
257
252
  * True for any error object that is an (optionally normalized) external error
258
253
  * False for any error we created and raised within the FF codebase, or wrapped in a well-known error type
@@ -260,7 +255,7 @@ export function originatedAsExternalError(e: any): boolean {
260
255
  export function isExternalError(e: any): boolean {
261
256
  return !isValidLegacyError(e) ||
262
257
  (e.getTelemetryProperties().untrustedOrigin === 1 &&
263
- e.errorType === defaultErrorTypeForNormalize);
258
+ e.errorType === NormalizedExternalError.normalizedErrorType);
264
259
  }
265
260
 
266
261
  /**
@@ -316,19 +311,18 @@ export const getCircularReplacer = () => {
316
311
  }
317
312
  seen.add(value);
318
313
  }
319
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
320
314
  return value;
321
315
  };
322
316
  };
323
317
 
324
318
  /**
325
319
  * Base class for "trusted" errors we create, whose properties can generally be logged to telemetry safely.
326
- * All properties set on the object, or passed in (via the constructor or getTelemetryProperties),
320
+ * All properties set on the object, or passed in (via the constructor or addTelemetryProperties),
327
321
  * will be logged in accordance with their tag, if present.
328
322
  *
329
323
  * PLEASE take care to avoid setting sensitive data on this object without proper tagging!
330
324
  */
331
- export class LoggingError extends Error implements ILoggingError, Pick<IFluidErrorBase, "errorInstanceId"> {
325
+ export class LoggingError extends Error implements ILoggingError, Omit<IFluidErrorBase, "errorType"> {
332
326
  private _errorInstanceId = uuid();
333
327
  get errorInstanceId() { return this._errorInstanceId; }
334
328
  overwriteErrorInstanceId(id: string) { this._errorInstanceId = id; }
@@ -381,19 +375,22 @@ export class LoggingError extends Error implements ILoggingError, Pick<IFluidErr
381
375
  }
382
376
  }
383
377
 
384
- /** Simple implementation of IFluidErrorBase, extending LoggingError */
385
- class SimpleFluidError extends LoggingError implements IFluidErrorBase {
386
- readonly errorType: string;
378
+ /** The Error class used when normalizing an external error */
379
+ class NormalizedExternalError extends LoggingError {
380
+ // errorType "genericError" is used as a default value throughout the code.
381
+ // Note that this matches ContainerErrorType/DriverErrorType's genericError
382
+ static readonly normalizedErrorType = "genericError";
383
+
384
+ errorType = NormalizedExternalError.normalizedErrorType;
387
385
 
388
386
  constructor(
389
387
  errorProps: Pick<IFluidErrorBase,
390
388
  | "message"
391
389
  | "stack"
392
- | "errorType"
393
390
  >,
394
391
  ) {
395
392
  super(errorProps.message);
396
- this.errorType = errorProps.errorType;
393
+
397
394
  if (errorProps.stack !== undefined) {
398
395
  overwriteStack(this, errorProps.stack);
399
396
  }
@@ -6,4 +6,4 @@
6
6
  */
7
7
 
8
8
  export const pkgName = "@fluidframework/telemetry-utils";
9
- export const pkgVersion = "1.0.1";
9
+ export const pkgVersion = "1.1.0";