Skip to content

Commit

Permalink
chore(ember): Show warning when using invalid config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mydea committed Oct 24, 2022
1 parent 31bf467 commit 03ae0a2
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions packages/ember/index.js
Expand Up @@ -27,10 +27,20 @@ module.exports = {
},
},

config(_, appConfig) {
const addonConfig = appConfig['@sentry/ember'];
this.options['@embroider/macros'].setOwnConfig.sentryConfig = { ...addonConfig };
return this._super(...arguments);
included() {
const app = this._findHost();
const config = app.project.config(app.env);
const addonConfig = config['@sentry/ember'] || {};

if (!isSerializable(addonConfig)) {
console.warn(
"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()`.",
);
}

this.options['@embroider/macros'].setOwnConfig.sentryConfig = addonConfig;

this._super.included.apply(this, arguments);
},

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

injectedScriptHashes: [initialLoadHeadSnippetHash, initialLoadBodySnippetHash],
};

function isSerializable(obj) {
if (isScalar(obj)) {
return true;
}

if (Array.isArray(obj)) {
return !obj.some(arrayItem => !isSerializable(arrayItem));
}

if (isPlainObject(obj)) {
for (let property in obj) {
let value = obj[property];
if (!isSerializable(value)) {
return false;
}
}

return true;
}

return false;
}

function isScalar(val) {
return (
typeof val === 'undefined' ||
typeof val === 'string' ||
typeof val === 'boolean' ||
typeof val === 'number' ||
val === null
);
}

function isPlainObject(obj) {
return typeof obj === 'object' && obj.constructor === Object && obj.toString() === '[object Object]';
}

0 comments on commit 03ae0a2

Please sign in to comment.