raygun4js
raygun4js copied to clipboard
getRaygunInstance always returns undefined
I cannot get the active instance of Raygun using getRaygunInstance, it always returns undefined.
Error reporting works just fine but we need to create a new instance since our web component is being used within another application that uses Raygun as well.
In some situations we run the component in a standalone app for testing so we (apparently?) need to use the v2 syntax to initialize our rg4js instance. And in other situations we need to just get the active instance and create a new one (and then use the V1 syntax).
You should just always be able to get the current instance using rg4js('getRaygunInstance')
right?
I already tried delaying the call with a timeout / in the window load event, but it just always returns undefined.
try {
rg4js('apiKey', environment.rayGunKey);
rg4js('setVersion', RAYGUN_VERSION_NUMBER);
rg4js('enableCrashReporting', true);
rg4js('options', raygunOptions);
const activeInstance = rg4js('getRaygunInstance'); // <== activeInstance is always undefined
} catch (e) { console.error('Raygun init error', e); }
Thanks for filling the issue @MartijnKooij.
I agree and would think you should be able to get the instance. The only case where this might not be the case is if the library was loading. But as you said, the load
handler should have catered to this. I'll have a look into this today and get back to you with some further information.
@MartijnKooij I return with further information and replicated what I believe you were experiencing.
First off, the rg4js('getRaygunInstance')
call will return undefined until the provider has internally executed its own window.onload
listener. This means a script executed before raygun4js which attaches a load
event will still get a undefined result when calling rg4js('getRaygunInstance')
. Due to the order that the events are attached. See the code example below:
const activeInstanceOne = rg4js('getRaygunInstance'); // returns undefined
window.addEventListener('load', () => {
const activeInstanceTwo = rg4js('getRaygunInstance'); // returns undefined
setTimeout(function() {
const activeInstanceThree = rg4js('getRaygunInstance'); // returns the instance
}, 1);
});
Now the above is undesirable and we will need to look into how we can fix that in the future. However, I do have a work around which uses the V1 api to construct a new Raygun instance on load.
window.addEventListener('load', () => {
var secondInstance = Raygun.constructNewRaygun();
secondRaygun.init('API-KEY', options);
});
The V2 API is generally recommended but for your use-case you need to use the V1 anyway to provide your new instance with options
alongside the API-KEY
.
Hope that helps you out. I'll leave this issue open so that we can address it in the future.
Thanks for looking into this @BenjaminHarding! We will for now use your suggested workaround of creating the 2nd instance using the V1 syntax because indeed we already had to use that for that new instance anyway.
We now support passing a callback to getRaygunInstance
that will be called once the instance is available.
rg4js('getRaygunInstance', (raygun) => {
// raygun is always defined
});
The instance will still be returned if it's available, but rg4js('getRaygunInstance')
will still return undefined if called before the instance is initialised, so it's preferable to use the callback if possible.
In addition, we recently fixed a bug with the UMD builds that caused all rg4js
calls to return undefined, so that should also help anyone using require
or import
based bundling.
It looks like the above has resolved this query. It makes sense to only find the instance after it is initialized to ensure that the initialization is successful.
I'll go ahead and close this thread. If there are any issues, please reach out to the Raygun team and we will gladly assist!