-
Notifications
You must be signed in to change notification settings - Fork 926
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
Add error listeners before Auth.init() #334
Conversation
In the current setup, if you want to create an error listener, you should create a plugin, and load them through the config with the setting auth.plugins, like so: `plugins: ['~/plugins/auth-error-listener.js']` The plugins are loaded *after* the main Auth plugin itself is loaded. The main plugin takes care of the init. So if an error occurs during init, it's impossible to catch it with a custom error listener. This commit adds a new config option, called `errorListeners`, it's used in the same way as plugins, so: `errorListeners: ['~/utils/auth-error-listener.js']` The plugin itself isn't a nuxt plugin, so it should export a default function: ` export default function(error) { console.log('error listener') console.log(error.response.data) console.log(error.response.status) } ` ErrorListeners will be loaded *before* init. This change should be backwards compatible, since the old method of loading listeners through plugins still works.
Awesome! 👍 |
How can we access the Nuxt context or the $auth object inside a listener? |
@dappiu I don't think you can, not with the new way of instantiating error listeners. The old method is just a 'regular' plugin, so you can get the context of the Vue app through the plugin (more info: https://nuxtjs.org/guide/plugins/). This new method is specifically meant to catch errors during Init of the $auth, as such I didn't think it would be important to have acces to the context or $auth. (I'm not even sure you can count on them already being constructed...) If you really needed $auth and context in the listener, loaded through |
Yeah, I do agree, but I don't see the difference with installing your listeners by But let me explain why I'm interested on this, and maybe my point of view will be clearer. |
Just to clarify: With regards to your second part of your comment. I don't think there's a way to do this less hacky at the moment. An alternative to your solution (if this PR gets merged) could be the following:
But it's not ideal. This module is in need of somebody doing some merges of PR's. |
Hi @gijswijs. First of all thanks for your contribution and sorry for the late PR review. Actually, I agree with @dappiu. We can register a normal nuxt plugin before auth module to intercept axios. With #234 we now handle I'm closing this PR by assuming #234 will cover it. BTW open to discuss if your requirement is not covered. Thanks. |
In the current setup, if you want to create an error listener, you
should create a plugin, and load them through the config with the
setting auth.plugins, like so:
plugins: ['~/plugins/auth-error-listener.js']
The plugins are loaded after the main Auth plugin itself is loaded.
The main plugin takes care of the init. So if an error occurs during
init, it's impossible to catch it with a custom error listener. As mentioned
in issue #220.
This commit adds a new config option, called
errorListeners
, it's usedin the same way as plugins, so:
errorListeners: ['~/utils/auth-error-listener.js']
The listener itself isn't a nuxt plugin anymore. It should export a
default function:
ErrorListeners will be loaded before init. This change should be
backwards compatible, since the old method of loading listeners through
plugins still works.