Skip to content

Commit fa0f8d3

Browse files
committed
simplify and rename getWalkSource
1 parent 1c84fce commit fa0f8d3

File tree

3 files changed

+44
-64
lines changed

3 files changed

+44
-64
lines changed

packages/utils/src/normalize.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isPrimitive, isSyntheticEvent } from './is';
22
import { memoBuilder, MemoFunc } from './memo';
3-
import { getWalkSource } from './object';
3+
import { convertToPlainObject } from './object';
44
import { getFunctionName } from './stacktrace';
55

66
type UnknownMaybeWithToJson = unknown & { toJSON?: () => string };
@@ -88,7 +88,7 @@ export function walk(
8888

8989
// Create source that we will use for the next iteration. It will either be an objectified error object (`Error` type
9090
// with extracted key:value pairs) or the input itself.
91-
const source = getWalkSource(value);
91+
const source = convertToPlainObject(value);
9292

9393
// Create an accumulator that will act as a parent for all future itterations of that branch
9494
const acc: { [key: string]: any } = Array.isArray(value) ? [] : {};
@@ -114,7 +114,7 @@ export function walk(
114114
propertyCount += 1;
115115

116116
// Recursively walk through all the child nodes
117-
const innerValue: UnknownMaybeWithToJson = source[innerKey];
117+
const innerValue = source[innerKey] as UnknownMaybeWithToJson;
118118
acc[innerKey] = walk(innerKey, innerValue, depth - 1, maxProperties, memo);
119119
}
120120

packages/utils/src/object.ts

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,21 @@ export function urlEncode(object: { [key: string]: any }): string {
9797
*
9898
* @param value Initial source that we have to transform in order for it to be usable by the serializer
9999
*/
100-
export function getWalkSource(value: any): {
101-
[key: string]: any;
100+
export function convertToPlainObject(value: unknown): {
101+
[key: string]: unknown;
102102
} {
103+
let newObj = value as {
104+
[key: string]: unknown;
105+
};
106+
103107
if (isError(value)) {
104-
const error = value as ExtendedError;
105-
const err: {
106-
[key: string]: any;
107-
stack: string | undefined;
108-
message: string;
109-
name: string;
110-
} = {
111-
message: error.message,
112-
name: error.name,
113-
stack: error.stack,
108+
newObj = {
109+
message: value.message,
110+
name: value.name,
111+
stack: value.stack,
112+
...getOwnProperties(value as ExtendedError),
114113
};
115-
116-
for (const i in error) {
117-
if (Object.prototype.hasOwnProperty.call(error, i)) {
118-
err[i] = error[i];
119-
}
120-
}
121-
122-
return err;
123-
}
124-
125-
if (isEvent(value)) {
114+
} else if (isEvent(value)) {
126115
/**
127116
* Event-like interface that's usable in browser and node
128117
*/
@@ -133,49 +122,40 @@ export function getWalkSource(value: any): {
133122
currentTarget?: unknown;
134123
}
135124

136-
const event = value as unknown as SimpleEvent;
137-
138-
const source: {
139-
[key: string]: any;
140-
} = {};
141-
142-
// Accessing event attributes can throw (see https://github.com/getsentry/sentry-javascript/issues/768 and
143-
// https://github.com/getsentry/sentry-javascript/issues/838), but accessing `type` hasn't been wrapped in a
144-
// try-catch in at least two years and no one's complained, so that's likely not an issue anymore
145-
source.type = event.type;
146-
147-
try {
148-
source.target = isElement(event.target)
149-
? htmlTreeAsString(event.target)
150-
: Object.prototype.toString.call(event.target);
151-
} catch (_oO) {
152-
source.target = '<unknown>';
153-
}
125+
const event = value as SimpleEvent;
154126

155-
try {
156-
source.currentTarget = isElement(event.currentTarget)
157-
? htmlTreeAsString(event.currentTarget)
158-
: Object.prototype.toString.call(event.currentTarget);
159-
} catch (_oO) {
160-
source.currentTarget = '<unknown>';
161-
}
127+
newObj = {
128+
type: event.type,
129+
target: serializeEventTarget(event.target),
130+
currentTarget: serializeEventTarget(event.currentTarget),
131+
...getOwnProperties(event),
132+
};
162133

163134
if (typeof CustomEvent !== 'undefined' && isInstanceOf(value, CustomEvent)) {
164-
source.detail = event.detail;
165-
}
166-
167-
for (const attr in event) {
168-
if (Object.prototype.hasOwnProperty.call(event, attr)) {
169-
source[attr] = event[attr];
170-
}
135+
newObj.detail = event.detail;
171136
}
137+
}
138+
return newObj;
139+
}
172140

173-
return source;
141+
/** Creates a string representation of the target of an `Event` object */
142+
function serializeEventTarget(target: unknown): string {
143+
try {
144+
return isElement(target) ? htmlTreeAsString(target) : Object.prototype.toString.call(target);
145+
} catch (_oO) {
146+
return '<unknown>';
174147
}
148+
}
175149

176-
return value as {
177-
[key: string]: any;
178-
};
150+
/** Filters out all but an object's own properties */
151+
function getOwnProperties(obj: { [key: string]: unknown }): { [key: string]: unknown } {
152+
const extractedProps: { [key: string]: unknown } = {};
153+
for (const property in obj) {
154+
if (Object.prototype.hasOwnProperty.call(obj, property)) {
155+
extractedProps[property] = obj[property];
156+
}
157+
}
158+
return extractedProps;
179159
}
180160

181161
/**
@@ -185,7 +165,7 @@ export function getWalkSource(value: any): {
185165
*/
186166
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
187167
export function extractExceptionKeysForMessage(exception: any, maxLength: number = 40): string {
188-
const keys = Object.keys(getWalkSource(exception));
168+
const keys = Object.keys(convertToPlainObject(exception));
189169
keys.sort();
190170

191171
if (!keys.length) {

packages/utils/test/normalize.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('normalize()', () => {
2121
});
2222
});
2323

24-
describe('getWalkSource()', () => {
24+
describe('convertToPlainObject()', () => {
2525
test('extracts extra properties from error objects', () => {
2626
const obj = new Error('Wubba Lubba Dub Dub') as any;
2727
obj.reason = new TypeError("I'm pickle Riiick!");

0 commit comments

Comments
 (0)