xref: /PHP-8.1/ext/intl/common/common_error.c (revision 01b3fc03)
1 /*
2    +----------------------------------------------------------------------+
3    | This source file is subject to version 3.01 of the PHP license,      |
4    | that is bundled with this package in the file LICENSE, and is        |
5    | available through the world-wide-web at the following url:           |
6    | https://www.php.net/license/3_01.txt                                 |
7    | If you did not receive a copy of the PHP license and are unable to   |
8    | obtain it through the world-wide-web, please send a note to          |
9    | license@php.net so we can mail you a copy immediately.               |
10    +----------------------------------------------------------------------+
11    | Authors: Vadim Savchuk <vsavchuk@productengine.com>                  |
12    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
13    +----------------------------------------------------------------------+
14  */
15 
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 
20 #include "php_intl.h"
21 #include "intl_error.h"
22 #include "common_error.h"
23 
24 /* {{{ Get code of the last occurred error. */
PHP_FUNCTION(intl_get_error_code)25 PHP_FUNCTION( intl_get_error_code )
26 {
27 	if (zend_parse_parameters_none() == FAILURE) {
28 		RETURN_THROWS();
29 	}
30 
31 	RETURN_LONG( intl_error_get_code( NULL ) );
32 }
33 /* }}} */
34 
35 /* {{{ Get text description of the last occurred error. */
PHP_FUNCTION(intl_get_error_message)36 PHP_FUNCTION( intl_get_error_message )
37 {
38 	if (zend_parse_parameters_none() == FAILURE) {
39 		RETURN_THROWS();
40 	}
41 
42 	RETURN_STR(intl_error_get_message( NULL ));
43 }
44 /* }}} */
45 
46 /* {{{ Check whether the given error code indicates a failure.
47  * Returns true if it does, and false if the code
48  * indicates success or a warning.
49  */
PHP_FUNCTION(intl_is_failure)50 PHP_FUNCTION( intl_is_failure )
51 {
52 	zend_long err_code;
53 
54 	/* Parse parameters. */
55 	if( zend_parse_parameters( ZEND_NUM_ARGS(), "l",
56 		&err_code ) == FAILURE )
57 	{
58 		RETURN_THROWS();
59 	}
60 
61 	RETURN_BOOL( U_FAILURE( err_code ) );
62 }
63 /* }}} */
64 
65 /* {{{ Return a string for a given error code.
66  * The string will be the same as the name of the error code constant.
67  */
PHP_FUNCTION(intl_error_name)68 PHP_FUNCTION( intl_error_name )
69 {
70 	zend_long err_code;
71 
72 	/* Parse parameters. */
73 	if( zend_parse_parameters( ZEND_NUM_ARGS(), "l",
74 		&err_code ) == FAILURE )
75 	{
76 		RETURN_THROWS();
77 	}
78 
79 	RETURN_STRING( (char*)u_errorName( err_code ) );
80 }
81 /* }}} */
82 
83 /* {{{ intl_expose_icu_error_codes
84  * Expose ICU error codes
85  */
intl_expose_icu_error_codes(INIT_FUNC_ARGS)86 void intl_expose_icu_error_codes( INIT_FUNC_ARGS )
87 {
88 	#define INTL_EXPOSE_CONST(x) REGISTER_LONG_CONSTANT(#x, x, CONST_PERSISTENT | CONST_CS)
89 
90 	/* Warnings */
91 	INTL_EXPOSE_CONST( U_USING_FALLBACK_WARNING );
92 	INTL_EXPOSE_CONST( U_ERROR_WARNING_START );
93 	INTL_EXPOSE_CONST( U_USING_DEFAULT_WARNING );
94 	INTL_EXPOSE_CONST( U_SAFECLONE_ALLOCATED_WARNING );
95 	INTL_EXPOSE_CONST( U_STATE_OLD_WARNING );
96 	INTL_EXPOSE_CONST( U_STRING_NOT_TERMINATED_WARNING );
97 	INTL_EXPOSE_CONST( U_SORT_KEY_TOO_SHORT_WARNING );
98 	INTL_EXPOSE_CONST( U_AMBIGUOUS_ALIAS_WARNING );
99 	INTL_EXPOSE_CONST( U_DIFFERENT_UCA_VERSION );
100 	INTL_EXPOSE_CONST( U_ERROR_WARNING_LIMIT );
101 
102 	/* Standard errors */
103 	INTL_EXPOSE_CONST( U_ZERO_ERROR );
104 	INTL_EXPOSE_CONST( U_ILLEGAL_ARGUMENT_ERROR );
105 	INTL_EXPOSE_CONST( U_MISSING_RESOURCE_ERROR );
106 	INTL_EXPOSE_CONST( U_INVALID_FORMAT_ERROR );
107 	INTL_EXPOSE_CONST( U_FILE_ACCESS_ERROR );
108 	INTL_EXPOSE_CONST( U_INTERNAL_PROGRAM_ERROR );
109 	INTL_EXPOSE_CONST( U_MESSAGE_PARSE_ERROR );
110 	INTL_EXPOSE_CONST( U_MEMORY_ALLOCATION_ERROR );
111 	INTL_EXPOSE_CONST( U_INDEX_OUTOFBOUNDS_ERROR );
112 	INTL_EXPOSE_CONST( U_PARSE_ERROR );
113 	INTL_EXPOSE_CONST( U_INVALID_CHAR_FOUND );
114 	INTL_EXPOSE_CONST( U_TRUNCATED_CHAR_FOUND );
115 	INTL_EXPOSE_CONST( U_ILLEGAL_CHAR_FOUND );
116 	INTL_EXPOSE_CONST( U_INVALID_TABLE_FORMAT );
117 	INTL_EXPOSE_CONST( U_INVALID_TABLE_FILE );
118 	INTL_EXPOSE_CONST( U_BUFFER_OVERFLOW_ERROR );
119 	INTL_EXPOSE_CONST( U_UNSUPPORTED_ERROR );
120 	INTL_EXPOSE_CONST( U_RESOURCE_TYPE_MISMATCH );
121 	INTL_EXPOSE_CONST( U_ILLEGAL_ESCAPE_SEQUENCE );
122 	INTL_EXPOSE_CONST( U_UNSUPPORTED_ESCAPE_SEQUENCE );
123 	INTL_EXPOSE_CONST( U_NO_SPACE_AVAILABLE );
124 	INTL_EXPOSE_CONST( U_CE_NOT_FOUND_ERROR );
125 	INTL_EXPOSE_CONST( U_PRIMARY_TOO_LONG_ERROR );
126 	INTL_EXPOSE_CONST( U_STATE_TOO_OLD_ERROR );
127 	INTL_EXPOSE_CONST( U_TOO_MANY_ALIASES_ERROR );
128 	INTL_EXPOSE_CONST( U_ENUM_OUT_OF_SYNC_ERROR );
129 	INTL_EXPOSE_CONST( U_INVARIANT_CONVERSION_ERROR );
130 	INTL_EXPOSE_CONST( U_INVALID_STATE_ERROR );
131 	INTL_EXPOSE_CONST( U_COLLATOR_VERSION_MISMATCH );
132 	INTL_EXPOSE_CONST( U_USELESS_COLLATOR_ERROR );
133 	INTL_EXPOSE_CONST( U_NO_WRITE_PERMISSION );
134 	INTL_EXPOSE_CONST( U_STANDARD_ERROR_LIMIT );
135 
136 	/* The error code range 0x10000 0x10100 are reserved for Transliterator */
137 	INTL_EXPOSE_CONST( U_BAD_VARIABLE_DEFINITION );
138 	INTL_EXPOSE_CONST( U_PARSE_ERROR_START );
139 	INTL_EXPOSE_CONST( U_MALFORMED_RULE );
140 	INTL_EXPOSE_CONST( U_MALFORMED_SET );
141 	INTL_EXPOSE_CONST( U_MALFORMED_SYMBOL_REFERENCE );
142 	INTL_EXPOSE_CONST( U_MALFORMED_UNICODE_ESCAPE );
143 	INTL_EXPOSE_CONST( U_MALFORMED_VARIABLE_DEFINITION );
144 	INTL_EXPOSE_CONST( U_MALFORMED_VARIABLE_REFERENCE );
145 	INTL_EXPOSE_CONST( U_MISMATCHED_SEGMENT_DELIMITERS );
146 	INTL_EXPOSE_CONST( U_MISPLACED_ANCHOR_START );
147 	INTL_EXPOSE_CONST( U_MISPLACED_CURSOR_OFFSET );
148 	INTL_EXPOSE_CONST( U_MISPLACED_QUANTIFIER );
149 	INTL_EXPOSE_CONST( U_MISSING_OPERATOR );
150 	INTL_EXPOSE_CONST( U_MISSING_SEGMENT_CLOSE );
151 	INTL_EXPOSE_CONST( U_MULTIPLE_ANTE_CONTEXTS );
152 	INTL_EXPOSE_CONST( U_MULTIPLE_CURSORS );
153 	INTL_EXPOSE_CONST( U_MULTIPLE_POST_CONTEXTS );
154 	INTL_EXPOSE_CONST( U_TRAILING_BACKSLASH );
155 	INTL_EXPOSE_CONST( U_UNDEFINED_SEGMENT_REFERENCE );
156 	INTL_EXPOSE_CONST( U_UNDEFINED_VARIABLE );
157 	INTL_EXPOSE_CONST( U_UNQUOTED_SPECIAL );
158 	INTL_EXPOSE_CONST( U_UNTERMINATED_QUOTE );
159 	INTL_EXPOSE_CONST( U_RULE_MASK_ERROR );
160 	INTL_EXPOSE_CONST( U_MISPLACED_COMPOUND_FILTER );
161 	INTL_EXPOSE_CONST( U_MULTIPLE_COMPOUND_FILTERS );
162 	INTL_EXPOSE_CONST( U_INVALID_RBT_SYNTAX );
163 	INTL_EXPOSE_CONST( U_INVALID_PROPERTY_PATTERN );
164 	INTL_EXPOSE_CONST( U_MALFORMED_PRAGMA );
165 	INTL_EXPOSE_CONST( U_UNCLOSED_SEGMENT );
166 	INTL_EXPOSE_CONST( U_ILLEGAL_CHAR_IN_SEGMENT );
167 	INTL_EXPOSE_CONST( U_VARIABLE_RANGE_EXHAUSTED );
168 	INTL_EXPOSE_CONST( U_VARIABLE_RANGE_OVERLAP );
169 	INTL_EXPOSE_CONST( U_ILLEGAL_CHARACTER );
170 	INTL_EXPOSE_CONST( U_INTERNAL_TRANSLITERATOR_ERROR );
171 	INTL_EXPOSE_CONST( U_INVALID_ID );
172 	INTL_EXPOSE_CONST( U_INVALID_FUNCTION );
173 	INTL_EXPOSE_CONST( U_PARSE_ERROR_LIMIT );
174 
175 	/* The error code range 0x10100 0x10200 are reserved for formatting API parsing error */
176 	INTL_EXPOSE_CONST( U_UNEXPECTED_TOKEN );
177 	INTL_EXPOSE_CONST( U_FMT_PARSE_ERROR_START );
178 	INTL_EXPOSE_CONST( U_MULTIPLE_DECIMAL_SEPARATORS );
179 	INTL_EXPOSE_CONST( U_MULTIPLE_DECIMAL_SEPERATORS );    /* Typo: kept for backward compatibility. Use U_MULTIPLE_DECIMAL_SEPARATORS */
180 	INTL_EXPOSE_CONST( U_MULTIPLE_EXPONENTIAL_SYMBOLS );
181 	INTL_EXPOSE_CONST( U_MALFORMED_EXPONENTIAL_PATTERN );
182 	INTL_EXPOSE_CONST( U_MULTIPLE_PERCENT_SYMBOLS );
183 	INTL_EXPOSE_CONST( U_MULTIPLE_PERMILL_SYMBOLS );
184 	INTL_EXPOSE_CONST( U_MULTIPLE_PAD_SPECIFIERS );
185 	INTL_EXPOSE_CONST( U_PATTERN_SYNTAX_ERROR );
186 	INTL_EXPOSE_CONST( U_ILLEGAL_PAD_POSITION );
187 	INTL_EXPOSE_CONST( U_UNMATCHED_BRACES );
188 	INTL_EXPOSE_CONST( U_UNSUPPORTED_PROPERTY );
189 	INTL_EXPOSE_CONST( U_UNSUPPORTED_ATTRIBUTE );
190 	INTL_EXPOSE_CONST( U_FMT_PARSE_ERROR_LIMIT );
191 
192 	/* The error code range 0x10200 0x102ff are reserved for Break Iterator related error */
193 	INTL_EXPOSE_CONST( U_BRK_INTERNAL_ERROR );
194 	INTL_EXPOSE_CONST( U_BRK_ERROR_START );
195 	INTL_EXPOSE_CONST( U_BRK_HEX_DIGITS_EXPECTED );
196 	INTL_EXPOSE_CONST( U_BRK_SEMICOLON_EXPECTED );
197 	INTL_EXPOSE_CONST( U_BRK_RULE_SYNTAX );
198 	INTL_EXPOSE_CONST( U_BRK_UNCLOSED_SET );
199 	INTL_EXPOSE_CONST( U_BRK_ASSIGN_ERROR );
200 	INTL_EXPOSE_CONST( U_BRK_VARIABLE_REDFINITION );
201 	INTL_EXPOSE_CONST( U_BRK_MISMATCHED_PAREN );
202 	INTL_EXPOSE_CONST( U_BRK_NEW_LINE_IN_QUOTED_STRING );
203 	INTL_EXPOSE_CONST( U_BRK_UNDEFINED_VARIABLE );
204 	INTL_EXPOSE_CONST( U_BRK_INIT_ERROR );
205 	INTL_EXPOSE_CONST( U_BRK_RULE_EMPTY_SET );
206 	INTL_EXPOSE_CONST( U_BRK_UNRECOGNIZED_OPTION );
207 	INTL_EXPOSE_CONST( U_BRK_MALFORMED_RULE_TAG );
208 	INTL_EXPOSE_CONST( U_BRK_ERROR_LIMIT );
209 
210 	/* The error codes in the range 0x10300-0x103ff are reserved for regular expression related errrs */
211 	INTL_EXPOSE_CONST( U_REGEX_INTERNAL_ERROR );
212 	INTL_EXPOSE_CONST( U_REGEX_ERROR_START );
213 	INTL_EXPOSE_CONST( U_REGEX_RULE_SYNTAX );
214 	INTL_EXPOSE_CONST( U_REGEX_INVALID_STATE );
215 	INTL_EXPOSE_CONST( U_REGEX_BAD_ESCAPE_SEQUENCE );
216 	INTL_EXPOSE_CONST( U_REGEX_PROPERTY_SYNTAX );
217 	INTL_EXPOSE_CONST( U_REGEX_UNIMPLEMENTED );
218 	INTL_EXPOSE_CONST( U_REGEX_MISMATCHED_PAREN );
219 	INTL_EXPOSE_CONST( U_REGEX_NUMBER_TOO_BIG );
220 	INTL_EXPOSE_CONST( U_REGEX_BAD_INTERVAL );
221 	INTL_EXPOSE_CONST( U_REGEX_MAX_LT_MIN );
222 	INTL_EXPOSE_CONST( U_REGEX_INVALID_BACK_REF );
223 	INTL_EXPOSE_CONST( U_REGEX_INVALID_FLAG );
224 	INTL_EXPOSE_CONST( U_REGEX_LOOK_BEHIND_LIMIT );
225 	INTL_EXPOSE_CONST( U_REGEX_SET_CONTAINS_STRING );
226 	INTL_EXPOSE_CONST( U_REGEX_ERROR_LIMIT );
227 
228 	/* The error code in the range 0x10400-0x104ff are reserved for IDNA related error codes */
229 	INTL_EXPOSE_CONST( U_IDNA_PROHIBITED_ERROR );
230 	INTL_EXPOSE_CONST( U_IDNA_ERROR_START );
231 	INTL_EXPOSE_CONST( U_IDNA_UNASSIGNED_ERROR );
232 	INTL_EXPOSE_CONST( U_IDNA_CHECK_BIDI_ERROR );
233 	INTL_EXPOSE_CONST( U_IDNA_STD3_ASCII_RULES_ERROR );
234 	INTL_EXPOSE_CONST( U_IDNA_ACE_PREFIX_ERROR );
235 	INTL_EXPOSE_CONST( U_IDNA_VERIFICATION_ERROR );
236 	INTL_EXPOSE_CONST( U_IDNA_LABEL_TOO_LONG_ERROR );
237 	INTL_EXPOSE_CONST( U_IDNA_ZERO_LENGTH_LABEL_ERROR );
238 	INTL_EXPOSE_CONST( U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR );
239 	INTL_EXPOSE_CONST( U_IDNA_ERROR_LIMIT );
240 
241 	/* Aliases for StringPrep */
242 	INTL_EXPOSE_CONST( U_STRINGPREP_PROHIBITED_ERROR );
243 	INTL_EXPOSE_CONST( U_STRINGPREP_UNASSIGNED_ERROR );
244 	INTL_EXPOSE_CONST( U_STRINGPREP_CHECK_BIDI_ERROR );
245 
246 	INTL_EXPOSE_CONST( U_ERROR_LIMIT );
247 
248 	#undef INTL_EXPOSE_CONST
249 }
250 /* }}} */
251