Skip to content

Commit

Permalink
feat: significantly better error handling on store functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rylanharper committed Nov 30, 2024
1 parent a8398a3 commit f66c703
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 45 deletions.
11 changes: 9 additions & 2 deletions app/pages/account/addresses/add.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ const handleNewAddress = async () => {
customerAccessToken: authStore.accessToken
});
if (response?.customerUserErrors?.length) {
throw new Error(response?.customerUserErrors[0]?.message);
}
const newId = response?.customerAddress?.id;
if (defaultAddress.value && newId) {
if (newId && defaultAddress.value) {
await shopify.customer.updateDefaultAddress({
addressId: newId,
customerAccessToken: authStore.accessToken
Expand All @@ -87,7 +91,10 @@ const handleNewAddress = async () => {
}
} catch (error) {
console.error('Error during address creation:', error);
errorMessage.value = 'An error occurred. Please try again later.';
if (error instanceof Error) {
errorMessage.value = `${error.message}. Please try again later.`;
}
} finally {
isLoading.value = false;
}
Expand Down
9 changes: 8 additions & 1 deletion app/pages/account/addresses/edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ const handleUpdateAddress = async () => {
id: decodeURIComponent(urlQuery.id as string)
});
if (response?.customerUserErrors?.length) {
throw new Error(response?.customerUserErrors[0]?.message);
}
if (defaultAddress.value) {
await shopify.customer.updateDefaultAddress({
addressId: decodeURIComponent(urlQuery.id as string),
Expand All @@ -90,7 +94,10 @@ const handleUpdateAddress = async () => {
}
} catch (error) {
console.error('Error during customer account update:', error);
errorMessage.value = 'An error occurred. Please try again later.';
if (error instanceof Error) {
errorMessage.value = `${error.message}. Please try again later.`;
}
} finally {
isLoading.value = false;
}
Expand Down
5 changes: 4 additions & 1 deletion app/pages/account/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ const handleLogin = async () => {
}
} catch (error) {
console.error('Error during account login:', error);
errorMessage.value = 'An error occurred. Please try again later.';
if (error instanceof Error) {
errorMessage.value = `${error.message}. Please try again later.`;
}
} finally {
isLoading.value = false;
}
Expand Down
5 changes: 4 additions & 1 deletion app/pages/account/recover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const handleRecover = async () => {
successMessage.value = 'Success! Please check your email for instructions on how to reset your password.';
} catch (error) {
console.error('Error during password recovery:', error);
errorMessage.value = 'An error occurred. Please try again later.';
if (error instanceof Error) {
errorMessage.value = `${error.message}. Please try again later.`;
}
} finally {
isLoading.value = false;
}
Expand Down
5 changes: 4 additions & 1 deletion app/pages/account/register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ const handleRegister = async () => {
}
} catch (error) {
console.error('Error during account registration:', error);
errorMessage.value = 'An error occurred. Please try again later.';
if (error instanceof Error) {
errorMessage.value = `${error.message}. Please try again later.`;
}
} finally {
isLoading.value = false;
}
Expand Down
5 changes: 4 additions & 1 deletion app/pages/account/reset.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ const handleReset = async () => {
}
} catch (error) {
console.error('Error during password reset:', error);
errorMessage.value = 'An error occurred. Please try again later.';
if (error instanceof Error) {
errorMessage.value = `${error.message}. Please try again later.`;
}
} finally {
isLoading.value = false;
}
Expand Down
76 changes: 51 additions & 25 deletions app/stores/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ export const useAuthStore = defineStore('@nitrogen/auth', {
input: input
});

if (response?.customerUserErrors?.length) {
throw new Error(response?.customerUserErrors[0]?.message);
}

if (response?.customerAccessToken) {
this.accessToken = response.customerAccessToken.accessToken;
await this.getCustomer();
}
} catch (error) {
console.error('Cannot create customer token', error);
console.error('Cannot create customer token:', error);
throw error;
}
},
/**
Expand All @@ -51,19 +56,22 @@ export const useAuthStore = defineStore('@nitrogen/auth', {
customerAccessToken: this.accessToken
});

if (response) {
const customerInfo = {
id: response.id,
email: response.email,
firstName: response.firstName,
lastName: response.lastName
// Add more if needed...
};

this.customer = customerInfo;
if (!response) {
throw new Error('Customer data not found.');
}

const customerInfo = {
id: response.id,
email: response.email,
firstName: response.firstName,
lastName: response.lastName
// Add more if needed...
};

this.customer = customerInfo;
} catch (error) {
console.error('Cannot get customer data', error);
console.error('No data retrieved from customer query:', error);
throw error;
}
},
/**
Expand All @@ -76,14 +84,19 @@ export const useAuthStore = defineStore('@nitrogen/auth', {
input: input
});

if (response?.customerUserErrors?.length) {
throw new Error(response?.customerUserErrors[0]?.message);
}

if (response?.customer) {
await this.createToken({
email: input.email,
password: input.password
});
}
} catch (error) {
console.error('Cannot create new customer', error);
console.error('Cannot create new customer:', error);
throw error;
}
},
/**
Expand All @@ -92,14 +105,10 @@ export const useAuthStore = defineStore('@nitrogen/auth', {
* @param password - The customer's password
*/
async login(email: string, password: string) {
try {
await this.createToken({
email: email,
password: password
});
} catch (error) {
console.error('Cannot login customer', error);
}
await this.createToken({
email: email,
password: password
});
},
/**
* Logs out the customer, deletes the customer access token.
Expand All @@ -110,12 +119,17 @@ export const useAuthStore = defineStore('@nitrogen/auth', {
customerAccessToken: this.accessToken
});

if (response?.userErrors?.length) {
throw new Error(response?.userErrors[0]?.message);
}

if (response?.deletedAccessToken) {
this.accessToken = '';
this.customer = null;
}
} catch (error) {
console.error('Cannot logout customer', error);
console.error('Cannot logout customer:', error);
throw error;
}
},
/**
Expand All @@ -124,11 +138,18 @@ export const useAuthStore = defineStore('@nitrogen/auth', {
*/
async recover(email: string) {
try {
await shopify.customer.recover({
const response = await shopify.customer.recover({
email: email
});

if (response?.customerUserErrors?.length) {
throw new Error(response?.customerUserErrors[0]?.message);
}

return response;
} catch (error) {
console.error('Cannot reccover password', error);
console.error('Cannot reccover password:', error);
throw error;
}
},
/**
Expand All @@ -147,12 +168,17 @@ export const useAuthStore = defineStore('@nitrogen/auth', {
}
});

if (response?.customerUserErrors?.length) {
throw new Error(response?.customerUserErrors[0]?.message);
}

if (response?.customerAccessToken) {
this.accessToken = response.customerAccessToken.accessToken;
await this.getCustomer();
}
} catch (error) {
console.error('Cannot reset password', error);
console.error('Cannot reset password:', error);
throw error;
}
}
},
Expand Down
42 changes: 35 additions & 7 deletions app/stores/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ export const useCartStore = defineStore('@nitrogen/cart', {
...optionalParams
});

if (response?.userErrors?.length) {
throw new Error(response?.userErrors[0]?.message);
}

if (response?.cart) {
this.cart = response.cart;
}
} catch (error) {
console.error('No cart returned from cartCreate mutation', error);
console.error('No cart returned from cartCreate mutation:', error);
throw error;
}
},
/**
Expand All @@ -66,11 +71,14 @@ export const useCartStore = defineStore('@nitrogen/cart', {
...optionalParams
});

if (response) {
this.cart = response;
if (!response) {
throw new Error('Cart data not found.');
}

this.cart = response;
} catch (error) {
console.error('No cart retrieved from cart query', error);
console.error('No data retrieved from cart query:', error);
throw error;
}
},
/**
Expand All @@ -91,11 +99,16 @@ export const useCartStore = defineStore('@nitrogen/cart', {
...optionalParams
});

if (response?.userErrors?.length) {
throw new Error(response?.userErrors[0]?.message);
}

if (response?.cart) {
this.cart = response.cart;
}
} catch (error) {
console.error('Cannot add item to cart', error);
console.error('Cannot add item to cart:', error);
throw error;
}
},
/**
Expand All @@ -116,11 +129,16 @@ export const useCartStore = defineStore('@nitrogen/cart', {
...optionalParams
});

if (response?.userErrors?.length) {
throw new Error(response?.userErrors[0]?.message);
}

if (response?.cart) {
this.cart = response.cart;
}
} catch (error) {
console.error('Cannot remove item from cart', error);
console.error('Cannot remove item from cart:', error);
throw error;
}
},
/**
Expand All @@ -141,11 +159,16 @@ export const useCartStore = defineStore('@nitrogen/cart', {
...optionalParams
});

if (response?.userErrors?.length) {
throw new Error(response?.userErrors[0]?.message);
}

if (response?.cart) {
this.cart = response.cart;
}
} catch (error) {
console.error('Cannot update cart item', error);
console.error('Cannot update cart item:', error);
throw error;
}
},
/**
Expand All @@ -166,11 +189,16 @@ export const useCartStore = defineStore('@nitrogen/cart', {
...optionalParams
});

if (response?.userErrors?.length) {
throw new Error(response?.userErrors[0]?.message);
}

if (response?.cart) {
this.cart = response.cart;
}
} catch (error) {
console.error('Cannot update cart buyer identity', error);
throw error;
}
}
},
Expand Down
15 changes: 9 additions & 6 deletions app/stores/shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ export const useShopStore = defineStore('@nitrogen/shop', {
language: newLanguageCode ?? this.locale.language.isoCode
});

if (response) {
this.locale.availableCountries = response.availableCountries;
this.locale.availableLanguages = response.availableLanguages;
this.locale.country = response.country;
this.locale.language = response.language;
if (!response) {
throw new Error('Localization data not found.');
}

this.locale.availableCountries = response.availableCountries;
this.locale.availableLanguages = response.availableLanguages;
this.locale.country = response.country;
this.locale.language = response.language;
} catch (error) {
console.error('Cannot get localization data from Shopify', error);
console.error('No data retrieved from localization query:', error);
throw error;
}
}
},
Expand Down

0 comments on commit f66c703

Please sign in to comment.