Skip to content

Commit

Permalink
export errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed Jun 7, 2022
1 parent 5ba6ce3 commit 1e04204
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { typeDefs } from './typeDefs'
import type { Options as OptionsVTEX } from './platforms/vtex'

export * from './__generated__/schema'
export * from './platforms/errors'

export type Options = OptionsVTEX

Expand Down
45 changes: 45 additions & 0 deletions packages/api/src/platforms/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
type ErrorType = 'BadRequestError' | 'NotFoundError' | 'RedirectError'

interface Extension {
type: ErrorType
status: number
}

class FastStoreError<T extends Extension = Extension> extends Error {
constructor(public extensions: T, message?: string) {
super(message)
this.name = 'FastStoreError'
}
}

export class BadRequestError extends FastStoreError {
constructor(message?: string) {
super({ status: 400, type: 'BadRequestError' }, message)
}
}

export class NotFoundError extends FastStoreError {
constructor(message?: string) {
super({ status: 404, type: 'NotFoundError' }, message)
}
}

export class RedirectError extends FastStoreError<
Extension & { location: string; status: 301 | 302 }
> {
constructor(status: 301 | 302, location: string, message?: string) {
super({ status, location, type: 'RedirectError' }, message)
}
}

export const isFastStoreError = (error: any): error is FastStoreError =>
error?.name === 'FastStoreError'

export const isRedirectError = (error: any): error is RedirectError =>
error?.extensions?.type === 'RedirectError'

export const isNotFoundError = (error: any): error is NotFoundError =>
error?.extensions?.type === 'NotFoundError'

export const isBadRequestError = (error: any): error is BadRequestError =>
error?.extensions?.type === 'BadRequestError'
2 changes: 1 addition & 1 deletion packages/api/src/platforms/vtex/loaders/collection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import DataLoader from 'dataloader'
import pLimit from 'p-limit'

import { NotFoundError } from '../utils/errors'
import { NotFoundError } from '../../errors'
import type { CollectionPageType } from '../clients/commerce/types/Portal'
import type { Options } from '..'
import type { Clients } from '../clients'
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/platforms/vtex/resolvers/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export const StoreProduct: Record<string, Resolver<Root>> & {
name: ({ isVariantOf, name }) => name ?? isVariantOf.productName,
slug: ({ isVariantOf: { linkText }, itemId }) => getSlug(linkText, itemId),
description: ({ isVariantOf: { description } }) => description,
seo: ({ isVariantOf: { description, productName, linkText } }) => ({
seo: ({ isVariantOf: { description, productName, linkText, items } }) => ({
title: productName,
description,
canonical: `/${linkText}/p`,
canonical: `/${linkText}-${items[0].itemId}/p`,
}),
brand: ({ isVariantOf: { brand } }) => ({ name: brand }),
breadcrumbList: ({
Expand Down
8 changes: 5 additions & 3 deletions packages/api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RedirectError, BadRequestError } from '../../errors'
import { mutateChannelContext, mutateLocaleContext } from '../utils/contex'
import { enhanceSku } from '../utils/enhanceSku'
import { BadRequestError } from '../utils/errors'
import {
findChannel,
findLocale,
Expand Down Expand Up @@ -55,13 +55,15 @@ export const Query = {
// On standard VTEX PDP routes /slug/p does not contain sku ids. We need
// to figure out the sku id before returning a product
const products = await commerce.search.slug(slug)
const skuId = products?.[0]?.items[0]?.itemId
const product = products?.[0]
const skuId = product?.items[0]?.itemId
const location = skuId && `/${product.linkText}-${skuId}/p`

if (skuId == null) {
throw new BadRequestError(`Could not find product for slug ${slug}`)
}

return skuLoader.load(skuId)
throw new RedirectError(301, location)
}
}

Expand Down
13 changes: 0 additions & 13 deletions packages/api/src/platforms/vtex/utils/errors.ts

This file was deleted.

0 comments on commit 1e04204

Please sign in to comment.