File size: 5,412 Bytes
16cdcb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package taxcode

import (
	"errors"
	"net/http"

	"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
	"github.com/openmeterio/openmeter/pkg/models"
)

const ErrCodeResourceNamespaceEmpty models.ErrorCode = "resource_namespace_empty"

var ErrResourceNamespaceEmpty = models.NewValidationIssue(
	ErrCodeResourceNamespaceEmpty,
	"namespace must not be empty",
	models.WithFieldString("namespace"),
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusBadRequest),
)

const ErrCodeResourceKeyEmpty models.ErrorCode = "resource_key_empty"

var ErrResourceKeyEmpty = models.NewValidationIssue(
	ErrCodeResourceKeyEmpty,
	"key must not be empty",
	models.WithFieldString("key"),
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusBadRequest),
)

const ErrCodeResourceIDEmpty models.ErrorCode = "resource_id_empty"

var ErrResourceIDEmpty = models.NewValidationIssue(
	ErrCodeResourceIDEmpty,
	"id must not be empty",
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusBadRequest),
)

const ErrCodeResourceNameEmpty models.ErrorCode = "resource_name_empty"

var ErrResourceNameEmpty = models.NewValidationIssue(
	ErrCodeResourceNameEmpty,
	"name must not be empty",
	models.WithFieldString("name"),
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusBadRequest),
)

const ErrCodeAppTypesMustBeUnique models.ErrorCode = "app_types_must_be_unique"

var ErrAppTypesMustBeUnique = models.NewValidationIssue(
	ErrCodeAppTypesMustBeUnique,
	"app types must be unique",
	models.WithFieldString("app_mappings"),
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusBadRequest),
)

const ErrCodeTaxCodeNotFound models.ErrorCode = "tax_code_not_found"

var ErrTaxCodeNotFound = models.NewValidationIssue(
	ErrCodeTaxCodeNotFound,
	"tax code not found",
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusNotFound),
)

func NewTaxCodeNotFoundError(id string) error {
	return ErrTaxCodeNotFound.
		WithPathString("id").
		WithAttr("id", id)
}

func NewTaxCodeByKeyNotFoundError(key string) error {
	return ErrTaxCodeNotFound.
		WithPathString("key").
		WithAttr("key", key)
}

func NewTaxCodeByAppMappingNotFoundError(appType, taxCode string) error {
	return ErrTaxCodeNotFound.
		WithPathString("app_mappings").
		WithAttr("app_type", appType).
		WithAttr("tax_code", taxCode)
}

func IsTaxCodeNotFoundError(err error) bool {
	var vi models.ValidationIssue
	return errors.As(err, &vi) && vi.Code() == ErrCodeTaxCodeNotFound
}

const ErrCodeTaxCodeEmpty models.ErrorCode = "tax_code_empty"

var ErrTaxCodeEmpty = models.NewValidationIssue(
	ErrCodeTaxCodeEmpty,
	"tax code cannot be empty",
	models.WithFieldString("app_mappings"),
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusBadRequest),
)

const ErrCodeTaxCodeManagedBySystem models.ErrorCode = "tax_code_managed_by_system"

var ErrTaxCodeManagedBySystem = models.NewValidationIssue(
	ErrCodeTaxCodeManagedBySystem,
	"tax code is managed by the system and cannot be modified or deleted",
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusConflict),
)

const ErrCodeTaxCodeStripeInvalid models.ErrorCode = "tax_code_stripe_invalid"

var ErrTaxCodeStripeInvalid = models.NewValidationIssue(
	ErrCodeTaxCodeStripeInvalid,
	"stripe tax code must be in the format of txcd_12345678",
	models.WithFieldString("app_mappings"),
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusBadRequest),
)

const ErrCodeOrganizationDefaultTaxCodesNotFound models.ErrorCode = "organization_default_tax_codes_not_found"

var ErrOrganizationDefaultTaxCodesNotFound = models.NewValidationIssue(
	ErrCodeOrganizationDefaultTaxCodesNotFound,
	"organization default tax codes not found",
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusNotFound),
)

func NewOrganizationDefaultTaxCodesNotFoundError(namespace string) error {
	return ErrOrganizationDefaultTaxCodesNotFound.
		WithPathString("namespace").
		WithAttr("namespace", namespace)
}

func IsOrganizationDefaultTaxCodesNotFoundError(err error) bool {
	var vi models.ValidationIssue
	return errors.As(err, &vi) && vi.Code() == ErrCodeOrganizationDefaultTaxCodesNotFound
}

const ErrCodeTaxCodeIsOrganizationDefault models.ErrorCode = "tax_code_is_organization_default"

var ErrTaxCodeIsOrganizationDefault = models.NewValidationIssue(
	ErrCodeTaxCodeIsOrganizationDefault,
	"tax code is set as an organization default and cannot be deleted",
	models.WithCriticalSeverity(),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusConflict),
)

func IsTaxCodeIsOrganizationDefaultError(err error) bool {
	var vi models.ValidationIssue
	return errors.As(err, &vi) && vi.Code() == ErrCodeTaxCodeIsOrganizationDefault
}

// ErrTaxCodeOrphanedKey is returned by GetOrCreateByAppMapping when a key derived from
// the Stripe code already exists but no longer carries that app mapping (the mapping was
// changed after the key was auto-created). The typed error prevents a raw constraint error from
// poisoning the pg transaction (25P02).
var ErrTaxCodeOrphanedKey = errors.New("tax code key exists but app mapping is orphaned")

func IsTaxCodeOrphanedKeyError(err error) bool {
	return errors.Is(err, ErrTaxCodeOrphanedKey)
}