Access from plugin context clientside

ThomasKientz
20
ThomasKientz
commented 7 months ago

I am trying to automatically track network errors.

export default function({ $axios, store, error, $raven }) {
  $axios.onError(e => {
    if (process.client) {
      $raven.captureException(e.response.data);
    }
    return Promise.reject(e);
  });
}

But $raven is undefined.

Any ideas ? I have read https://github.com/nuxt-community/sentry-module/issues/19 but I don't find the solution.

Thanks

0
aldarund
783
aldarund
commented 7 months ago

1) there no $raven in latest version of nuxt sentry, its $sentry
2) export default function({ $axios, store, error, {app: { $sentry } }) {

0
ThomasKientz
20
ThomasKientz
commented 7 months ago

@aldarund Thanks !

I was using v1 of sentry module using $raven.

So I have updated module to v2.

Some details

I don't know why, It doesn't work with destructured assignement :

export default function({ $axios, store, error, app: { $sentry } }) {

=> $sentry is still undefined.

It works though with :

export default function({ $axios, store, error, app }) {

and then accessing $sentry : app.$sentry

0
DiederikvandenB
395
DiederikvandenB
commented 7 months ago

Could you maybe share some more code? In which file are you calling this code, what are you importing, etc.

0
ThomasKientz
20
ThomasKientz
commented 7 months ago

@DiederikvandenB It's a simple nuxt plugin :

// plugins/axios.js
export default function({ $axios, store, error, app }) {
  $axios.onError(e => {
    if (process.client) {
      app.$sentry.configureScope(scope => {
        scope.setExtra("request", e);
        app.$sentry && app.$sentry.captureException(e.response.data);
      });
    }
    return Promise.reject(e);
  });
}
// nuxt.config.js
module.exports = {
  modules: [
    "@nuxtjs/axios",
  ],
  plugins: [
    "@/plugins/axios",
  ],
}
0
seandelaney
0
seandelaney
commented 7 months ago

What was the solution?

I have a similar issue where $sentry is undefined within my plugin: https://cmty.app/nuxt/sentry-module/issues/c36

0
ThomasKientz
20
ThomasKientz
commented 7 months ago

@seandelaney solution for me was (as you can see in the thread)

using

app.$sentry

with module updated to v2.

0
ThomasKientz
20
ThomasKientz
commented 7 months ago

Ok I think I finally get it ! My guess :

Sentry is working in client side only, but the plugin is not registered for client side only cause I need it to be initiated in server side (as I need for axios)

But I am calling $sentry only in $axios.onError(e => { ... app.$sentry }) through app and its working cause by the time $sentry had time to be injected !

0
DiederikvandenB
395
DiederikvandenB
commented 7 months ago

Sorry for not responding! Completely forgot. Very glad you were able to resolve it yourself.

0
Informations
Question โ€ข Unresolved
#c34 - Created 7 months ago