Improve error handling

This commit is contained in:
Michael Bromley 2021-01-25 21:24:26 +01:00
parent 695caba290
commit d4e8105ce0
8 changed files with 152 additions and 40 deletions

View File

@ -1,15 +1,21 @@
import type { ServerResponse } from 'http'
import type { LoginMutation, LoginMutationVariables } from '../schema'
import type { RecursivePartial } from '../api/utils/types'
import concatHeader from '../api/utils/concat-cookie'
import { VendureConfig, getConfig } from '../api'
import { getConfig, VendureConfig } from '../api'
import { CommerceError } from '@commerce/utils/errors'
import { ErrorResult } from '../schema'
export const loginMutation = /* GraphQL */ `
mutation loginServer($email: String!, $password: String!) {
login(username: $email, password: $password) {
__typename
... on CurrentUser {
id
}
... on ErrorResult {
errorCode
message
}
}
}
`
@ -44,10 +50,11 @@ async function login({
}): Promise<LoginResult> {
config = getConfig(config)
const { data, res } = await config.fetch<RecursivePartial<LoginMutation>>(
query,
{ variables }
)
const { data, res } = await config.fetch<LoginMutation>(query, { variables })
if (data.login.__typename !== 'CurrentUser') {
throw new CommerceError({ message: (data.login as ErrorResult).message })
}
// Bigcommerce returns a Set-Cookie header with the auth cookie
let cookie = res.headers.get('Set-Cookie')
@ -68,7 +75,7 @@ async function login({
}
return {
result: data.login?.result,
result: data.login.id.toString(),
}
}

View File

@ -3,7 +3,11 @@ import type { HookFetcher } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors'
import useCommerceLogin from '@commerce/use-login'
import useCustomer from '../customer/use-customer'
import { LoginMutation, LoginMutationVariables } from '@framework/schema'
import {
ErrorResult,
LoginMutation,
LoginMutationVariables,
} from '@framework/schema'
export const loginMutation = /* GraphQL */ `
mutation login($username: String!, $password: String!) {
@ -12,6 +16,10 @@ export const loginMutation = /* GraphQL */ `
... on CurrentUser {
id
}
... on ErrorResult {
errorCode
message
}
}
}
`
@ -49,7 +57,9 @@ export function extendHook(customFetcher: typeof fetcher) {
password: input.password,
})
if (data.login.__typename !== 'CurrentUser') {
throw new CommerceError({ message: 'The credentials are not valid' })
throw new CommerceError({
message: (data.login as ErrorResult).message,
})
}
await revalidate()
return data

View File

@ -3,14 +3,23 @@ import type { HookFetcher } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors'
import useCommerceSignup from '@commerce/use-signup'
import useCustomer from '../customer/use-customer'
import { SignupMutation, SignupMutationVariables } from '@framework/schema'
import {
ErrorResult,
SignupMutation,
SignupMutationVariables,
} from '@framework/schema'
export const signupMutation = /* GraphQL */ `
mutation signup($input: RegisterCustomerInput!) {
registerCustomerAccount(input: $input) {
__typename
... on Success {
success
}
... on ErrorResult {
errorCode
message
}
}
}
`
@ -52,7 +61,7 @@ export function extendHook(customFetcher: typeof fetcher) {
return useCallback(
async function signup(input: SignupInput) {
const data = await fn({
const { registerCustomerAccount } = await fn({
input: {
firstName: input.firstName,
lastName: input.lastName,
@ -60,8 +69,13 @@ export function extendHook(customFetcher: typeof fetcher) {
password: input.password,
},
})
if (registerCustomerAccount.__typename !== 'Success') {
throw new CommerceError({
message: (registerCustomerAccount as ErrorResult).message,
})
}
await revalidate()
return data
return { registerCustomerAccount }
},
[fn]
)

View File

@ -8,12 +8,18 @@ import { cartFragment } from '../api/fragments/cart'
import {
AddItemToOrderMutation,
AddItemToOrderMutationVariables,
ErrorResult,
} from '@framework/schema'
export const addItemToOrderMutation = /* GraphQL */ `
mutation addItemToOrder($variantId: ID!, $quantity: Int!) {
addItemToOrder(productVariantId: $variantId, quantity: $quantity) {
__typename
...Cart
... on ErrorResult {
errorCode
message
}
}
}
${cartFragment}
@ -39,9 +45,6 @@ export const fetcher: HookFetcher<
...options,
query: addItemToOrderMutation,
variables: { variantId, quantity: quantity || 1 },
}).then((res) => {
console.log({ res })
return res
})
}
@ -52,12 +55,18 @@ export function extendHook(customFetcher: typeof fetcher) {
return useCallback(
async function addItem(input: AddItemInput) {
const data = await fn({
const { addItemToOrder } = await fn({
quantity: input.quantity || 1,
variantId: input.variantId,
})
await mutate(data, false)
return data
if (addItemToOrder.__typename === 'Order') {
await mutate({ addItemToOrder }, false)
} else {
throw new CommerceError({
message: (addItemToOrder as ErrorResult).message,
})
}
return { addItemToOrder }
},
[fn, mutate]
)

View File

@ -30,7 +30,7 @@ export function extendHook(
swrOptions?: SwrOptions<any | null>
) {
const useCart = () => {
const response = useData<Cart>(
const response = useData<CartResult>(
{ query: getCartQuery },
[],
customFetcher,

View File

@ -4,15 +4,21 @@ import useCartRemoveItem from '@commerce/cart/use-remove-item'
import useCart from './use-cart'
import { cartFragment } from '@framework/api/fragments/cart'
import {
ErrorResult,
RemoveOrderLineMutation,
RemoveOrderLineMutationVariables,
} from '@framework/schema'
import { CommerceError } from '@commerce/utils/errors'
export const removeOrderLineMutation = /* GraphQL */ `
mutation removeOrderLine($orderLineId: ID!) {
removeOrderLine(orderLineId: $orderLineId) {
__typename
...Cart
... on ErrorResult {
errorCode
message
}
}
}
${cartFragment}
@ -42,6 +48,10 @@ export function extendHook(customFetcher: typeof fetcher) {
const { removeOrderLine } = await fn({ orderLineId: input.id })
if (removeOrderLine.__typename === 'Order') {
await mutate({ removeOrderLine }, false)
} else {
throw new CommerceError({
message: (removeOrderLine as ErrorResult).message,
})
}
return { removeOrderLine }
},

View File

@ -7,12 +7,19 @@ import { cartFragment } from '@framework/api/fragments/cart'
import {
AdjustOrderLineMutation,
AdjustOrderLineMutationVariables,
ErrorResult,
} from '@framework/schema'
import { CommerceError } from '@commerce/utils/errors'
export const adjustOrderLineMutation = /* GraphQL */ `
mutation adjustOrderLine($orderLineId: ID!, $quantity: Int!) {
adjustOrderLine(orderLineId: $orderLineId, quantity: $quantity) {
__typename
...Cart
... on ErrorResult {
errorCode
message
}
}
}
${cartFragment}
@ -44,6 +51,10 @@ function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
})
if (adjustOrderLine.__typename === 'Order') {
await mutate({ adjustOrderLine }, false)
} else {
throw new CommerceError({
message: (adjustOrderLine as ErrorResult).message,
})
}
return { adjustOrderLine }
}, cfg?.wait ?? 500),

View File

@ -2843,10 +2843,19 @@ export type LoginServerMutationVariables = Exact<{
export type LoginServerMutation = { __typename?: 'Mutation' } & {
login:
| ({ __typename?: 'CurrentUser' } & Pick<CurrentUser, 'id'>)
| { __typename?: 'InvalidCredentialsError' }
| { __typename?: 'NotVerifiedError' }
| { __typename?: 'NativeAuthStrategyError' }
| ({ __typename: 'CurrentUser' } & Pick<CurrentUser, 'id'>)
| ({ __typename: 'InvalidCredentialsError' } & Pick<
InvalidCredentialsError,
'errorCode' | 'message'
>)
| ({ __typename: 'NotVerifiedError' } & Pick<
NotVerifiedError,
'errorCode' | 'message'
>)
| ({ __typename: 'NativeAuthStrategyError' } & Pick<
NativeAuthStrategyError,
'errorCode' | 'message'
>)
}
export type LoginMutationVariables = Exact<{
@ -2857,9 +2866,18 @@ export type LoginMutationVariables = Exact<{
export type LoginMutation = { __typename?: 'Mutation' } & {
login:
| ({ __typename: 'CurrentUser' } & Pick<CurrentUser, 'id'>)
| { __typename: 'InvalidCredentialsError' }
| { __typename: 'NotVerifiedError' }
| { __typename: 'NativeAuthStrategyError' }
| ({ __typename: 'InvalidCredentialsError' } & Pick<
InvalidCredentialsError,
'errorCode' | 'message'
>)
| ({ __typename: 'NotVerifiedError' } & Pick<
NotVerifiedError,
'errorCode' | 'message'
>)
| ({ __typename: 'NativeAuthStrategyError' } & Pick<
NativeAuthStrategyError,
'errorCode' | 'message'
>)
}
export type LogoutMutationVariables = Exact<{ [key: string]: never }>
@ -2874,9 +2892,15 @@ export type SignupMutationVariables = Exact<{
export type SignupMutation = { __typename?: 'Mutation' } & {
registerCustomerAccount:
| ({ __typename?: 'Success' } & Pick<Success, 'success'>)
| { __typename?: 'MissingPasswordError' }
| { __typename?: 'NativeAuthStrategyError' }
| ({ __typename: 'Success' } & Pick<Success, 'success'>)
| ({ __typename: 'MissingPasswordError' } & Pick<
MissingPasswordError,
'errorCode' | 'message'
>)
| ({ __typename: 'NativeAuthStrategyError' } & Pick<
NativeAuthStrategyError,
'errorCode' | 'message'
>)
}
export type AddItemToOrderMutationVariables = Exact<{
@ -2886,11 +2910,23 @@ export type AddItemToOrderMutationVariables = Exact<{
export type AddItemToOrderMutation = { __typename?: 'Mutation' } & {
addItemToOrder:
| ({ __typename?: 'Order' } & CartFragment)
| { __typename?: 'OrderModificationError' }
| { __typename?: 'OrderLimitError' }
| { __typename?: 'NegativeQuantityError' }
| { __typename?: 'InsufficientStockError' }
| ({ __typename: 'Order' } & CartFragment)
| ({ __typename: 'OrderModificationError' } & Pick<
OrderModificationError,
'errorCode' | 'message'
>)
| ({ __typename: 'OrderLimitError' } & Pick<
OrderLimitError,
'errorCode' | 'message'
>)
| ({ __typename: 'NegativeQuantityError' } & Pick<
NegativeQuantityError,
'errorCode' | 'message'
>)
| ({ __typename: 'InsufficientStockError' } & Pick<
InsufficientStockError,
'errorCode' | 'message'
>)
}
export type ActiveOrderQueryVariables = Exact<{ [key: string]: never }>
@ -2906,7 +2942,10 @@ export type RemoveOrderLineMutationVariables = Exact<{
export type RemoveOrderLineMutation = { __typename?: 'Mutation' } & {
removeOrderLine:
| ({ __typename: 'Order' } & CartFragment)
| { __typename: 'OrderModificationError' }
| ({ __typename: 'OrderModificationError' } & Pick<
OrderModificationError,
'errorCode' | 'message'
>)
}
export type AdjustOrderLineMutationVariables = Exact<{
@ -2916,11 +2955,23 @@ export type AdjustOrderLineMutationVariables = Exact<{
export type AdjustOrderLineMutation = { __typename?: 'Mutation' } & {
adjustOrderLine:
| ({ __typename?: 'Order' } & CartFragment)
| { __typename?: 'OrderModificationError' }
| { __typename?: 'OrderLimitError' }
| { __typename?: 'NegativeQuantityError' }
| { __typename?: 'InsufficientStockError' }
| ({ __typename: 'Order' } & CartFragment)
| ({ __typename: 'OrderModificationError' } & Pick<
OrderModificationError,
'errorCode' | 'message'
>)
| ({ __typename: 'OrderLimitError' } & Pick<
OrderLimitError,
'errorCode' | 'message'
>)
| ({ __typename: 'NegativeQuantityError' } & Pick<
NegativeQuantityError,
'errorCode' | 'message'
>)
| ({ __typename: 'InsufficientStockError' } & Pick<
InsufficientStockError,
'errorCode' | 'message'
>)
}
export type GetCollectionsQueryVariables = Exact<{ [key: string]: never }>