Skip to content

Commit 808db4e

Browse files
committed
Refactor error handling to use constants for required field messages across multiple modules, enhancing maintainability and consistency in exception messages.
1 parent a1b56cd commit 808db4e

File tree

34 files changed

+300
-128
lines changed

34 files changed

+300
-128
lines changed

contentstack_management/_constant.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import platform
2-
import sys
2+
import sys
3+
from ._messages import REQUEST_HEADERS_INVALID
34

45

56
def _default_user_agent():
@@ -55,4 +56,4 @@ def _request_headers():
5556

5657

5758
if __name__ == '__main__':
58-
print(_request_headers().__str__())
59+
print(REQUEST_HEADERS_INVALID)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
"""
2+
Centralized error and UI messages for Contentstack Management SDK.
3+
4+
This module contains all user-facing messages used throughout the SDK
5+
to ensure consistency and easy maintenance.
6+
"""
7+
8+
# General messages
9+
REQUEST_HEADERS_INVALID = "Request headers are invalid. Provide valid headers and try again."
10+
11+
# Alias messages
12+
ALIAS_UID_REQUIRED = "Alias UID is required. Provide a valid Alias UID and try again."
13+
14+
# Asset messages
15+
ASSET_UID_REQUIRED = "Asset UID is required. Provide a valid Asset UID and try again."
16+
ASSET_TYPE_REQUIRED = "Asset Type is required. Provide a valid Asset Type and try again."
17+
ASSET_VERSION_NUMBER_REQUIRED = "Version Number is required. Provide a valid Version Number and try again."
18+
19+
# Audit log messages
20+
LOG_ITEM_UID_REQUIRED = "Log Item UID is required. Provide a valid Log Item UID and try again."
21+
22+
# Branch messages
23+
BRANCH_UID_REQUIRED = "Branch UID is required. Provide a valid Branch UID and try again."
24+
25+
# Bulk operation messages
26+
JOB_UID_REQUIRED = "Job UID is required. Provide a valid Job UID and try again."
27+
28+
# Content type messages
29+
CONTENT_TYPE_UID_REQUIRED = "Content Type UID is required. Provide a valid Content Type UID and try again."
30+
31+
# Delivery token messages
32+
DELIVERY_TOKEN_UID_REQUIRED = "Delivery Token UID is required. Provide a valid Delivery Token UID and try again."
33+
34+
# Entry messages
35+
ENTRY_UID_REQUIRED = "Entry UID is required. Provide a valid Entry UID and try again."
36+
ENTRY_VERSION_NUMBER_REQUIRED = "Version Number is required. Provide a valid Version Number and try again."
37+
ENTRY_BODY_REQUIRED = "Body is required. Provide a valid Body value and try again."
38+
ENTRY_FILE_PATH_REQUIRED = "File Path is required. Provide a valid File Path and try again."
39+
40+
# Entry variant messages
41+
ENTRY_VARIANT_CONTENT_TYPE_UID_REQUIRED = "Content Type UID is required. Provide a valid Content Type UID and try again."
42+
ENTRY_VARIANT_ENTRY_UID_REQUIRED = "Entry UID is required. Provide a valid Entry UID and try again."
43+
ENTRY_VARIANT_UID_REQUIRED = "Variant UID is required. Provide a valid Variant UID and try again."
44+
45+
# Environment messages
46+
ENVIRONMENT_UID_REQUIRED = "Environment UID is required. Provide a valid Environment UID and try again."
47+
48+
# Extension messages
49+
EXTENSION_UID_REQUIRED = "Extension UID is required. Provide a valid Extension UID and try again."
50+
51+
# Global field messages
52+
GLOBAL_FIELD_UID_REQUIRED = "Global Field UID is required. Provide a valid Global Field UID and try again."
53+
54+
# Label messages
55+
LABEL_UID_REQUIRED = "Label UID is required. Provide a valid Label UID and try again."
56+
57+
# Locale messages
58+
LOCALE_CODE_REQUIRED = "Locale Code is required. Provide a valid Locale Code and try again."
59+
60+
# Management token messages
61+
MANAGEMENT_TOKEN_UID_REQUIRED = "Management Token UID is required. Provide a valid Management Token UID and try again."
62+
63+
# Metadata messages
64+
METADATA_UID_REQUIRED = "Metadata UID is required. Provide a valid Metadata UID and try again."
65+
66+
# OAuth messages
67+
OAUTH_ACCESS_TOKEN_EXPIRED = "Access token expired. Refreshing the token."
68+
OAUTH_ACCESS_TOKEN_NOT_AVAILABLE = "Access token is not available. Authenticate to continue."
69+
OAUTH_TOKENS_NOT_AVAILABLE = "OAuth tokens are not available. Please authenticate first."
70+
OAUTH_REFRESH_TOKEN_NOT_AVAILABLE = "Refresh token is not available. Please authenticate to get a new token."
71+
OAUTH_NOT_CONFIGURED = "OAuth is not configured. Set up OAuth to continue."
72+
OAUTH_AUTHORIZATION_CODE_NOT_FOUND = "Authorization code was not found in the redirect URL. Provide a valid code and try again."
73+
OAUTH_TOKEN_EXCHANGE_FAILED = "Token exchange failed. Please try authenticating again."
74+
OAUTH_TOKEN_REFRESH_FAILED = "Token refresh failed due to {error}. Review the error and try again."
75+
OAUTH_BASE_URL_NOT_SET = "OAuth Base URL is not set. Provide a valid OAuth Base URL and try again."
76+
OAUTH_AUTHORIZING = "Authorizing using app ID {app_id} and client ID {client_id}."
77+
OAUTH_AUTHORIZATION_URL_GENERATED = "OAuth handler generated the final authorization URL at {final_url}."
78+
OAUTH_AUTHORIZATION_URL_GENERATION_FAILED = "Authorization URL generation failed due to {error}. Review the error and try again."
79+
OAUTH_AUTHORIZATION_CODE_EMPTY = "Authorization code is required. Provide a valid code and try again."
80+
OAUTH_TOKEN_EXCHANGE_ERROR = "Token exchange failed due to {error}. Review the error and try again."
81+
OAUTH_TOKEN_REFRESH_ERROR = "Token refresh failed due to {error}. Review the error and try again."
82+
OAUTH_TOKEN_REFRESH_FAILED_AFTER_401 = "Token refresh failed after a 401 response due to {error}. Review the error and try again."
83+
OAUTH_VISIT_URL_TO_AUTHORIZE = "Visit {auth_url} to authorize."
84+
85+
# Organization messages
86+
ORGANIZATION_UID_REQUIRED = "Organization UID is required. Provide a valid Organization UID and try again."
87+
88+
# Publish queue messages
89+
PUBLISH_QUEUE_UID_REQUIRED = "Publish Queue UID is required. Provide a valid Publish Queue UID and try again."
90+
91+
# Release messages
92+
RELEASE_UID_REQUIRED = "Release UID is required. Provide a valid Release UID and try again."
93+
94+
# Role messages
95+
ROLE_UID_REQUIRED = "Role UID is required. Provide a valid Release UID and try again."
96+
97+
# Stack messages
98+
API_KEY_REQUIRED = "API Key is required. Provide a valid API Key and try again."
99+
USER_ID_REQUIRED = "User ID is required. Provide a valid User ID and try again."
100+
OWNERSHIP_TOKEN_REQUIRED = "Ownership Token is required. Provide a valid Ownership Token and try again."
101+
102+
# Taxonomy messages
103+
TAXONOMY_UID_REQUIRED = "Taxonomy UID is required. Provide a valid Taxonomy UID and try again."
104+
105+
# Terms messages
106+
TERMS_UID_REQUIRED = "Terms UID is required. Provide a valid Terms UID and try again."
107+
TERM_STRING_REQUIRED = "Term String is required. Provide a valid Term String and try again."
108+
109+
# User session messages
110+
EMAIL_ID_REQUIRED = "Email ID is required. Provide a valid Email ID and try again."
111+
PASSWORD_REQUIRED = "Password is required. Provide a valid Password and try again."
112+
113+
# Variant group messages
114+
VARIANT_GROUP_UID_REQUIRED = "Variant Group UID is required. Provide a valid Variant Group UID and try again."
115+
116+
# Variant messages
117+
VARIANT_UIDS_NON_EMPTY_LIST_REQUIRED = "Variant UIDs must be a non-empty list. Provide at least one Variant UID and try again."
118+
VARIANT_UID_REQUIRED = "Variant UID is required. Provide a valid Variant UID and try again."
119+
120+
# Webhook messages
121+
WEBHOOK_UID_REQUIRED = "Webhook UID is required. Provide a valid Webhook UID and try again."
122+
WEBHOOK_FILE_PATH_REQUIRED = "File Path is required. Provide a valid File Path and try again."
123+
WEBHOOK_EXECUTION_UID_REQUIRED = "Execution UID is required. Provide a valid Execution UID and try again."
124+
125+
# Workflow messages
126+
WORKFLOW_UID_REQUIRED = "Workflow UID is required. Provide a valid Workflow UID and try again."
127+
WORKFLOW_CONTENT_TYPE_UID_REQUIRED = "Content Type UID is required. Provide a valid Content Type UID and try again."
128+
WORKFLOW_ENTRY_UID_REQUIRED = "Entry UID is required. Provide a valid Entry UID and try again."
129+
WORKFLOW_RULE_UID_REQUIRED = "Rule UID is required. Provide a valid Rule UID and try again."
130+
131+

