Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ember): Show warning when using invalid config #6032

Merged
merged 1 commit into from Oct 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 53 additions & 4 deletions packages/ember/index.js
Expand Up @@ -27,10 +27,22 @@ 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()\`, which is usually defined in app/app.js.
The reason for this is that @embroider/macros, which is used under the hood to handle environment config, requires serializable configuration.`,
);
}

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

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

contentFor(type, config) {
Expand All @@ -50,3 +62,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]';
}