Last active
August 30, 2019 14:41
-
-
Save sbatson5/45640a5acd9c53641502b18db3c7e770 to your computer and use it in GitHub Desktop.
Our error handled actions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import firebase from 'nativescript-plugin-firebase'; | |
| import { api } from '@/utils'; | |
| import store from '@/store'; | |
| /** | |
| * If we get an auth token failure, refresh it with firebase and try calling the same action again. | |
| * Otherwise, log an error listing which one failed | |
| */ | |
| const catchError = error => { | |
| const { status, data } = error.response; | |
| const { lastDispatchedAction, firebaseAuthUser } = store.state; | |
| if (status === 401 && data.error === 'Firebase token has expired') { | |
| return firebaseAuthUser.getIdToken(true) | |
| .then(idToken => { | |
| setToken(idToken); // Function to set token as a header | |
| return store.retryLastAction(); | |
| }) | |
| .catch(error => { | |
| console.error('📣: Could not fetch token from firebase', error); | |
| return store.dispatch('logout'); | |
| }); | |
| } else { | |
| console.error(`📣: ${lastDispatchedAction.name} -> error`, error); | |
| throw error; | |
| } | |
| }; | |
| const actions = { | |
| // Set onboarding content | |
| setWelcomeContent({ commit }) { | |
| return api.get(`/content`) | |
| .then(response => { | |
| commit('SET_WELCOME_CONTENT', response.data); | |
| }) | |
| .catch(catchError); | |
| }, | |
| setProfile({ commit }, payload) { | |
| return api.post('/profile', payload) | |
| .then(response => { | |
| commit('SET_PROFILE', response.data); | |
| }) | |
| .catch(catchError); | |
| } | |
| // etc. | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment