Skip to content

Commit

Permalink
fix(lazy): crash on iterating "delayedCalls" (#219)
Browse files Browse the repository at this point in the history
* fix: crash on iterating "delayedCalls"

It looks like the code allowed "loadSentry()" to run more than once and
the first run would unset "delayedCalls" causing the second run to crash on
trying to iterate it.

* Null-check vm as it might not exist
  • Loading branch information
rchl committed Jul 30, 2020
1 parent 4fb726d commit a3de1cf
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/plugin.lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ let SentryMock = {}
<% } %>
let sentryReadyResolve
let loadInitiated = false
let loadCompleted = false

<% if (options.lazy.injectMock) { %>
let loadCompleted = false
const vueErrorHandler = VueLib.config.errorHandler

VueLib.config.errorHandler = (error, vm, info) => {
if (!loadCompleted) {
vm.$sentry.captureException(error)
if (vm) {
vm.$sentry.captureException(error)
}

if (VueLib.util) {
VueLib.util.warn(`Error in ${info}: "${error.toString()}"`, vm)
Expand Down Expand Up @@ -42,7 +44,7 @@ export default function SentryPlugin (ctx, inject) {
ctx.$sentry = SentryMock
<% } %>

const loadSentryHook = () => !loadInitiated && loadSentry(ctx, inject)
const loadSentryHook = () => attemptLoadSentry(ctx, inject)

<% if (options.lazy.injectLoadHook) { %>
inject('sentryLoad', loadSentryHook)
Expand All @@ -61,15 +63,31 @@ export default function SentryPlugin (ctx, inject) {
ctx.$sentryReady = sentryReady
}

async function loadSentry (ctx, inject) {
async function attemptLoadSentry(ctx, inject) {
if (loadInitiated) {
return
}

loadInitiated = true

if (!window.<%= globals.nuxt %>) {
<% if (options.dev) { %>
// eslint-disable-next-line no-console
console.warn(`$sentryLoad was called but window.<%= globals.nuxt %> is not available, delaying sentry loading until onNuxtReady callback. Do you really need to use lazy loading for Sentry?`)
<% } %>
<% if (options.lazy.injectLoadHook) { %>
window.<%= globals.readyCallback %>(() => loadSentry(ctx, inject))
<% } else { %>
// Wait for onNuxtReady hook to trigger.
<% } %>
return
}

await loadSentry(ctx, inject)
}

async function loadSentry (ctx, inject) {
if (loadCompleted) {
return
}

Expand Down Expand Up @@ -107,8 +125,8 @@ async function loadSentry (ctx, inject) {
Sentry.init(config)
<% } %>

<% if (options.lazy.injectMock) { %>
loadCompleted = true
<% if (options.lazy.injectMock) { %>
window.removeEventListener('error', SentryMock.captureException)

delayedCalls.forEach(([methodName, args]) => Sentry[methodName].apply(Sentry, args))
Expand Down

0 comments on commit a3de1cf

Please sign in to comment.