Skip to content

Commit b4c5d29

Browse files
committed
feat: Introduce centralized error messages for improved consistency and maintainability across the codebase
1 parent b36db03 commit b4c5d29

26 files changed

+156
-58
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.contentstack.cms.core;
2+
3+
public final class ErrorMessages {
4+
5+
private ErrorMessages() {
6+
throw new AssertionError("This class cannot be instantiated. Use the static constants to continue.");
7+
}
8+
9+
public static final String NOT_LOGGED_IN = "You are not logged in. Login to continue.";
10+
public static final String ALREADY_LOGGED_IN = "Operation not allowed. You are already logged in.";
11+
public static final String OAUTH_LOGIN_REQUIRED = "Login or configure OAuth to continue.";
12+
public static final String OAUTH_NO_TOKENS = "No OAuth tokens available. Please authenticate first.";
13+
public static final String OAUTH_NO_REFRESH_TOKEN = "No refresh token available";
14+
public static final String OAUTH_EMPTY_CODE = "Authorization code cannot be null or empty";
15+
public static final String OAUTH_CONFIG_MISSING = "OAuth is not configured. Use Builder.setOAuth() with or without clientSecret for PKCE flow";
16+
public static final String OAUTH_REFRESH_FAILED = "Failed to refresh access token";
17+
public static final String OAUTH_REVOKE_FAILED = "Failed to revoke authorization";
18+
public static final String OAUTH_STATUS_FAILED = "Failed to get authorization status";
19+
public static final String OAUTH_ORG_EMPTY = "organizationUid can not be empty";
20+
21+
public static final String PRIVATE_CONSTRUCTOR = "This class cannot be instantiated. Use the static methods to continue.";
22+
public static final String PRIVATE_CONSTRUCTOR_DETAILED = "This private constructor cannot be accessed outside the class. Use the public methods instead.";
23+
24+
public static final String ASSET_UID_REQUIRED = "Asset UID is required. Provide a valid Asset UID and try again.";
25+
public static final String LOG_ITEM_UID_REQUIRED = "Log Item UID is required. Provide a valid Log Item UID and try again.";
26+
public static final String BRANCH_UID_REQUIRED = "Branch UID is required. Provide a valid Branch UID and try again.";
27+
public static final String CUSTOM_FIELD_UID_REQUIRED = "Custom Field UID is required. Provide a valid Custom Field UID and try again.";
28+
public static final String DELIVERY_TOKEN_UID_REQUIRED = "Delivery Token UID is required. Provide a valid Delivery Token UID and try again.";
29+
public static final String ENVIRONMENT_REQUIRED = "Environment is required. Provide a valid Environment and try again.";
30+
public static final String FOLDER_UID_REQUIRED = "Folder UID is required. Provide a valid Folder UID and try again.";
31+
public static final String GLOBAL_FIELD_UID_REQUIRED = "Global Field UID is required. Provide a valid Global Field UID and try again.";
32+
public static final String LABEL_UID_REQUIRED = "Label UID is required. Provide a valid Label UID and try again.";
33+
public static final String LOCALE_CODE_REQUIRED = "Locale Code is required. Provide a valid Locale Code and try again.";
34+
public static final String MANAGEMENT_TOKEN_UID_REQUIRED = "Management Token UID is required. Provide a valid Management Token UID and try again.";
35+
public static final String ORGANIZATION_UID_REQUIRED = "Organization UID is required. Provide a valid Organization UID and try again.";
36+
public static final String PUBLISH_QUEUE_UID_REQUIRED = "Publish Queue UID is required. Provide a valid Publish Queue UID and try again.";
37+
public static final String RELEASE_UID_REQUIRED = "Release UID is required. Provide a valid Release UID and try again.";
38+
public static final String ROLE_UID_REQUIRED = "Role UID is required. Provide a valid Role UID and try again.";
39+
public static final String VARIANT_GROUP_UID_REQUIRED = "Variant Group UID is required. Provide a valid Variant Group UID and try again.";
40+
public static final String WEBHOOK_UID_REQUIRED = "Webhook UID is required. Provide a valid Webhook UID and try again.";
41+
public static final String WORKFLOW_UID_REQUIRED = "Workflow UID is required. Provide a valid Workflow UID and try again.";
42+
43+
public static final String CONTENT_TYPE_REQUIRED = "Content Type is required. Provide a valid Content Type and try again.";
44+
public static final String REFERENCE_FIELDS_INVALID = "Reference Fields must be a string or an array of strings. Provide valid values and try again.";
45+
public static final String TERM_STRING_REQUIRED = "Term String is required. Provide a valid Term String and try again.";
46+
47+
public static final String FILE_CONTENT_TYPE_UNKNOWN = "The file's content type could not be determined. Provide a valid file and try again.";
48+
49+
public static final String MISSING_INSTALLATION_ID = "installation uid is required";
50+
public static final String MISSING_ORG_ID = "organization uid is required";
51+
}

