-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Resolve "_a.substring is not a function" error with applicationInsights Issue was found in applicationinsights Library's TelemetryClient.trackTrace and EnvelopeFactory.createTraceData methods. This fix targets the compatibility issue observed in version 2.5.1 or higher. Co-authored-by: Florian Rudisch <[email protected]>
- Loading branch information
Showing
7 changed files
with
170 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { ApplicationError } from '../error'; | ||
import { stringify } from './stringify'; | ||
|
||
describe('stringify', () => { | ||
describe('when provided with a single argument', () => { | ||
it('returns the input string as is', () => { | ||
const input = 'test string'; | ||
expect(stringify(input)).toBe(input); | ||
}); | ||
|
||
it('stringifies an Error object with message and stack', () => { | ||
const error = new ApplicationError('conflict', 409); | ||
const result = stringify(error); | ||
expect(result).toContain(error.message); | ||
}); | ||
|
||
it('stringifies a plain object correctly', () => { | ||
const input = { key: 'value' }; | ||
expect(stringify(input)).toBe(JSON.stringify(input)); | ||
}); | ||
|
||
it('returns object.toString() for an empty object', () => { | ||
const input = {}; | ||
expect(stringify(input)).toBe('[object Object]'); | ||
}); | ||
|
||
it('returns error message for null', () => { | ||
expect(stringify(null)).toBe('The provided error was eq to null - unable to log a specific error-message'); | ||
}); | ||
|
||
it('returns the number as a string for numbers', () => { | ||
expect(stringify(123)).toBe('123'); | ||
}); | ||
|
||
it('returns the boolean value as a string for booleans', () => { | ||
expect(stringify(true)).toBe('true'); | ||
expect(stringify(false)).toBe('false'); | ||
}); | ||
|
||
it('handles symbols correctly', () => { | ||
expect(stringify(Symbol())).toContain('not suitable for logging: symbol'); | ||
}); | ||
|
||
it('handles functions correctly', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
expect(stringify(function () {})).toContain('not suitable for logging: function'); | ||
}); | ||
|
||
it('handles cyclic objects by using toString', () => { | ||
const obj: any = {}; | ||
obj.cycle = obj; | ||
expect(stringify(obj)).toBe(obj.toString()); | ||
}); | ||
}); | ||
|
||
describe('when provided with multiple arguments', () => { | ||
it('joins stringified arguments with spaces', () => { | ||
const arg1 = 'Hello'; | ||
const arg2 = { message: 'World' }; | ||
expect(stringify(arg1, arg2)).toBe('Hello {"message":"World"}'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
type ErrorWithMessage = { | ||
message: string; | ||
stack?: string; | ||
}; | ||
|
||
const isErrorWithMessage = (error: unknown): error is ErrorWithMessage => { | ||
return ( | ||
typeof error === 'object' && | ||
error !== null && | ||
'message' in error && | ||
typeof (error as Record<string, unknown>).message === 'string' | ||
); | ||
}; | ||
|
||
const stringifyObject = (error: object | null): string => { | ||
if (error === null) { | ||
return 'The provided error was eq to null - unable to log a specific error-message'; | ||
} | ||
|
||
if (error === undefined) { | ||
return 'The provided error was eq to undefined - unable to log a specific error-message'; | ||
} | ||
|
||
if (isErrorWithMessage(error)) { | ||
return JSON.stringify({ message: error.message, stack: error.stack }); | ||
} else { | ||
try { | ||
const errorAsJson = JSON.stringify(error); | ||
if (errorAsJson === '{}') { | ||
return error.toString(); | ||
} else { | ||
return errorAsJson; | ||
} | ||
} catch (_) { | ||
//Fallback in case there's an error stringify | ||
return error.toString(); | ||
} | ||
} | ||
}; | ||
|
||
const unknownToString = (input: unknown) => { | ||
if (typeof input === 'object' && input !== null && 'toString' in input) return input.toString(); | ||
return String(input); | ||
}; | ||
|
||
const stringifySingleArgument = (arg: unknown): string => { | ||
switch (typeof arg) { | ||
case 'string': | ||
return arg; | ||
case 'object': | ||
return stringifyObject(arg); | ||
case 'number': | ||
return arg.toString(); | ||
case 'boolean': | ||
return String(arg); | ||
default: | ||
return `The error object with value ${unknownToString( | ||
arg, | ||
)} has a type, that is not suitable for logging: ${typeof arg}`; | ||
} | ||
}; | ||
|
||
export const stringify = (...args: unknown[]): string => { | ||
switch (typeof args) { | ||
case 'string': | ||
return args; | ||
case 'object': | ||
if (Array.isArray(args)) { | ||
return args.map((element) => stringifySingleArgument(element)).join(' '); | ||
} | ||
|
||
return stringifyObject(args[0]); | ||
default: | ||
return `The error object has a type, that is not suitable for logging: ${typeof args}`; | ||
} | ||
}; |