From 2934facd94695b4e46aac3811eb6f3c890952f1c Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Tue, 2 Sep 2025 11:06:54 +0530 Subject: [PATCH 1/4] Refactor error handling in BaseQuery and helper functions to use centralized ErrorMessages for consistency and clarity. --- src/Error/ErrorMessages.php | 50 +++++++++++++++++++++++++++++++++++++ src/Stack/BaseQuery.php | 15 +++++------ src/Support/helper.php | 31 ++++++++++++----------- 3 files changed, 74 insertions(+), 22 deletions(-) create mode 100644 src/Error/ErrorMessages.php diff --git a/src/Error/ErrorMessages.php b/src/Error/ErrorMessages.php new file mode 100644 index 00000000..27f5b71d --- /dev/null +++ b/src/Error/ErrorMessages.php @@ -0,0 +1,50 @@ + + * @license https://github.com/contentstack/contentstack-php/blob/master/LICENSE.txt MIT Licence + * @link https://www.contentstack.com/docs/platforms/php/ + */ +class ErrorMessages +{ + // BaseQuery.php error messages + const FIELD_UIDS_ARRAY = 'Field UIDs must be an array. Convert the value to an array and try again.'; + const TAGS_ARRAY = 'Tags must be an array. Convert the value to an array and try again.'; + const VALUE_ARRAY = 'Value must be an array. Convert the value to an array and try again.'; + const INVALID_QUERY = 'Invalid query. Update the query and try again.'; + + // helper.php error messages + const INVALID_STRING_INPUT = 'Invalid input for "%s". Use a string value and try again.'; + const INVALID_INCLUDE_REFERENCES = 'Invalid input for includeReferences. Use an array and try again.'; + const INVALID_INPUT_TYPE = 'Invalid input. Use a string or an array and try again.'; + const INVALID_REGEX_KEY_VALUE = 'Invalid input for regex. Use a string for the key and a valid regular expression for the value.'; + const INVALID_REGEX_OPTIONS = 'Invalid regex options. Provide valid options and try again.'; + const INVALID_REGEX_ARGS = 'Invalid input for regex. Provide 2 or 3 arguments and try again.'; + const INVALID_TAGS_INPUT = 'Invalid input for tags. Use a valid array of tags and try again.'; + const INVALID_KEY_VALUE = 'Invalid input for "%s". Use a string for the key and a valid value, then try again.'; + const INVALID_QUERY_INPUT = 'Invalid input for "%s". Provide at least one query and try again.'; + const INVALID_QUERY_OBJECTS = 'Invalid input. Query objects are expected as arguments. Update the input and try again.'; + const INVALID_KEY_ARRAY_VALUE = 'Invalid input for "%s". Use a string for the key and an array for the value, then try again.'; + const INVALID_NUMERIC_INPUT = 'Invalid input for "%s". Use a numeric value and try again.'; + const INVALID_FIELD_INPUT = 'Invalid input for "%s". Use a valid field from the entry and try again.'; + const INVALID_FIELD_UID = 'Invalid input for "%s". Use a valid string field UID and try again.'; + + /** + * Format error message with function name + * + * @param string $message The message template containing %s placeholder + * @param string $functionName The function name to insert + * + * @return string Formatted error message + */ + public static function formatMessage($message, $functionName = '') + { + return sprintf($message, $functionName); + } +} diff --git a/src/Stack/BaseQuery.php b/src/Stack/BaseQuery.php index b63d5b66..039eeaa0 100755 --- a/src/Stack/BaseQuery.php +++ b/src/Stack/BaseQuery.php @@ -16,6 +16,7 @@ namespace Contentstack\Stack; use Contentstack\Support\Utility; +use Contentstack\Error\ErrorMessages; require_once __DIR__ . "/../Support/helper.php"; @@ -110,7 +111,7 @@ public function except($level = 'BASE', $field_uids = array()) ); return $this->queryObject; } - throw contentstackCreateError('field_uids must be an array'); + throw contentstackCreateError(ErrorMessages::FIELD_UIDS_ARRAY); } /** @@ -142,7 +143,7 @@ public function only($level = 'BASE', $field_uids = array()) ); return $this->queryObject; } - throw contentstackCreateError('field_uids must be an array'); + throw contentstackCreateError(ErrorMessages::FIELD_UIDS_ARRAY); } /** @@ -175,7 +176,7 @@ public function includeReference($field_uids = array()) ); return $this->queryObject; } - throw contentstackCreateError('field_uids must be an array'); + throw contentstackCreateError(ErrorMessages::FIELD_UIDS_ARRAY); } /** @@ -710,7 +711,7 @@ public function tags($tags = array()) ); return $this->queryObject; } - throw contentstackCreateError('tags must be an array'); + throw contentstackCreateError(ErrorMessages::TAGS_ARRAY); } /** @@ -773,7 +774,7 @@ public function containedIn($field = '', $value = array()) ); return $this->queryObject; } - throw contentstackCreateError('value must be an array'); + throw contentstackCreateError(ErrorMessages::VALUE_ARRAY); } /** @@ -809,7 +810,7 @@ public function notContainedIn($field = '', $value = array()) ); return $this->queryObject; } - throw contentstackCreateError('value must be an array'); + throw contentstackCreateError(ErrorMessages::VALUE_ARRAY); } /** @@ -990,7 +991,7 @@ public function addQuery($_query = array()) $this->subQuery = $_query; return $this->queryObject; } - throw contentstackCreateError("Provide valid query"); + throw contentstackCreateError(ErrorMessages::INVALID_QUERY); } /** diff --git a/src/Support/helper.php b/src/Support/helper.php index 04a08384..6b32e121 100755 --- a/src/Support/helper.php +++ b/src/Support/helper.php @@ -1,6 +1,7 @@ 0)) { - throw contentstackCreateError('Invalid options for regex. Please provide the valid options'); + throw contentstackCreateError(ErrorMessages::INVALID_REGEX_OPTIONS); } $query[$values[0]] = array($operator => $values[1]); if(isset($values[2])) $query[$values[0]]['$options'] = $values[2]; return $query; } else { - throw contentstackCreateError('Invalid input for regex. At least 2 or maximum 3 arguments are required.'); + throw contentstackCreateError(ErrorMessages::INVALID_REGEX_ARGS); } } } @@ -126,7 +127,7 @@ function contentstackRegexp($operator = '', $query = array(), $values = array()) * */ function contentstackTags($operator = '', $query = array(), $value = '') { if(!(is_array($value) && count($value) > 0)) - throw contentstackCreateError('Invalid input for tags.Value must be valid array of tags'); + throw contentstackCreateError(ErrorMessages::INVALID_TAGS_INPUT); $query[$operator] = $value; return $query; } @@ -146,7 +147,7 @@ function contentstackTags($operator = '', $query = array(), $value = '') { * */ function contentstackComparision($operator = '', $query = array(), $key = '', $value = '') { if(!(!Utility::isEmpty($key) && is_string($key) && !Utility::isEmpty($value))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Key must be string and value should be valid not empty.'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_KEY_VALUE, contentstackGetFunctionName())); $query[$key] = array($operator => $value); return $query; } @@ -165,7 +166,7 @@ function contentstackComparision($operator = '', $query = array(), $key = '', $v * */ function contentstackLogical($operator = '', $query = array(), $value = array()) { if(!(is_array($value) && count($value) > 0)) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". At least one Query or array object is expected'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_QUERY_INPUT, contentstackGetFunctionName())); foreach($value as $key => $_qry) { if(!Utility::isKeySet($query, $operator)) $query[$operator] = array(); if($_qry instanceof \Contentstack\Stack\BaseQuery) @@ -174,7 +175,7 @@ function contentstackLogical($operator = '', $query = array(), $value = array()) array_push($query[$operator], $_qry); else { unset($query[$operator]); - throw contentstackCreateError('Query objects are expected as arguments'); + throw contentstackCreateError(ErrorMessages::INVALID_QUERY_OBJECTS); } } return $query; @@ -194,7 +195,7 @@ function contentstackLogical($operator = '', $query = array(), $value = array()) * */ function contentstackContains($operator = '', $query = array(), $key = '', $value = array()) { if (!(!Utility::isEmpty($key) && is_string($key) && is_array($value))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Key should be string and value must be array.'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_KEY_ARRAY_VALUE, contentstackGetFunctionName())); $query[$key] = array($operator => $value); return $query; } @@ -212,7 +213,7 @@ function contentstackContains($operator = '', $query = array(), $key = '', $valu * */ function contentstackPagination($operator = '', $query = array(), $value = '') { if (!(!Utility::isEmpty($value) && is_numeric($value))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'", it should be Numeric.'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_NUMERIC_INPUT, contentstackGetFunctionName())); $query[$operator] = $value; return $query; } @@ -231,7 +232,7 @@ function contentstackPagination($operator = '', $query = array(), $value = '') { function contentstackLanguage($operator = '', $query = array(), $value = '') { if (!(!Utility::isEmpty($value) && is_string($value))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'", it should be String.'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_STRING_INPUT, contentstackGetFunctionName())); $query[$operator] = $value; return $query; } @@ -249,7 +250,7 @@ function contentstackLanguage($operator = '', $query = array(), $value = '') { * */ function contentstackSorting($operator = '', $query = array(), $key = '') { if (!(!Utility::isEmpty($key) && is_string($key))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Value should be valid field in entry'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_FIELD_INPUT, contentstackGetFunctionName())); $query[$operator] = $key; return $query; } @@ -300,7 +301,7 @@ function contentstackAddParam($key = '', $query = array(), $value = '') { * */ function contentstackExistence($operator = '', $query = array(), $key = '', $value = false) { if (!(!Utility::isEmpty($key) && is_string($key))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Key should be valid String field uid'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_FIELD_UID, contentstackGetFunctionName())); $query[$key] = array($operator => $value); return $query; } From 3c697169e215ff100c806f478878a6d4730d2a63 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Fri, 7 Nov 2025 14:53:04 +0530 Subject: [PATCH 2/4] version bump --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28097c85..019670fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ ## CHANGELOG +------------------------------------------------ +## Version 2.4.1 +###### Date: 10-November-2025 +### Enhancement + - Improved Error messages ------------------------------------------------ ## Version 2.4.0 ###### Date: 13-May-2024 From e2968c837b9e15db8278d3cd178b9fe193de9cf5 Mon Sep 17 00:00:00 2001 From: harshithad0703 <104908717+harshithad0703@users.noreply.github.com> Date: Mon, 10 Nov 2025 11:32:50 +0530 Subject: [PATCH 3/4] Update copyright year in LICENSE file Updated copyright year from 2024 to 2025. --- LICENSE.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE.txt b/LICENSE.txt index 30b216ff..feb70b81 100755 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2012-2024 Contentstack (http://app.contentstack.com). All Rights Reserved +Copyright (c) 2012-2025 Contentstack (http://app.contentstack.com). All Rights Reserved Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 55555657b0c77adc8a1b5c8ce848050a6f45e9b4 Mon Sep 17 00:00:00 2001 From: harshithad0703 <104908717+harshithad0703@users.noreply.github.com> Date: Mon, 10 Nov 2025 11:54:12 +0530 Subject: [PATCH 4/4] Update branch restrictions from 'next' to 'staging' --- .github/workflows/check-branch.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/check-branch.yml b/.github/workflows/check-branch.yml index 1e2d24a5..2332f0d0 100644 --- a/.github/workflows/check-branch.yml +++ b/.github/workflows/check-branch.yml @@ -8,13 +8,13 @@ jobs: runs-on: ubuntu-latest steps: - name: Comment PR - if: github.base_ref == 'master' && github.head_ref != 'next' + if: github.base_ref == 'master' && github.head_ref != 'staging' uses: thollander/actions-comment-pull-request@v2 with: message: | - We regret to inform you that you are currently not able to merge your changes into the master branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the next branch. Our team will then review the changes and work with you to ensure a successful merge into the master branch. + We regret to inform you that you are currently not able to merge your changes into the master branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the staging branch. Our team will then review the changes and work with you to ensure a successful merge into the master branch. - name: Check branch - if: github.base_ref == 'master' && github.head_ref != 'next' + if: github.base_ref == 'master' && github.head_ref != 'staging' run: | - echo "ERROR: We regret to inform you that you are currently not able to merge your changes into the master branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the next branch. Our team will then review the changes and work with you to ensure a successful merge into the master branch." - exit 1 \ No newline at end of file + echo "ERROR: We regret to inform you that you are currently not able to merge your changes into the master branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the staging branch. Our team will then review the changes and work with you to ensure a successful merge into the master branch." + exit 1