src/main/java/com/contentstack/cms/core/Util.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@ public class Util {
2020
// "1.2.0".
2121
public static final String SDK_VERSION = "1.3.1";
2222

23-
static final String PRIVATE_CONSTRUCTOR = "private constructor can't be accessed outside the class";
23+
static final String PRIVATE_CONSTRUCTOR = ErrorMessages.PRIVATE_CONSTRUCTOR;
2424
public static final Boolean RETRY_ON_FAILURE = true;
2525
public static final String PROTOCOL = "https";
2626
public static final String HOST = "api.contentstack.io";
2727
public static final String PORT = "443";
2828
public static final String VERSION = "v3";
2929
public static final int TIMEOUT = 30;
3030
public static final String SDK_NAME = "contentstack-management-java";
31-
public static final String ILLEGAL_USER = "Please Login to access stack instance";
32-
public static final String USER_ALREADY_LOGGED_IN = "User is already loggedIn, "
33-
+ "Please logout then try to login again";
34-
public static final String LOGIN_FLAG = "Please login to access user instance";
35-
public static final String PLEASE_LOGIN = "Please Login to access stack instance";
31+
public static final String ILLEGAL_USER = ErrorMessages.NOT_LOGGED_IN;
32+
public static final String USER_ALREADY_LOGGED_IN = ErrorMessages.ALREADY_LOGGED_IN;
33+
public static final String LOGIN_FLAG = ErrorMessages.NOT_LOGGED_IN;
34+
public static final String PLEASE_LOGIN = ErrorMessages.NOT_LOGGED_IN;
3635

3736
// CONSTANT KEYS
3837
public static final String API_KEY = "api_key";
@@ -47,9 +46,9 @@ public class Util {
4746
public static final String CONTENT_TYPE_VALUE = "application/json";
4847

4948
// Error Messages
50-
public static final String MISSING_INSTALLATION_ID = "installation uid is required";
51-
public static final String ERROR_INSTALLATION = "installation uid is required";
52-
public static final String MISSING_ORG_ID = "organization uid is required";
49+
public static final String MISSING_INSTALLATION_ID = ErrorMessages.MISSING_INSTALLATION_ID;
50+
public static final String ERROR_INSTALLATION = ErrorMessages.MISSING_INSTALLATION_ID;
51+
public static final String MISSING_ORG_ID = ErrorMessages.MISSING_ORG_ID;
5352

5453
// OAuth Constants
5554
public static final String OAUTH_APP_HOST = "app.contentstack.com";
@@ -58,15 +57,15 @@ public class Util {
5857
public static final String OAUTH_AUTHORIZE_ENDPOINT = "/#!/apps/%s/authorize";
5958

6059
// OAuth Error Messages
61-
public static final String OAUTH_NO_TOKENS = "No OAuth tokens available. Please authenticate first.";
62-
public static final String OAUTH_NO_REFRESH_TOKEN = "No refresh token available";
63-
public static final String OAUTH_EMPTY_CODE = "Authorization code cannot be null or empty";
64-
public static final String OAUTH_CONFIG_MISSING = "OAuth is not configured. Use Builder.setOAuth() with or without clientSecret for PKCE flow";
65-
public static final String OAUTH_REFRESH_FAILED = "Failed to refresh access token";
66-
public static final String OAUTH_REVOKE_FAILED = "Failed to revoke authorization";
67-
public static final String OAUTH_STATUS_FAILED = "Failed to get authorization status";
68-
public static final String OAUTH_LOGIN_REQUIRED = "Please login or configure OAuth to access";
69-
public static final String OAUTH_ORG_EMPTY = "organizationUid can not be empty";
60+
public static final String OAUTH_NO_TOKENS = ErrorMessages.OAUTH_NO_TOKENS;
61+
public static final String OAUTH_NO_REFRESH_TOKEN = ErrorMessages.OAUTH_NO_REFRESH_TOKEN;
62+
public static final String OAUTH_EMPTY_CODE = ErrorMessages.OAUTH_EMPTY_CODE;
63+
public static final String OAUTH_CONFIG_MISSING = ErrorMessages.OAUTH_CONFIG_MISSING;
64+
public static final String OAUTH_REFRESH_FAILED = ErrorMessages.OAUTH_REFRESH_FAILED;
65+
public static final String OAUTH_REVOKE_FAILED = ErrorMessages.OAUTH_REVOKE_FAILED;
66+
public static final String OAUTH_STATUS_FAILED = ErrorMessages.OAUTH_STATUS_FAILED;
67+
public static final String OAUTH_LOGIN_REQUIRED = ErrorMessages.OAUTH_LOGIN_REQUIRED;
68+
public static final String OAUTH_ORG_EMPTY = ErrorMessages.OAUTH_ORG_EMPTY;
7069

7170
// The code `Util() throws IllegalAccessException` is a constructor for the
7271
// `Util` class that throws an
@@ -76,7 +75,7 @@ public class Util {
7675
// `IllegalAccessException` is to prevent
7776
// the instantiation of the `Util` class from outside the class itself.
7877
Util() throws IllegalAccessException {
79-
throw new IllegalAccessException("private=modifier");
78+
throw new IllegalAccessException(ErrorMessages.PRIVATE_CONSTRUCTOR_DETAILED);
8079
}
8180

8281
/**
@@ -109,7 +108,7 @@ public static void nullEmptyThrowsException(@NotNull String field) {
109108
try {
110109
throw new CMARuntimeException(field + " cannot take in an empty String or null value");
111110
} catch (CMARuntimeException e) {
112-
System.out.println("Exception: " + e.getLocalizedMessage());
111+
System.out.println("An error occurred due to " + e.getLocalizedMessage() + ".");
113112
}
114113
}
115114

src/main/java/com/contentstack/cms/organization/Organization.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.contentstack.cms.organization;
22

3+
import com.contentstack.cms.core.ErrorMessages;
4+
35
import com.contentstack.cms.BaseImplementation;
46
import okhttp3.ResponseBody;
57
import org.jetbrains.annotations.NotNull;
@@ -26,7 +28,7 @@ public class Organization implements BaseImplementation<Organization> {
2628
protected HashMap<String, String> headers;
2729
protected HashMap<String, Object> params;
2830
private String organizationUid;
29-
final String ERROR_MSG = "OrganizationUid Can Not Be Null OR Empty";
31+
final String ERROR_MSG = ErrorMessages.ORGANIZATION_UID_REQUIRED;
3032

3133

3234
/**

src/main/java/com/contentstack/cms/stack/Alias.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.contentstack.cms.stack;
22

3+
import com.contentstack.cms.core.ErrorMessages;
4+
35
import com.contentstack.cms.BaseImplementation;
46
import okhttp3.ResponseBody;
57
import org.jetbrains.annotations.NotNull;
@@ -160,7 +162,7 @@ public Call<ResponseBody> find() {
160162
* @since 2022-10-20
161163
*/
162164
public Call<ResponseBody> fetch() {
163-
Objects.requireNonNull(this.uid, "Global Field Uid can not be null or empty");
165+
Objects.requireNonNull(this.uid, "ErrorMessages.GLOBAL_FIELD_UID_REQUIRED");
164166
return this.service.single(this.headers, this.uid);
165167
}
166168

@@ -206,7 +208,7 @@ public Call<ResponseBody> update(@NotNull JSONObject body) {
206208
* @since 2022-10-20
207209
*/
208210
public Call<ResponseBody> delete() {
209-
Objects.requireNonNull(this.uid, "Global Field Uid can not be null or empty");
211+
Objects.requireNonNull(this.uid, "ErrorMessages.GLOBAL_FIELD_UID_REQUIRED");
210212
return this.service.delete(this.headers, this.uid, this.params);
211213
}
212214

src/main/java/com/contentstack/cms/stack/Asset.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.contentstack.cms.stack;
22

3+
import com.contentstack.cms.core.ErrorMessages;
4+
35
import com.contentstack.cms.BaseImplementation;
46
import okhttp3.MediaType;
57
import okhttp3.MultipartBody;
@@ -225,12 +227,12 @@ public Call<AssetListResponse> findAsPojo() {
225227
* @since 2022-10-20
226228
*/
227229
public Call<ResponseBody> fetch() {
228-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
230+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
229231
return this.service.single(this.headers, this.assetUid, this.params);
230232
}
231233

232234
public Call<AssetResponse> fetchAsPojo() { // New method for POJO conversion
233-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
235+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
234236
return this.service.singlePojo(this.headers, this.assetUid, this.params);
235237
}
236238

@@ -390,7 +392,7 @@ private MultipartBody createMultipartBody(String filePath, String parentUid, Str
390392
* @since 2022-10-20
391393
*/
392394
public Call<ResponseBody> replace(@NotNull String filePath, @NotNull String description) {
393-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
395+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
394396
MultipartBody.Part assetPath = uploadFile(filePath);
395397
RequestBody body = RequestBody.create(Objects.requireNonNull(MediaType.parse(String.valueOf(MultipartBody.FORM))), description);
396398
return this.service.replace(this.headers, this.assetUid, assetPath, body, this.params);
@@ -443,7 +445,7 @@ private MultipartBody.Part uploadFile(@NotNull String filePath) {
443445
* @since 2022-10-20
444446
*/
445447
public Call<ResponseBody> generatePermanentUrl(JSONObject body) {
446-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
448+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
447449
return this.service.generatePermanentUrl(this.headers, this.assetUid, body);
448450
}
449451

@@ -473,7 +475,7 @@ public Call<ResponseBody> generatePermanentUrl(JSONObject body) {
473475
* @since 2022-10-20
474476
*/
475477
public Call<ResponseBody> getPermanentUrl(String slugUrl) {
476-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
478+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
477479
return this.service.downloadPermanentUrl(this.headers, this.assetUid, slugUrl, this.params);
478480
}
479481

@@ -490,7 +492,7 @@ public Call<ResponseBody> getPermanentUrl(String slugUrl) {
490492
* @since 0.1.0
491493
*/
492494
public Call<ResponseBody> delete() {
493-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
495+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
494496
return this.service.delete(this.headers, this.assetUid);
495497
}
496498

@@ -580,7 +582,7 @@ public Call<ResponseBody> setVersionName(int versionNumber,
580582
* @since 0.1.0
581583
*/
582584
public Call<ResponseBody> getVersionNameDetails() {
583-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
585+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
584586
return this.service.getVersionNameDetails(this.headers, this.assetUid, this.params);
585587
}
586588

@@ -601,7 +603,7 @@ public Call<ResponseBody> getVersionNameDetails() {
601603
* @since 0.1.0
602604
*/
603605
public Call<ResponseBody> deleteVersionName(int versionNumber) {
604-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
606+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
605607
return this.service.deleteVersionName(this.headers, this.assetUid, versionNumber);
606608
}
607609

@@ -618,7 +620,7 @@ public Call<ResponseBody> deleteVersionName(int versionNumber) {
618620
* @since 0.1.0
619621
*/
620622
public Call<ResponseBody> getReferences() {
621-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
623+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
622624
return this.service.getReferences(this.headers, this.assetUid);
623625
}
624626

@@ -676,7 +678,7 @@ public Call<ResponseBody> getByType(@NotNull String assetType) {
676678
* @since 0.1.0
677679
*/
678680
public Call<ResponseBody> updateDetails(JSONObject requestBody) {
679-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
681+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
680682
return this.service.updateDetails(this.headers, this.assetUid, this.params, requestBody);
681683
}
682684

@@ -702,7 +704,7 @@ public Call<ResponseBody> updateDetails(JSONObject requestBody) {
702704
* @since 0.1.0
703705
*/
704706
public Call<ResponseBody> publish(@NotNull JSONObject requestBody) {
705-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
707+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
706708
return this.service.publish(this.headers, this.assetUid, requestBody);
707709
}
708710

@@ -728,7 +730,7 @@ public Call<ResponseBody> publish(@NotNull JSONObject requestBody) {
728730
*/
729731
public Call<ResponseBody> unpublish(
730732
@NotNull JSONObject requestBody) {
731-
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
733+
Objects.requireNonNull(this.assetUid, "ErrorMessages.ASSET_UID_REQUIRED");
732734
return this.service.unpublish(this.headers, this.assetUid, requestBody);
733735
}
734736

src/main/java/com/contentstack/cms/stack/AuditLog.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.contentstack.cms.stack;
22

3+
import com.contentstack.cms.core.ErrorMessages;
4+
35
import com.contentstack.cms.BaseImplementation;
46
import okhttp3.ResponseBody;
57
import org.jetbrains.annotations.NotNull;
@@ -112,7 +114,7 @@ public Call<ResponseBody> find() {
112114
* @return Call
113115
*/
114116
public Call<ResponseBody> fetch() {
115-
Objects.requireNonNull(this.logItemUid, "Log Item uid can not be null or empty");
117+
Objects.requireNonNull(this.logItemUid, "ErrorMessages.LOG_ITEM_UID_REQUIRED");
116118
return this.service.fetch(this.headers, this.logItemUid);
117119
}
118120

src/main/java/com/contentstack/cms/stack/Branch.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.contentstack.cms.stack;
22

3+
import com.contentstack.cms.core.ErrorMessages;
4+
35
import com.contentstack.cms.BaseImplementation;
46
import okhttp3.ResponseBody;
57
import org.jetbrains.annotations.NotNull;
@@ -53,7 +55,7 @@ protected Branch(Retrofit instance,Map<String, Object> headers, String uid) {
5355
}
5456

5557
void validate() {
56-
final String ERROR_MESSAGE = "The Branch UID Can Not Be Null ORr Empty";
58+
final String ERROR_MESSAGE = ErrorMessages.BRANCH_UID_REQUIRED;
5759
Objects.requireNonNull(this.baseBranchId, ERROR_MESSAGE);
5860
if (this.baseBranchId.isEmpty())
5961
throw new IllegalStateException(ERROR_MESSAGE);

src/main/java/com/contentstack/cms/stack/DeliveryToken.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.contentstack.cms.stack;
22

3+
import com.contentstack.cms.core.ErrorMessages;
4+
35
import com.contentstack.cms.BaseImplementation;
46
import okhttp3.ResponseBody;
57
import org.jetbrains.annotations.NotNull;
@@ -30,7 +32,7 @@ public class DeliveryToken implements BaseImplementation<DeliveryToken> {
3032
protected HashMap<String, Object> headers;
3133
protected HashMap<String, Object> params;
3234
private String tokenUid;
33-
String ERROR = "Token UID Can Not Be Null OR Empty";
35+
String ERROR = ErrorMessages.DELIVERY_TOKEN_UID_REQUIRED;
3436

3537
protected DeliveryToken(TokenService service, Map<String, Object> headers) {
3638
this.headers = new HashMap<>();

src/main/java/com/contentstack/cms/stack/Entry.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.contentstack.cms.stack;
22

3+
import com.contentstack.cms.core.ErrorMessages;
4+
35
import com.contentstack.cms.BaseImplementation;
46
import okhttp3.ResponseBody;
57
import org.jetbrains.annotations.NotNull;
@@ -145,7 +147,7 @@ public Call<ResponseBody> includeReference(@NotNull Object referenceField){
145147
if (referenceField instanceof String || referenceField instanceof String[]) {
146148
addToParams("include[]", referenceField);
147149
} else {
148-
throw new IllegalArgumentException("Reference fields must be a String or an array of Strings");
150+
throw new IllegalArgumentException(ErrorMessages.REFERENCE_FIELDS_INVALID);
149151
}
150152
validateCT();
151153
return this.service.fetch(this.headers, this.contentTypeUid, this.params);

0 commit comments

Comments
 (0)