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