Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Export API Errors #1361

Merged
merged 2 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 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 { BadRequestError, NotFoundError } from '../../errors'
import type { EnhancedSku } from '../utils/enhanceSku'
import type { Options } from '..'
import type { Clients } from '../clients'
Expand Down Expand Up @@ -36,11 +36,11 @@ export const getSkuLoader = (_: Options, clients: Clients) => {
}, {} as Record<string, EnhancedSku>)

const skus = skuIds.map((skuId) => skuBySkuId[skuId])
const missingSkus = skus.filter((sku) => !sku)
const missingSkus = skuIds.filter((skuId) => !skuBySkuId[skuId])

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(',')}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the time I tested it, it was not working as expected.

Maybe it's a good opportunity to improve this logic here. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NICE! Sure!

)
}

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

This file was deleted.