Skip to content

Commit

Permalink
fix error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed Jun 10, 2022
1 parent 943421a commit 8aca7c1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
11 changes: 7 additions & 4 deletions src/pages/[...slug].tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNotFoundError } from '@faststore/api'
import {
formatSearchState,
parseSearchState,
Expand Down Expand Up @@ -174,16 +175,18 @@ export const getStaticProps: GetStaticProps<
operationName: query,
})

if (errors?.length > 0) {
throw new Error(`${errors[0]}`)
}
const notFound = errors.find(isNotFoundError)

if (data === null) {
if (notFound) {
return {
notFound: true,
}
}

if (errors.length > 0) {
throw errors[0]
}

return {
props: data,
}
Expand Down
13 changes: 8 additions & 5 deletions src/pages/[slug]/p.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { gql } from '@vtex/graphql-utils'
import { BreadcrumbJsonLd, NextSeo, ProductJsonLd } from 'next-seo'
import { useRouter } from 'next/router'
import type { GetStaticPaths, GetStaticProps } from 'next'
import { isNotFoundError } from '@faststore/api'

import ProductDetails from 'src/components/sections/ProductDetails'
import ProductShelf from 'src/components/sections/ProductShelf'
Expand Down Expand Up @@ -155,24 +156,26 @@ export const getStaticProps: GetStaticProps<
> = async ({ params }) => {
const id = params?.slug.split('-').pop() ?? ''

const { data, errors } = await execute<
const { data, errors = [] } = await execute<
ServerProductPageQueryQueryVariables,
ServerProductPageQueryQuery
>({
variables: { id },
operationName: query,
})

if (errors?.length > 0) {
throw new Error(`${errors[0]}`)
}
const notFound = errors.find(isNotFoundError)

if (data === null) {
if (notFound) {
return {
notFound: true,
}
}

if (errors.length > 0) {
throw errors[0]
}

return {
props: data,
}
Expand Down
6 changes: 4 additions & 2 deletions src/pages/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isFastStoreError } from '@faststore/api'
import type { NextApiHandler, NextApiRequest } from 'next'

import { execute } from '../../server'
Expand Down Expand Up @@ -46,8 +47,9 @@ const handler: NextApiHandler = async (request, response) => {
)

if (Array.isArray(result.errors)) {
// TODO: Return 400 on userError
response.status(500)
const error = result.errors.find(isFastStoreError)

response.status(error?.extensions.status ?? 500)
}

response.setHeader('cache-control', 'no-cache, no-store')
Expand Down
12 changes: 4 additions & 8 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { FormatErrorHandler } from '@envelop/core'
import { useGraphQlJit } from '@envelop/graphql-jit'
import { useParserCache } from '@envelop/parser-cache'
import { useValidationCache } from '@envelop/validation-cache'
import { getContextFactory, getSchema } from '@faststore/api'
import { getContextFactory, getSchema, isFastStoreError } from '@faststore/api'
import { GraphQLError } from 'graphql'
import type { Options as APIOptions } from '@faststore/api'

Expand Down Expand Up @@ -40,17 +40,13 @@ export const apiSchema = getSchema(apiOptions)

const apiContextFactory = getContextFactory(apiOptions)

const isBadRequestError = (err: GraphQLError) => {
return err.originalError && err.originalError.name === 'BadRequestError'
}

const formatError: FormatErrorHandler = (err) => {
console.error(err)

if (err instanceof GraphQLError && isBadRequestError(err)) {
if (err instanceof GraphQLError && isFastStoreError(err.originalError)) {
return err
}

console.error(err)

return new GraphQLError('Sorry, something went wrong.')
}

Expand Down

0 comments on commit 8aca7c1

Please sign in to comment.