diff --git a/src/pages/[...slug].tsx b/src/pages/[...slug].tsx index e6dab594..73f2fac1 100644 --- a/src/pages/[...slug].tsx +++ b/src/pages/[...slug].tsx @@ -1,3 +1,4 @@ +import { isNotFoundError } from '@faststore/api' import { formatSearchState, parseSearchState, @@ -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, } diff --git a/src/pages/[slug]/p.tsx b/src/pages/[slug]/p.tsx index d7e5b90e..cc389a10 100644 --- a/src/pages/[slug]/p.tsx +++ b/src/pages/[slug]/p.tsx @@ -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' @@ -155,7 +156,7 @@ export const getStaticProps: GetStaticProps< > = async ({ params }) => { const id = params?.slug.split('-').pop() ?? '' - const { data, errors } = await execute< + const { data, errors = [] } = await execute< ServerProductPageQueryQueryVariables, ServerProductPageQueryQuery >({ @@ -163,16 +164,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, } diff --git a/src/pages/api/graphql.ts b/src/pages/api/graphql.ts index 9af70e3d..e840d4ef 100644 --- a/src/pages/api/graphql.ts +++ b/src/pages/api/graphql.ts @@ -1,3 +1,4 @@ +import { isFastStoreError } from '@faststore/api' import type { NextApiHandler, NextApiRequest } from 'next' import { execute } from '../../server' @@ -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') diff --git a/src/server/index.ts b/src/server/index.ts index 02c4137e..80c69967 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -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' @@ -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.') }