contentstack_management/aliases/aliases.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import json
55
from ..common import Parameter
6+
from .._messages import ALIAS_UID_REQUIRED
67
_path = 'stacks/branch_aliases'
78

89

@@ -47,7 +48,7 @@ def fetch(self):
4748
--------------------------------
4849
"""
4950
if self.alias_uid is None or '':
50-
raise Exception('alias_uid is required')
51+
raise Exception(ALIAS_UID_REQUIRED)
5152
url = f"{_path}/{self.alias_uid}"
5253
return self.client.get(url, headers=self.client.headers, params=self.params)
5354

contentstack_management/assets/assets.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ..common import Parameter
99
import mimetypes
1010
import os
11+
from .._messages import ASSET_UID_REQUIRED, ASSET_TYPE_REQUIRED, ASSET_VERSION_NUMBER_REQUIRED
1112

1213
class Assets(Parameter):
1314
"""
@@ -62,7 +63,7 @@ def fetch(self):
6263
"""
6364

6465
if self.asset_uid is None or '':
65-
raise Exception('asset_uid is required')
66+
raise Exception(ASSET_UID_REQUIRED)
6667
url = f"assets/{self.asset_uid}"
6768
return self.client.get(url, headers = self.client.headers, params = self.params)
6869

@@ -284,7 +285,7 @@ def version(self):
284285
"""
285286

