Skip to content

Commit 917cc8a

Browse files
committed
intl: report more information about message pattern parse errors
the message patterns can be pretty complex, so reporting a generic U_PARSE_ERROR without any additional information makes it needlessly hard to fix erroneous patterns. This commit makes use of the additional UParseError* parameter to umsg_open to retrieve more details about the parse error to report that to the user via intl_get_error_message()
1 parent c3365bb commit 917cc8a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ TypeError: msgfmt_create(): Argument #1 ($locale) must be of type string, array
146146
'U_ZERO_ERROR'
147147

148148
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'
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

153153
IntlException: Constructor failed in %s on line %d
154154
'msgfmt_create: message formatter creation failed: U_UNMATCHED_BRACES'

0 commit comments

Comments
 (0)