Skip to content

Commit

Permalink
export error
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed Jun 10, 2022
1 parent 1654ccb commit 2dab0da
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 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
34 changes: 34 additions & 0 deletions packages/api/src/platforms/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 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'
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
6 changes: 3 additions & 3 deletions packages/api/src/platforms/vtex/loaders/sku.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import DataLoader from 'dataloader'

import { BadRequestError } from '../utils/errors'
import { enhanceSku } from '../utils/enhanceSku'
import { NotFoundError } from '../../errors'
import type { EnhancedSku } from '../utils/enhanceSku'
import type { Options } from '..'
import type { Clients } from '../clients'
Expand Down Expand Up @@ -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(',')}`
)
}

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

This file was deleted.

0 comments on commit 2dab0da

Please sign in to comment.