Skip to content

Commit 656c37a

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: intl: report more information about message pattern parse errors
2 parents c0b4dea + 6edad17 commit 656c37a

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

ext/intl/msgformat/msgformat.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static int msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
3434
int spattern_len = 0;
3535
zval* object;
3636
MessageFormatter_object* mfo;
37+
UParseError parse_error;
3738
intl_error_reset( NULL );
3839

3940
object = return_value;
@@ -74,12 +75,26 @@ static int msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
7475
(mfo)->mf_data.orig_format_len = pattern_len;
7576

7677
/* Create an ICU message formatter. */
77-
MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(mfo));
78+
MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, &parse_error, &INTL_DATA_ERROR_CODE(mfo));
7879

7980
if(spattern) {
8081
efree(spattern);
8182
}
8283

84+
if (INTL_DATA_ERROR_CODE( mfo ) == U_PATTERN_SYNTAX_ERROR) {
85+
char *msg = NULL;
86+
smart_str parse_error_str;
87+
parse_error_str = intl_parse_error_to_string( &parse_error );
88+
spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
89+
smart_str_free( &parse_error_str );
90+
91+
intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) );
92+
intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 );
93+
94+
efree( msg );
95+
return FAILURE;
96+
}
97+
8398
INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed");
8499
return SUCCESS;
85100
}
@@ -105,7 +120,9 @@ PHP_METHOD( MessageFormatter, __construct )
105120
return_value = ZEND_THIS;
106121
if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU) == FAILURE) {
107122
if (!EG(exception)) {
108-
zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
123+
zend_string *err = intl_error_get_message(NULL);
124+
zend_throw_exception(IntlException_ce_ptr, ZSTR_VAL(err), intl_error_get_code(NULL));
125+
zend_string_release_ex(err, 0);
109126
}
110127
}
111128
zend_restore_error_handling(&error_handling);

ext/intl/msgformat/msgformat_format.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ PHP_FUNCTION( msgfmt_format_message )
8080
size_t slocale_len = 0;
8181
MessageFormatter_object mf;
8282
MessageFormatter_object *mfo = &mf;
83+
UParseError parse_error;
8384

8485
/* Parse parameters. */
8586
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "ssa",
@@ -119,10 +120,25 @@ PHP_FUNCTION( msgfmt_format_message )
119120
#endif
120121

121122
/* Create an ICU message formatter. */
122-
MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
123+
MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, &parse_error, &INTL_DATA_ERROR_CODE(mfo));
123124
if(spattern && spattern_len) {
124125
efree(spattern);
125126
}
127+
128+
if (INTL_DATA_ERROR_CODE( mfo ) == U_PATTERN_SYNTAX_ERROR) {
129+
char *msg = NULL;
130+
smart_str parse_error_str;
131+
parse_error_str = intl_parse_error_to_string( &parse_error );
132+
spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
133+
smart_str_free( &parse_error_str );
134+
135+
intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) );
136+
intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 );
137+
138+
efree( msg );
139+
RETURN_FALSE;
140+
}
141+
126142
INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
127143

128144
msgfmt_do_format(mfo, args, return_value);

ext/intl/tests/msgfmt_fail2.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ ArgumentCountError: msgfmt_create() expects exactly 2 arguments, 1 given in %s o
126126
ArgumentCountError: MessageFormatter::create() expects exactly 2 arguments, 1 given in %s on line %d
127127
'U_ZERO_ERROR'
128128

129-
IntlException: Constructor failed in %s on line %d
129+
IntlException: msgfmt_create: message formatter creation failed: U_ILLEGAL_ARGUMENT_ERROR in %s on line %d
130130
'msgfmt_create: message formatter creation failed: U_ILLEGAL_ARGUMENT_ERROR'
131131
'msgfmt_create: message formatter creation failed: U_ILLEGAL_ARGUMENT_ERROR'
132132
'msgfmt_create: message formatter creation failed: U_ILLEGAL_ARGUMENT_ERROR'
133133

134-
IntlException: Constructor failed in %s on line %d
134+
IntlException: msgfmt_create: message formatter creation failed: U_ILLEGAL_ARGUMENT_ERROR in %s on line %d
135135
'msgfmt_create: message formatter creation failed: U_ILLEGAL_ARGUMENT_ERROR'
136136
'msgfmt_create: message formatter creation failed: U_ILLEGAL_ARGUMENT_ERROR'
137137
'msgfmt_create: message formatter creation failed: U_ILLEGAL_ARGUMENT_ERROR'
@@ -145,17 +145,17 @@ TypeError: MessageFormatter::create(): Argument #1 ($locale) must be of type str
145145
TypeError: msgfmt_create(): Argument #1 ($locale) must be of type string, array given in %s on line %d
146146
'U_ZERO_ERROR'
147147

148-
IntlException: Constructor failed in %s on line %d
149-
'msgfmt_create: message formatter creation failed: U_PATTERN_SYNTAX_ERROR'
150-
'msgfmt_create: message formatter creation failed: U_PATTERN_SYNTAX_ERROR'
151-
'msgfmt_create: message formatter creation failed: U_PATTERN_SYNTAX_ERROR'
148+
IntlException: pattern syntax error (parse error at offset 1, after "{", before or at "0,choice}"): U_PATTERN_SYNTAX_ERROR in %s on line %d
149+
'pattern syntax error (parse error at offset 1, after "{", before or at "0,choice}"): U_PATTERN_SYNTAX_ERROR'
150+
'pattern syntax error (parse error at offset 1, after "{", before or at "0,choice}"): U_PATTERN_SYNTAX_ERROR'
151+
'pattern syntax error (parse error at offset 1, after "{", before or at "0,choice}"): U_PATTERN_SYNTAX_ERROR'
152152

153-
IntlException: Constructor failed in %s on line %d
153+
IntlException: msgfmt_create: message formatter creation failed: U_UNMATCHED_BRACES in %s on line %d
154154
'msgfmt_create: message formatter creation failed: U_UNMATCHED_BRACES'
155155
'msgfmt_create: message formatter creation failed: U_UNMATCHED_BRACES'
156156
'msgfmt_create: message formatter creation failed: U_UNMATCHED_BRACES'
157157

158-
IntlException: Constructor failed in %s on line %d
158+
IntlException: msgfmt_create: error converting pattern to UTF-16: U_INVALID_CHAR_FOUND in %s on line %d
159159
'msgfmt_create: error converting pattern to UTF-16: U_INVALID_CHAR_FOUND'
160160
'msgfmt_create: error converting pattern to UTF-16: U_INVALID_CHAR_FOUND'
161161
'msgfmt_create: error converting pattern to UTF-16: U_INVALID_CHAR_FOUND'

0 commit comments

Comments
 (0)