|
1 | 1 | import { DubApiError } from "../api/errors"; |
2 | 2 |
|
3 | | -export class DiscountIntegrationNotAvailableError extends DubApiError { |
4 | | - constructor({ message }: { message: string }) { |
5 | | - super({ code: "bad_request", message }); |
6 | | - this.name = "DiscountIntegrationNotAvailableError"; |
| 3 | +export type DiscountProviderErrorCode = |
| 4 | + | "INTEGRATION_NOT_AVAILABLE" |
| 5 | + | "AUTH_EXPIRED" |
| 6 | + | "DISCOUNT_ALREADY_EXISTS" |
| 7 | + | "COUPON_NOT_FOUND" |
| 8 | + | "PERMISSIONS_REQUIRED" |
| 9 | + | "CREATE_FAILED"; |
| 10 | + |
| 11 | +const API_CODE_BY_PROVIDER_CODE: Record< |
| 12 | + DiscountProviderErrorCode, |
| 13 | + "bad_request" | "conflict" | "internal_server_error" |
| 14 | +> = { |
| 15 | + INTEGRATION_NOT_AVAILABLE: "bad_request", |
| 16 | + AUTH_EXPIRED: "bad_request", |
| 17 | + DISCOUNT_ALREADY_EXISTS: "conflict", |
| 18 | + COUPON_NOT_FOUND: "bad_request", |
| 19 | + PERMISSIONS_REQUIRED: "bad_request", |
| 20 | + CREATE_FAILED: "internal_server_error", |
| 21 | +}; |
| 22 | + |
| 23 | +function resolveDiscountProviderMessage( |
| 24 | + provider: "stripe" | "shopify", |
| 25 | + providerCode: DiscountProviderErrorCode, |
| 26 | + message: string, |
| 27 | +): string { |
| 28 | + if (providerCode === "INTEGRATION_NOT_AVAILABLE") { |
| 29 | + return provider === "stripe" |
| 30 | + ? "STRIPE_CONNECTION_REQUIRED: Your workspace isn't connected to Stripe yet. Please install the Dub Stripe app in settings to create a discount." |
| 31 | + : "SHOPIFY_CONNECTION_REQUIRED: Your workspace isn't connected to Shopify yet. Please install the Dub Shopify app in settings to create a discount."; |
| 32 | + } |
| 33 | + |
| 34 | + if (providerCode === "AUTH_EXPIRED") { |
| 35 | + return provider === "stripe" |
| 36 | + ? "STRIPE_RECONNECT_REQUIRED: Your Stripe connection has expired or been revoked. Please reconnect the Dub Stripe app in settings." |
| 37 | + : "SHOPIFY_RECONNECT_REQUIRED: Your Shopify connection has expired or been revoked. Please reconnect the Dub Shopify app in settings."; |
| 38 | + } |
| 39 | + |
| 40 | + if (providerCode === "PERMISSIONS_REQUIRED") { |
| 41 | + return provider === "stripe" |
| 42 | + ? "STRIPE_APP_UPGRADE_REQUIRED: Your connected Stripe account doesn't have the permissions needed to create discount codes. Please upgrade your Stripe integration in settings or reach out to our support team for help." |
| 43 | + : "SHOPIFY_APP_UPGRADE_REQUIRED: Your connected Shopify store doesn't have permission to create discount codes. Please reinstall or upgrade the Dub Shopify app."; |
| 44 | + } |
| 45 | + |
| 46 | + return message; |
| 47 | +} |
| 48 | + |
| 49 | +export class DiscountProviderError extends DubApiError { |
| 50 | + constructor( |
| 51 | + public readonly provider: "stripe" | "shopify", |
| 52 | + public readonly providerCode: DiscountProviderErrorCode, |
| 53 | + message: string, |
| 54 | + ) { |
| 55 | + super({ |
| 56 | + code: API_CODE_BY_PROVIDER_CODE[providerCode], |
| 57 | + message: resolveDiscountProviderMessage(provider, providerCode, message), |
| 58 | + }); |
| 59 | + this.name = "DiscountProviderError"; |
7 | 60 | Object.setPrototypeOf(this, new.target.prototype); |
8 | 61 | } |
| 62 | + |
| 63 | + get isRecoverable() { |
| 64 | + return this.providerCode === "CREATE_FAILED"; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export function isDiscountProviderError( |
| 69 | + error: unknown, |
| 70 | +): error is DiscountProviderError { |
| 71 | + return error instanceof DiscountProviderError; |
9 | 72 | } |
10 | 73 |
|
11 | | -export function isDiscountIntegrationNotAvailableError( |
| 74 | +export function isNonRecoverableDiscountError( |
12 | 75 | error: unknown, |
13 | | -): error is DiscountIntegrationNotAvailableError { |
14 | | - return error instanceof DiscountIntegrationNotAvailableError; |
| 76 | +): error is DiscountProviderError { |
| 77 | + return isDiscountProviderError(error) && !error.isRecoverable; |
15 | 78 | } |
0 commit comments