php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
intl_error.c
Go to the documentation of this file.
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 | Stanislav Malyshev <stas@zend.com> |
14 +----------------------------------------------------------------------+
15 */
16
17#ifdef HAVE_CONFIG_H
18#include <config.h>
19#endif
20
21#include <php.h>
22#include <zend_exceptions.h>
23
24#include "php_intl.h"
25#include "intl_error.h"
26#include "intl_convert.h"
27
29
31
32/* {{{ Return global error structure. */
33static intl_error* intl_g_error_get( void )
34{
35 return &INTL_G( g_error );
36}
37/* }}} */
38
39/* {{{ Free mem. */
40static void intl_free_custom_error_msg( intl_error* err )
41{
42 if( !err && !( err = intl_g_error_get( ) ) )
43 return;
44
45 if(err->free_custom_error_message ) {
46 efree( err->custom_error_message );
47 }
48
49 err->custom_error_message = NULL;
50 err->free_custom_error_message = 0;
51}
52/* }}} */
53
54/* {{{ Create and initialize internals of 'intl_error'. */
56{
57 intl_error* err = ecalloc( 1, sizeof( intl_error ) );
58
60
61 return err;
62}
63/* }}} */
64
65/* {{{ Initialize internals of 'intl_error'. */
67{
68 if( !err && !( err = intl_g_error_get( ) ) )
69 return;
70
71 err->code = U_ZERO_ERROR;
72 err->custom_error_message = NULL;
73 err->free_custom_error_message = 0;
74}
75/* }}} */
76
77/* {{{ Set last error code to 0 and unset last error message */
79{
80 if( !err && !( err = intl_g_error_get( ) ) )
81 return;
82
83 err->code = U_ZERO_ERROR;
84
85 intl_free_custom_error_msg( err );
86}
87/* }}} */
88
89/* {{{ Set last error message to msg copying it if needed. */
90void intl_error_set_custom_msg( intl_error* err, const char* msg, int copyMsg )
91{
92 if( !msg )
93 return;
94
95 if( !err ) {
96 if( INTL_G( error_level ) )
98 if( INTL_G( use_exceptions ) )
100 }
101 if( !err && !( err = intl_g_error_get( ) ) )
102 return;
103
104 /* Free previous message if any */
105 intl_free_custom_error_msg( err );
106
107 /* Mark message copied if any */
108 err->free_custom_error_message = copyMsg;
109
110 /* Set user's error text message */
111 err->custom_error_message = copyMsg ? estrdup( msg ) : (char *) msg;
112}
113/* }}} */
114
115/* {{{ Create output message in format "<intl_error_text>: <extra_user_error_text>". */
117{
118 const char *uErrorName = NULL;
119 zend_string *errMessage = 0;
120
121 if( !err && !( err = intl_g_error_get( ) ) )
122 return ZSTR_EMPTY_ALLOC();
123
124 uErrorName = u_errorName( err->code );
125
126 /* Format output string */
127 if( err->custom_error_message )
128 {
129 errMessage = strpprintf(0, "%s: %s", err->custom_error_message, uErrorName );
130 }
131 else
132 {
133 errMessage = strpprintf(0, "%s", uErrorName );
134 }
135
136 return errMessage;
137}
138/* }}} */
139
140/* {{{ Set last error code. */
141void intl_error_set_code( intl_error* err, UErrorCode err_code )
142{
143 if( !err && !( err = intl_g_error_get( ) ) )
144 return;
145
146 err->code = err_code;
147}
148/* }}} */
149
150/* {{{ Return last error code. */
152{
153 if( !err && !( err = intl_g_error_get( ) ) )
154 return U_ZERO_ERROR;
155
156 return err->code;
157}
158/* }}} */
159
160/* {{{ Set error code and message. */
161void intl_error_set( intl_error* err, UErrorCode code, const char* msg, int copyMsg )
162{
163 intl_error_set_code( err, code );
164 intl_error_set_custom_msg( err, msg, copyMsg );
165}
166/* }}} */
167
168/* {{{ Set error code and message. */
169void intl_errors_set( intl_error* err, UErrorCode code, const char* msg, int copyMsg )
170{
171 intl_errors_set_code( err, code );
173}
174/* }}} */
175
176/* {{{ */
178{
179 if(err) {
181 }
183}
184/* }}} */
185
186/* {{{ */
187void intl_errors_set_custom_msg( intl_error* err, const char* msg, int copyMsg )
188{
189 if(err) {
190 intl_error_set_custom_msg( err, msg, copyMsg );
191 }
193}
194/* }}} */
195
196/* {{{ */
197void intl_errors_set_code( intl_error* err, UErrorCode err_code )
198{
199 if(err) {
200 intl_error_set_code( err, err_code );
201 }
202 intl_error_set_code( NULL, err_code );
203}
204/* }}} */
205
207{
208 smart_str ret = {0};
209 zend_string *u8str;
210 UErrorCode status;
211 int any = 0;
212
213 assert( pe != NULL );
214
215 smart_str_appends( &ret, "parse error " );
216 if( pe->line > 0 )
217 {
218 smart_str_appends( &ret, "on line " );
219 smart_str_append_long( &ret, (zend_long ) pe->line );
220 any = 1;
221 }
222 if( pe->offset >= 0 ) {
223 if( any )
224 smart_str_appends( &ret, ", " );
225 else
226 smart_str_appends( &ret, "at " );
227
228 smart_str_appends( &ret, "offset " );
229 smart_str_append_long( &ret, (zend_long ) pe->offset );
230 any = 1;
231 }
232
233 if (pe->preContext[0] != 0 ) {
234 if( any )
235 smart_str_appends( &ret, ", " );
236
237 smart_str_appends( &ret, "after \"" );
238 u8str = intl_convert_utf16_to_utf8(pe->preContext, -1, &status );
239 if( !u8str )
240 {
241 smart_str_appends( &ret, "(could not convert parser error pre-context to UTF-8)" );
242 }
243 else {
244 smart_str_append( &ret, u8str );
245 zend_string_release_ex( u8str, 0 );
246 }
247 smart_str_appends( &ret, "\"" );
248 any = 1;
249 }
250
251 if( pe->postContext[0] != 0 )
252 {
253 if( any )
254 smart_str_appends( &ret, ", " );
255
256 smart_str_appends( &ret, "before or at \"" );
257 u8str = intl_convert_utf16_to_utf8(pe->postContext, -1, &status );
258 if( !u8str )
259 {
260 smart_str_appends( &ret, "(could not convert parser error post-context to UTF-8)" );
261 }
262 else
263 {
264 smart_str_append( &ret, u8str );
265 zend_string_release_ex( u8str, 0 );
266 }
267 smart_str_appends( &ret, "\"" );
268 any = 1;
269 }
270
271 if( !any )
272 {
273 smart_str_free( &ret );
274 smart_str_appends( &ret, "no parse error" );
275 }
276
277 smart_str_0( &ret );
278 return ret;
279}
assert(mixed $assertion, Throwable|string|null $description=null)
const U_ZERO_ERROR
DNS_STATUS status
Definition dns_win32.c:49
char * err
Definition ffi.c:3029
#define NULL
Definition gdcache.h:45
zend_class_entry * IntlException_ce_ptr
Definition intl_error.c:30
zend_string * intl_convert_utf16_to_utf8(const UChar *src, int32_t src_len, UErrorCode *status)
void intl_errors_set_custom_msg(intl_error *err, const char *msg, int copyMsg)
Definition intl_error.c:187
void intl_error_init(intl_error *err)
Definition intl_error.c:66
void intl_errors_set(intl_error *err, UErrorCode code, const char *msg, int copyMsg)
Definition intl_error.c:169
void intl_error_set(intl_error *err, UErrorCode code, const char *msg, int copyMsg)
Definition intl_error.c:161
void intl_error_reset(intl_error *err)
Definition intl_error.c:78
intl_error * intl_error_create(void)
Definition intl_error.c:55
void intl_errors_reset(intl_error *err)
Definition intl_error.c:177
void intl_error_set_code(intl_error *err, UErrorCode err_code)
Definition intl_error.c:141
UErrorCode intl_error_get_code(intl_error *err)
Definition intl_error.c:151
void intl_error_set_custom_msg(intl_error *err, const char *msg, int copyMsg)
Definition intl_error.c:90
smart_str intl_parse_error_to_string(UParseError *pe)
Definition intl_error.c:206
void intl_errors_set_code(intl_error *err, UErrorCode err_code)
Definition intl_error.c:197
zend_string * intl_error_get_message(intl_error *err)
Definition intl_error.c:116
struct _intl_error intl_error
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
intl_error g_error
Definition php_intl.h:51
bool use_exceptions
Definition php_intl.h:53
zend_long error_level
Definition php_intl.h:52
#define INTL_G(v)
Definition php_intl.h:62
char * msg
Definition phpdbg.h:289
#define strpprintf
Definition spprintf.h:30
#define ZEND_EXTERN_MODULE_GLOBALS(module_name)
Definition zend_API.h:270
#define ecalloc(nmemb, size)
Definition zend_alloc.h:158
#define efree(ptr)
Definition zend_alloc.h:155
#define estrdup(s)
Definition zend_alloc.h:164
zend_string_release_ex(func->internal_function.function_name, 0)
ZEND_API ZEND_COLD zend_object * zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format,...)
int32_t zend_long
Definition zend_long.h:42
struct _zend_string zend_string
struct _zend_class_entry zend_class_entry
#define ZSTR_EMPTY_ALLOC()
zval * ret