Skip to content

Commit 9b2323e

Browse files
Merge pull request #50 from contentstack/fix/DX-3455-error-message-improve
Error messages updates
2 parents 787ef36 + 3c69716 commit 9b2323e

File tree

4 files changed

+79
-22
lines changed

4 files changed

+79
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
## CHANGELOG
33

4+
------------------------------------------------
5+
## Version 2.4.1
6+
###### Date: 10-November-2025
7+
### Enhancement
8+
- Improved Error messages
49
------------------------------------------------
510
## Version 2.4.0
611
###### Date: 13-May-2024

src/Error/ErrorMessages.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace Contentstack\Error;
3+
4+
/**
5+
* Class ErrorMessages
6+
* Contains all error messages used across the SDK
7+
*
8+
* @category PHP
9+
* @package Contentstack
10+
* @author Contentstack <[email protected]>
11+
* @license https://github.com/contentstack/contentstack-php/blob/master/LICENSE.txt MIT Licence
12+
* @link https://www.contentstack.com/docs/platforms/php/
13+
*/
14+
class ErrorMessages
15+
{
16+
// BaseQuery.php error messages
17+
const FIELD_UIDS_ARRAY = 'Field UIDs must be an array. Convert the value to an array and try again.';
18+
const TAGS_ARRAY = 'Tags must be an array. Convert the value to an array and try again.';
19+
const VALUE_ARRAY = 'Value must be an array. Convert the value to an array and try again.';
20+
const INVALID_QUERY = 'Invalid query. Update the query and try again.';
21+
22+
// helper.php error messages
23+
const INVALID_STRING_INPUT = 'Invalid input for "%s". Use a string value and try again.';
24+
const INVALID_INCLUDE_REFERENCES = 'Invalid input for includeReferences. Use an array and try again.';
25+
const INVALID_INPUT_TYPE = 'Invalid input. Use a string or an array and try again.';
26+
const INVALID_REGEX_KEY_VALUE = 'Invalid input for regex. Use a string for the key and a valid regular expression for the value.';
27+
const INVALID_REGEX_OPTIONS = 'Invalid regex options. Provide valid options and try again.';
28+
const INVALID_REGEX_ARGS = 'Invalid input for regex. Provide 2 or 3 arguments and try again.';
29+
const INVALID_TAGS_INPUT = 'Invalid input for tags. Use a valid array of tags and try again.';
30+
const INVALID_KEY_VALUE = 'Invalid input for "%s". Use a string for the key and a valid value, then try again.';
31+
const INVALID_QUERY_INPUT = 'Invalid input for "%s". Provide at least one query and try again.';
32+
const INVALID_QUERY_OBJECTS = 'Invalid input. Query objects are expected as arguments. Update the input and try again.';
33+
const INVALID_KEY_ARRAY_VALUE = 'Invalid input for "%s". Use a string for the key and an array for the value, then try again.';
34+
const INVALID_NUMERIC_INPUT = 'Invalid input for "%s". Use a numeric value and try again.';
35+
const INVALID_FIELD_INPUT = 'Invalid input for "%s". Use a valid field from the entry and try again.';
36+
const INVALID_FIELD_UID = 'Invalid input for "%s". Use a valid string field UID and try again.';
37+
38+
/**
39+
* Format error message with function name
40+
*
41+
* @param string $message The message template containing %s placeholder
42+
* @param string $functionName The function name to insert
43+
*
44+
* @return string Formatted error message
45+
*/
46+
public static function formatMessage($message, $functionName = '')
47+
{
48+
return sprintf($message, $functionName);
49+
}
50+
}

src/Stack/BaseQuery.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace Contentstack\Stack;
1818
use Contentstack\Support\Utility;
19+
use Contentstack\Error\ErrorMessages;
1920

2021
require_once __DIR__ . "/../Support/helper.php";
2122

@@ -110,7 +111,7 @@ public function except($level = 'BASE', $field_uids = array())
110111
);
111112
return $this->queryObject;
112113
}
113-
throw contentstackCreateError('field_uids must be an array');
114+
throw contentstackCreateError(ErrorMessages::FIELD_UIDS_ARRAY);
114115
}
115116

