Skip to content

Commit 03ae0a2

Browse files
committed
chore(ember): Show warning when using invalid config
When defining Sentry config in `ENV['@sentry/ember'].sentry`, this goes through @embroider/macros under the hood. This relies on config being serializable, which means that e.g. you cannot use regex in the config. To make this easier to pin down (as the error message can be a bit cryptic), this now validates the passed config (based on embroider-build/embroider#1083) and shows a helpful warning message. Note that this also moved this around a bit, leading to config being written only once on build, which should be a bit faster.
1 parent 31bf467 commit 03ae0a2

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

packages/ember/index.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ module.exports = {
2727
},
2828
},
2929

30-
config(_, appConfig) {
31-
const addonConfig = appConfig['@sentry/ember'];
32-
this.options['@embroider/macros'].setOwnConfig.sentryConfig = { ...addonConfig };
33-
return this._super(...arguments);
30+
included() {
31+
const app = this._findHost();
32+
const config = app.project.config(app.env);
33+
const addonConfig = config['@sentry/ember'] || {};
34+
35+
if (!isSerializable(addonConfig)) {
36+
console.warn(
37+
"Warning: You passed a non-serializable config to `ENV['@sentry/ember'].sentry`. Non-serializable config (e.g. RegExp, ...) can only be passed directly to `Sentry.init()`.",
38+
);
39+
}
40+
41+
this.options['@embroider/macros'].setOwnConfig.sentryConfig = addonConfig;
42+
43+
this._super.included.apply(this, arguments);
3444
},
3545

3646
contentFor(type, config) {
@@ -50,3 +60,40 @@ module.exports = {
5060

5161
injectedScriptHashes: [initialLoadHeadSnippetHash, initialLoadBodySnippetHash],
5262
};
63+
64+
function isSerializable(obj) {
65+
if (isScalar(obj)) {
66+
return true;
67+
}
68+
69+
if (Array.isArray(obj)) {
70+
return !obj.some(arrayItem => !isSerializable(arrayItem));
71+
}
72+
73+
if (isPlainObject(obj)) {
74+
for (let property in obj) {
75+
let value = obj[property];
76+
if (!isSerializable(value)) {
77+
return false;
78+
}
79+
}
80+
81+
return true;
82+
}
83+
84+
return false;
85+
}
86+
87+
function isScalar(val) {
88+
return (
89+
typeof val === 'undefined' ||
90+
typeof val === 'string' ||
91+
typeof val === 'boolean' ||
92+
typeof val === 'number' ||
93+
val === null
94+
);
95+
}
96+
97+
function isPlainObject(obj) {
98+
return typeof obj === 'object' && obj.constructor === Object && obj.toString() === '[object Object]';
99+
}

0 commit comments

Comments
 (0)