286287
if self.asset_uid is None or '':
287-
raise Exception('asset_uid is required')
288+
raise Exception(ASSET_UID_REQUIRED)
288289
url = f"assets/{self.asset_uid}/versions"
289290
return self.client.get(url, headers = self.client.headers, params = self.params)
290291

@@ -305,9 +306,9 @@ def version_delete(self, version_number):
305306
"""
306307

307308
if self.asset_uid is None or '':
308-
raise Exception('asset_uid is required')
309+
raise Exception(ASSET_UID_REQUIRED)
309310
if version_number is None:
310-
raise Exception('Version Number is required')
311+
raise Exception(ASSET_VERSION_NUMBER_REQUIRED)
311312
url = f"assets/{self.asset_uid}/versions/{version_number}/name"
312313
return self.client.delete(url, headers = self.client.headers, params = self.params)
313314

@@ -326,7 +327,7 @@ def references(self):
326327
"""
327328

328329
if self.asset_uid is None or '':
329-
raise Exception('asset_uid is required')
330+
raise Exception(ASSET_UID_REQUIRED)
330331
url = f"assets/{self.asset_uid}/references"
331332
return self.client.get(url, headers = self.client.headers, params = self.params)
332333

@@ -346,7 +347,7 @@ def specific_asset_type(self, asset_type):
346347
"""
347348

348349
if asset_type is None or '':
349-
raise Exception('asset_type is required')
350+
raise Exception(ASSET_TYPE_REQUIRED)
350351
url = f"assets/{asset_type}"
351352
return self.client.get(url, headers = self.client.headers, params = self.params)
352353

@@ -376,7 +377,7 @@ def update_asset_revision(self, data):
376377

377378
data = json.dumps(data)
378379
if self.asset_uid is None or '':
379-
raise Exception('asset_uid is required')
380+
raise Exception(ASSET_UID_REQUIRED)
380381
url = f"assets/{self.asset_uid}"
381382
return self.client.put(url, headers = self.client.headers, params = self.params, data = data)
382383

@@ -404,7 +405,7 @@ def update(self, data):
404405

405406
data = json.dumps(data)
406407
if self.asset_uid is None or '':
407-
raise Exception('asset_uid is required')
408+
raise Exception(ASSET_UID_REQUIRED)
408409
url = f"assets/{self.asset_uid}"
409410
Parameter.add_header(self, "Content-Type", "multipart/form-data")
410411
return self.client.put(url, headers = self.client.headers, params = self.params, data = data)
@@ -439,7 +440,7 @@ def publish(self, data):
439440

440441
data = json.dumps(data)
441442
if self.asset_uid is None or '':
442-
raise Exception('asset_uid is required')
443+
raise Exception(ASSET_UID_REQUIRED)
443444
url = f"assets/{self.asset_uid}/publish"
444445
return self.client.post(url, headers = self.client.headers, data = data)
445446

@@ -474,7 +475,7 @@ def unpublish(self, data):
474475

475476
data = json.dumps(data)
476477
if self.asset_uid is None or '':
477-
raise Exception('asset_uid is required')
478+
raise Exception(ASSET_UID_REQUIRED)
478479
url = f"assets/{self.asset_uid}/unpublish"
479480
return self.client.post(url, headers = self.client.headers, data = data)
480481

contentstack_management/auditlogs/auditlog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from ..common import Parameter
77
from .._errors import ArgumentException
8+
from .._messages import LOG_ITEM_UID_REQUIRED
89

910
class Auditlog(Parameter):
1011
"""
@@ -58,6 +59,6 @@ def fetch(self):
5859