116117
/**
@@ -142,7 +143,7 @@ public function only($level = 'BASE', $field_uids = array())
142143
);
143144
return $this->queryObject;
144145
}
145-
throw contentstackCreateError('field_uids must be an array');
146+
throw contentstackCreateError(ErrorMessages::FIELD_UIDS_ARRAY);
146147
}
147148

148149
/**
@@ -175,7 +176,7 @@ public function includeReference($field_uids = array())
175176
);
176177
return $this->queryObject;
177178
}
178-
throw contentstackCreateError('field_uids must be an array');
179+
throw contentstackCreateError(ErrorMessages::FIELD_UIDS_ARRAY);
179180
}
180181

181182
/**
@@ -710,7 +711,7 @@ public function tags($tags = array())
710711
);
711712
return $this->queryObject;
712713
}
713-
throw contentstackCreateError('tags must be an array');
714+
throw contentstackCreateError(ErrorMessages::TAGS_ARRAY);
714715
}
715716

716717
/**
@@ -773,7 +774,7 @@ public function containedIn($field = '', $value = array())
773774
);
774775
return $this->queryObject;
775776
}
776-
throw contentstackCreateError('value must be an array');
777+
throw contentstackCreateError(ErrorMessages::VALUE_ARRAY);
777778
}
778779

779780
/**
@@ -809,7 +810,7 @@ public function notContainedIn($field = '', $value = array())
809810
);
810811
return $this->queryObject;
811812
}
812-
throw contentstackCreateError('value must be an array');
813+
throw contentstackCreateError(ErrorMessages::VALUE_ARRAY);
813814
}
814815

815816
/**
@@ -990,7 +991,7 @@ public function addQuery($_query = array())
990991
$this->subQuery = $_query;
991992
return $this->queryObject;
992993
}
993-
throw contentstackCreateError("Provide valid query");
994+
throw contentstackCreateError(ErrorMessages::INVALID_QUERY);
994995
}
995996

996997
/**

src/Support/helper.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Contentstack\Support\Utility;
4+
use Contentstack\Error\ErrorMessages;
45

56
if(!function_exists('contentstackGetFunctionName')) {
67
/*
@@ -40,7 +41,7 @@ function contentstackCreateError($msg = '') {
4041
* */
4142
function contentstackSearch($operator = '', $query = array(), $value = '') {
4243
if(!(!Utility::isEmpty($value) && is_string($value)))
43-
throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". String value expected.');
44+
throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_STRING_INPUT, contentstackGetFunctionName()));
4445
$query[$operator] = $value;
4546
return $query;
4647
}
@@ -57,7 +58,7 @@ function contentstackSearch($operator = '', $query = array(), $value = '') {
5758
* */
5859
function contentstackReferences($operator = '', $query = array(), $value = array()) {
5960
if(!is_array($value))
60-
throw contentstackCreateError('Invalid input for includeReferences. Array expected.');
61+
throw contentstackCreateError(ErrorMessages::INVALID_INCLUDE_REFERENCES);
6162
$query[$operator] = $value;
6263
return $query;
6364
}
@@ -77,7 +78,7 @@ function contentstackProjection($operator = '', $query = array(), $level = 'BASE
7778
$value = $level;
7879
$level = 'BASE';
7980
}
80-
if(!(!Utility::isEmpty($level) && is_string($level) && is_array($value))) throw contentstackCreateError('Invalid Input');
81+
if(!(!Utility::isEmpty($level) && is_string($level) && is_array($value))) throw contentstackCreateError(ErrorMessages::INVALID_INPUT_TYPE);
8182
if(!Utility::isKeySet($query, $operator)) $query[$operator] = array();
8283
if(!Utility::isKeySet($query[$operator], $level)) $query[$operator][$level] = array();
8384
$query[$operator][$level] = array_merge($query[$operator][$level], $value);
@@ -100,16 +101,16 @@ function contentstackProjection($operator = '', $query = array(), $level = 'BASE
100101
function contentstackRegexp($operator = '', $query = array(), $values = array()) {
101102
if(count($values) === 2 || count($values) === 3) {
102103
if(Utility::isEmpty($values[0]) && Utility::isEmpty($values[1]) && is_string($values[0]) && is_string($values[1]))
103-
throw contentstackCreateError('Invalid input for regex.Key must be string and value must be valid RegularExpression');
104+
throw contentstackCreateError(ErrorMessages::INVALID_REGEX_KEY_VALUE);
104105
if(isset($values[2]) && !(is_string($values[2]) && strlen($values[2]) > 0)) {
105-
throw contentstackCreateError('Invalid options for regex. Please provide the valid options');
106+
throw contentstackCreateError(ErrorMessages::INVALID_REGEX_OPTIONS);
106107
}
107108
$query[$values[0]] = array($operator => $values[1]);
108109
if(isset($values[2]))
109110
$query[$values[0]]['$options'] = $values[2];
110111
return $query;
111112
} else {
112-
throw contentstackCreateError('Invalid input for regex. At least 2 or maximum 3 arguments are required.');
113+
throw contentstackCreateError(ErrorMessages::INVALID_REGEX_ARGS);
113114
}
114115
}
115116
}
@@ -126,7 +127,7 @@ function contentstackRegexp($operator = '', $query = array(), $values = array())
126127
* */
127128
function contentstackTags($operator = '', $query = array(), $value = '') {
128129
if(!(is_array($value) && count($value) > 0))
129-
throw contentstackCreateError('Invalid input for tags.Value must be valid array of tags');
130+
throw contentstackCreateError(ErrorMessages::INVALID_TAGS_INPUT);
130131
$query[$operator] = $value;
131132
return $query;
132133
}
@@ -146,7 +147,7 @@ function contentstackTags($operator = '', $query = array(), $value = '') {
146147
* */
147148
function contentstackComparision($operator = '', $query = array(), $key = '', $value = '') {
148149
if(!(!Utility::isEmpty($key) && is_string($key) && !Utility::isEmpty($value)))
149-
throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Key must be string and value should be valid not empty.');
150+
throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_KEY_VALUE, contentstackGetFunctionName()));
150151
$query[$key] = array($operator => $value);
151152
return $query;
152153
}
@@ -165,7 +166,7 @@ function contentstackComparision($operator = '', $query = array(), $key = '', $v
165166
* */
166167
function contentstackLogical($operator = '', $query = array(), $value = array()) {
167168
if(!(is_array($value) && count($value) > 0))
168-
throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". At least one Query or array object is expected');
169+
throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_QUERY_INPUT, contentstackGetFunctionName()));
169170
foreach($value as $key => $_qry) {
170171
if(!Utility::isKeySet($query, $operator)) $query[$operator] = array();
171172
if($_qry instanceof \Contentstack\Stack\BaseQuery)
@@ -174,7 +175,7 @@ function contentstackLogical($operator = '', $query = array(), $value = array())
174175
array_push($query[$operator], $_qry);
175176
else {
176177
unset($query[$operator]);
177-
throw contentstackCreateError('Query objects are expected as arguments');
178+
throw contentstackCreateError(ErrorMessages::INVALID_QUERY_OBJECTS);
178179
}
179180
}
180181
return $query;
@@ -194,7 +195,7 @@ function contentstackLogical($operator = '', $query = array(), $value = array())
194195
* */
195196
function contentstackContains($operator = '', $query = array(), $key = '', $value = array()) {
196197
if (!(!Utility::isEmpty($key) && is_string($key) && is_array($value)))
197-
throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Key should be string and value must be array.');
198+
throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_KEY_ARRAY_VALUE, contentstackGetFunctionName()));
198199
$query[$key] = array($operator => $value);
199200
return $query;
200201
}
@@ -212,7 +213,7 @@ function contentstackContains($operator = '', $query = array(), $key = '', $valu
212213
* */
213214
function contentstackPagination($operator = '', $query = array(), $value = '') {
214215
if (!(!Utility::isEmpty($value) && is_numeric($value)))
215-
throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'", it should be Numeric.');
216+
throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_NUMERIC_INPUT, contentstackGetFunctionName()));
216217
$query[$operator] = $value;
217218
return $query;
218219
}
@@ -231,7 +232,7 @@ function contentstackPagination($operator = '', $query = array(), $value = '') {
231232
function contentstackLanguage($operator = '', $query = array(), $value = '') {
232233

233234
if (!(!Utility::isEmpty($value) && is_string($value)))
234-
throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'", it should be String.');
235+
throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_STRING_INPUT, contentstackGetFunctionName()));
235236
$query[$operator] = $value;
236237
return $query;
237238
}
@@ -249,7 +250,7 @@ function contentstackLanguage($operator = '', $query = array(), $value = '') {
249250
* */
250251
function contentstackSorting($operator = '', $query = array(), $key = '') {
251252
if (!(!Utility::isEmpty($key) && is_string($key)))
252-
throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Value should be valid field in entry');
253+
throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_FIELD_INPUT, contentstackGetFunctionName()));
253254
$query[$operator] = $key;
254255
return $query;
255256
}
@@ -300,7 +301,7 @@ function contentstackAddParam($key = '', $query = array(), $value = '') {
300301
* */
301302
function contentstackExistence($operator = '', $query = array(), $key = '', $value = false) {
302303
if (!(!Utility::isEmpty($key) && is_string($key)))
303-
throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Key should be valid String field uid');
304+
throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_FIELD_UID, contentstackGetFunctionName()));
304305
$query[$key] = array($operator => $value);
305306
return $query;
306307
}

0 commit comments

Comments
 (0)