diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index 4c522a7e18..858cca83b4 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -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 diff --git a/packages/api/src/platforms/errors.ts b/packages/api/src/platforms/errors.ts new file mode 100644 index 0000000000..97b6a6c1c3 --- /dev/null +++ b/packages/api/src/platforms/errors.ts @@ -0,0 +1,34 @@ +type ErrorType = 'BadRequestError' | 'NotFoundError' | 'RedirectError' + +interface Extension { + type: ErrorType + status: number +} + +class FastStoreError 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 const isFastStoreError = (error: any): error is FastStoreError => + error?.name === 'FastStoreError' + +export const isNotFoundError = (error: any): error is NotFoundError => + error?.extensions?.type === 'NotFoundError' + +export const isBadRequestError = (error: any): error is BadRequestError => + error?.extensions?.type === 'BadRequestError' diff --git a/packages/api/src/platforms/vtex/loaders/collection.ts b/packages/api/src/platforms/vtex/loaders/collection.ts index a1c66d9d10..32fe3ee2f4 100644 --- a/packages/api/src/platforms/vtex/loaders/collection.ts +++ b/packages/api/src/platforms/vtex/loaders/collection.ts @@ -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' diff --git a/packages/api/src/platforms/vtex/loaders/sku.ts b/packages/api/src/platforms/vtex/loaders/sku.ts index 75a98c2afd..11d455151d 100644 --- a/packages/api/src/platforms/vtex/loaders/sku.ts +++ b/packages/api/src/platforms/vtex/loaders/sku.ts @@ -1,7 +1,7 @@ import DataLoader from 'dataloader' -import { BadRequestError } from '../utils/errors' import { enhanceSku } from '../utils/enhanceSku' +import { BadRequestError, NotFoundError } from '../../errors' import type { EnhancedSku } from '../utils/enhanceSku' import type { Options } from '..' import type { Clients } from '../clients' @@ -39,8 +39,8 @@ export const getSkuLoader = (_: Options, clients: Clients) => { const missingSkus = skus.filter((sku) => !sku) if (missingSkus.length > 0) { - throw new Error( - `Search API did not return the following skus: ${missingSkus.join(',')}` + throw new NotFoundError( + `Search API did not found the following skus: ${missingSkus.join(',')}` ) } diff --git a/packages/api/src/platforms/vtex/utils/errors.ts b/packages/api/src/platforms/vtex/utils/errors.ts deleted file mode 100644 index ddbcef8b28..0000000000 --- a/packages/api/src/platforms/vtex/utils/errors.ts +++ /dev/null @@ -1,13 +0,0 @@ -export class BadRequestError extends Error { - constructor(message: string) { - super(message) - this.name = 'BadRequestError' - } -} - -export class NotFoundError extends Error { - constructor(message: string) { - super(message) - this.name = 'NotFoundError' - } -}