5960
def validate_uid(self):
6061
if self.log_item_uid is None or '':
61-
raise ArgumentException('Log item Uid is required')
62+
raise ArgumentException(LOG_ITEM_UID_REQUIRED)
6263

6364

contentstack_management/branches/branches.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77

88
from contentstack_management.common import Parameter
9+
from contentstack_management._messages import BRANCH_UID_REQUIRED
910

1011
_path = 'stacks/branches'
1112

@@ -55,7 +56,7 @@ def fetch(self):
5556
--------------------------------
5657
"""
5758
if self.branch_uid is None or '':
58-
raise Exception('branch_uid is required field')
59+
raise Exception(BRANCH_UID_REQUIRED)
5960
url = f"{_path}/{self.branch_uid}"
6061
return self.client.get(url, headers=self.client.headers, params=self.params)
6162

@@ -101,6 +102,6 @@ def delete(self):
101102
--------------------------------
102103
"""
103104
if self.branch_uid is None or '':
104-
raise Exception('branch_uid is required field')
105+
raise Exception(BRANCH_UID_REQUIRED)
105106
url = f"{_path}/{self.branch_uid}"
106107
return self.client.delete(url, headers=self.client.headers, params=self.params)

contentstack_management/bulk_operations/bulk_operation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ..common import Parameter
88
from urllib.parse import quote
99
from .._errors import ArgumentException
10+
from .._messages import JOB_UID_REQUIRED
1011

1112
class BulkOperation(Parameter):
1213
"""
@@ -313,7 +314,7 @@ def job_status(self, job_uid: str, headers: dict = None):
313314
-------------------------------
314315
"""
315316
if job_uid is None:
316-
raise ArgumentException("job_uid", "job_uid cannot be None")
317+
raise ArgumentException(JOB_UID_REQUIRED)
317318
if headers is not None:
318319
self.client.headers.update(headers)
319320
url = f"{self.path}/jobs/{quote(job_uid)}"

contentstack_management/content_types/content_type.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from contentstack_management.common import Parameter
88
from ..entries import entry
9+
from .._messages import CONTENT_TYPE_UID_REQUIRED
910

1011
_path = 'content_types'
1112

@@ -163,7 +164,7 @@ def update(self, data):
163164
"""
164165
data = json.dumps(data)
165166
if self.content_type_uid is None or '':
166-
raise Exception('content_type_uid is required')
167+
raise Exception(CONTENT_TYPE_UID_REQUIRED)
167168
url = f"{_path}/{self.content_type_uid}"
168169
return self.client.put(url, headers=self.client.headers, params=self.params, data=data)
169170

@@ -347,5 +348,5 @@ def imports(self, file_path):
347348

348349
def entry(self, entry_uid: str =None):
349350
if self.content_type_uid is None:
350-
raise Exception('Content type uid is required')
351+
raise Exception(CONTENT_TYPE_UID_REQUIRED)
351352
return entry.Entry(self.client, self.content_type_uid, entry_uid)

contentstack_management/contentstack.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ def oauth(self, app_id: str, client_id: str, redirect_uri: str,
168168
... redirect_uri='http://localhost:3000/callback'
169169
... )
170170
>>> auth_url = oauth_handler.authorize()
171-
>>> print(f"Visit this URL to authorize: {auth_url}")
171+
>>> from contentstack_management._messages import OAUTH_VISIT_URL_TO_AUTHORIZE
172+
>>> print(OAUTH_VISIT_URL_TO_AUTHORIZE.format(auth_url=auth_url))
172173
"""
173174
return OAuthHandler(
174175
app_id=app_id,

contentstack_management/delivery_token/delivery_token.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
from ..common import Parameter
88
from .._errors import ArgumentException
9+
from .._messages import DELIVERY_TOKEN_UID_REQUIRED
910

1011
class DeliveryToken(Parameter):
1112
"""
@@ -191,4 +192,4 @@ def delete(self):
191192

192193
def validate_uid(self):
193194
if self.delivery_token_uid is None or '':
194-
raise ArgumentException("Delivery Token Uid is required")
195+
raise ArgumentException(DELIVERY_TOKEN_UID_REQUIRED)

0 commit comments

Comments
 (0)