-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathdiscount-error.ts
More file actions
70 lines (61 loc) · 2.41 KB
/
Copy pathdiscount-error.ts
File metadata and controls
70 lines (61 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { DubApiError } from "../api/errors";
export type DiscountProviderErrorCode =
| "INTEGRATION_NOT_AVAILABLE"
| "DISCOUNT_ALREADY_EXISTS"
| "COUPON_NOT_FOUND"
| "PERMISSIONS_REQUIRED"
| "CREATE_FAILED";
const API_CODE_BY_PROVIDER_CODE: Record<
DiscountProviderErrorCode,
"bad_request" | "conflict" | "internal_server_error"
> = {
INTEGRATION_NOT_AVAILABLE: "bad_request",
DISCOUNT_ALREADY_EXISTS: "conflict",
COUPON_NOT_FOUND: "bad_request",
PERMISSIONS_REQUIRED: "bad_request",
CREATE_FAILED: "internal_server_error",
};
function resolveDiscountProviderMessage(
provider: "stripe" | "shopify",
providerCode: DiscountProviderErrorCode,
message: string,
): string {
if (providerCode === "INTEGRATION_NOT_AVAILABLE") {
return provider === "stripe"
? "STRIPE_CONNECTION_REQUIRED: Your workspace isn't connected to Stripe yet. Please install the Dub Stripe app in settings to create a discount."
: "SHOPIFY_CONNECTION_REQUIRED: Your workspace isn't connected to Shopify yet. Please install the Dub Shopify app in settings to create a discount.";
}
if (providerCode === "PERMISSIONS_REQUIRED") {
return provider === "stripe"
? "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."
: "SHOPIFY_APP_UPGRADE_REQUIRED: Your connected Shopify store doesn't have permission to create discount codes. Please reinstall or upgrade the Dub Shopify app.";
}
return message;
}
export class DiscountProviderError extends DubApiError {
constructor(
public readonly provider: "stripe" | "shopify",
public readonly providerCode: DiscountProviderErrorCode,
message: string,
) {
super({
code: API_CODE_BY_PROVIDER_CODE[providerCode],
message: resolveDiscountProviderMessage(provider, providerCode, message),
});
this.name = "DiscountProviderError";
Object.setPrototypeOf(this, new.target.prototype);
}
get isRecoverable() {
return this.providerCode === "CREATE_FAILED";
}
}
export function isDiscountProviderError(
error: unknown,
): error is DiscountProviderError {
return error instanceof DiscountProviderError;
}
export function isNonRecoverableDiscountError(
error: unknown,
): error is DiscountProviderError {
return isDiscountProviderError(error) && !error.isRecoverable;
}