-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
Cannot import Bugsnag from 'bugsnag'
in test environment
#78
Comments
Hi @flexyford, thanks for reporting. Can you show me an example test that reproduces the problem and a bit more information about where you're doing the import in your app. e.g. is it in an initializer? Any example files you can provide are helpful |
Hi @binhums thanks for the quick reply. I want to throw an exception, log it to bugsnag, and catch it, without terminating the program execution. Here's a dummy example of what I'm trying to accomplish: // my-app/components/some-component.js
import Ember from 'ember';
import Bugsnag from 'bugsnag';
export default Ember.Component.extend({
didInsertElement() {
this._super(...arguments);
// Bugsnag Telemetry
try {
throw new BugsnagException(
"Expect this to log a Bugsnag Exception without terminating program execution"
);
}
} catch(e) {
Bugsnag.notify(e.name, e.message);
}
}
}
function BugsnagException(message) {
this.name = 'Bugsnag Exception';
this.message = message;
} I would like to have I do not see a reason to exclude bugsnag in test env since |
and to clarify what type of test would this be in? I want to recreate an example where we achieve this, possibly in the dummy test folder for this addon. |
In my instance, it's an acceptance test which uses I've come up with a solution which I hope still models the intended behavior. // ember-cli-bugsnag/index.js
// ...
config: function(env, config) {
const { bugsnag } = config;
const { releaseStage = env } = bugsnag;
// Determine if we should include bugsnag:
// That way if `env = test` it can still include `bugsnag`
// if `bugsnag.releaseState !== 'test'`
this._includeBugsnag = this.isDevelopingAddon() || releaseStage !== 'test';
return {
bugsnag: readEnvironmentConfig(process.env)
};
}, Open to submitting a PR if we decide to go this route |
I'm confused why isn't the code in https://github.com/binhums/ember-cli-bugsnag/blob/master/index.js#L45 working in your situation. |
It would seem to me that for whatever reason we're explicitly not loading bugsnag in test environments? |
Because I'm in a
So But I want bugsnag to load in my |
I'm not so good with the pattern matching syntax bear with me. Just so we're clear: so your code snippet overrides this line at the config step based on the release stage specified in the bugsnag config rather than the ember environment? I'd definitely be open to supporting test environments of course, but I think a solution would be more likely focussed around mocking the Bugsnag library. You wouldn't actually want errors to be reported to bugsnag every time you want your test suite. My open questions are:
There are examples of what you're describing in the acceptance tests for this addon https://github.com/binhums/ember-cli-bugsnag/blob/master/tests/acceptance/custom-release-stage-test.js. I'm assuming that these work because of the |
to be clear I don't expect you to have an answer to this one :) |
The examples in acceptance tests work because this addon adds I think the real mystery is #1: Why is it explicitly disabled? |
🕵️ time to pull out the blame hammer Would you be averse to specifying bugsnag as a dependency in your package.json as a workaround? Not trying to halt this conversation, just trying to help you achieve your immediate goals. |
Looks like #52 (they made blame so much better in Github now!):
OK, it doesn't seem like an attempt to stop bugsnag reporting by accident at all costs because surely it isn't reported if it's not in notifyReleaseStages. Could it be performance? |
🤔 I'm sure there are some performance gains by not including it, but it means tests fail when Reporting Handled Bugsnag Exceptions. The correct solution here might be exposing all Bugnag methods from the |
Ok I finally figured out what my actual issue was: Reporting Handled Bugsnag Exceptions. Using the following pattern, I can handle exceptions, pass them to bugnsag using // app/utils/bugsnag-exception.js
export default function bugsnagNotifyException(e, name) {
const onerror = Ember.onerror || function() {};
e.name = name || e.name;
onerror(e);
}; // app/components/some-component.js
import bugsnagNotifyException from '../utils/bugsnag-exception';
try {
// Some code which might throw an exception
} catch (exception) {
bugsnagNotifyException(exception, 'customErrorName');
} I think |
Hey Alex. Really glad you were able to come to a solution in the end. It's definitely not what that interface was originally designed for but I'm glad it works. Are you happy for me to close the issue? I'm looking back to hearing from @cibernox on the specific motivation for the other PR.
Any chance you could give a concrete example? I often find myself being tempted to implement features that wrap around features that bugnsnag-js implements directly and I'm relatively convinced that the right thing there is to let the user work that out for themselves. E.g. see #53. |
Woops, closed this issue by merging my branch, did not know github did that when referencing different repos? Yes I'm fine closing this issue, but will let @binhums decide
IMO a bugsnag wrapper should be exposed so that Regardless, I think the README could benefit from an explanation of "reporting handled exceptions" Reporting Handled ExceptionsTo handle exceptions but still log them in try {
// Some code which might throw an exception
} catch (exception) {
exception.name = 'customErrorName'; // Default Name: 'Error'
if (Ember.onerror) { Ember.onerror(e) };
} |
@flexyford sorry for letting things hang so long, it didn't feel so actionable at the time.
Agreed
Agreed, I know Bugsnag is quite limited on what you can do here. One thing that I'm not sure on is the distinction between marking a project as a JS project vs an Ember.js project. Perhaps they do a better job of grouping errors that are specific to the Ember.js ecosystem.
It probably makes more sense to rethrow a wrapped exception. I wouldn't want to make any assumptions about the consuming app's setup. Otherwise looks great. Can you add a PR for the docs? Sorry I've let this sit open a while. @flexyford correct me if I'm wrong but the two actions are:
|
I agree with @flexyford's (at least initial) assessment that we should NOT prevent the bugsnag library from being included, but should rather than the I talked with @cibernox about this; it would be bad if we suddenly started importing Bugsnag if people aren't prepared for it, since it might mean they'll start getting real bugsnags from test environments if they haven't spent time tweaking their |
@machty I think that a config option that overrides the default behavior and allows people to opt in to inporting bugsnag in test environment makes sense, it would be backwards-compatible and easy to implement. |
Was able to resolve my issue thanks to this thread - cheers! +1 to For context on my use case, I have a bunch of components that .catch() promise exceptions in order to display the error to the user via toast notification. When this happens, I still want to notify bugsnag of the error. |
I'm using the
Bugsnag.notify
API and therfore have toHowever, this breaks my tests:
Any suggestions here? I'm curious why
index.js
explicitly does not include bugsnag intest
environments.Any chance we could import just the
bugsnag/shim
intest
environments?The text was updated successfully, but these errors were encountered: