Skip to content

Commit fe0263f

Browse files
authored
uri: Throw UriError for unexpected failures (php#19850)
* uri: Add `UriError` * uri: Throw `UriError` for unexpected failures in uri_parser_rfc3986 This is a follow-up for php#19779 which updated the error *messages* for the non-syntax errors, but did not update the exception class, still implying it's related to invalid URIs. Given that we don't know ourselves if these are reachable in practice, they are cannot be meaningfully handled by a user of PHP. Thus this should be a `Error` according to our exception policy. * uri: Throw `UriError` when unable to recompose URIs * uri: Throw `UriError` when unable to read component * NEWS
1 parent 4b6c465 commit fe0263f

File tree

7 files changed

+32
-12
lines changed

7 files changed

+32
-12
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ PHP NEWS
4646
(nielsdos)
4747
. Prevent modifying Uri\WhatWg\Url and Uri\Rfc3986\Uri objects by manually
4848
calling __construct() or __unserialize(). (timwolla)
49+
. Add new Uri\UriError exception that is thrown for internal error
50+
conditions. (timwolla)
4951
. Further clean up the internal API. (timwolla)
5052

5153
- Windows:

ext/uri/php_uri.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ zend_class_entry *uri_whatwg_url_ce;
3838
zend_object_handlers uri_whatwg_uri_object_handlers;
3939
zend_class_entry *uri_comparison_mode_ce;
4040
zend_class_entry *uri_exception_ce;
41+
zend_class_entry *uri_error_ce;
4142
zend_class_entry *uri_invalid_uri_exception_ce;
4243
zend_class_entry *uri_whatwg_invalid_url_exception_ce;
4344
zend_class_entry *uri_whatwg_url_validation_error_type_ce;
@@ -675,7 +676,7 @@ PHP_METHOD(Uri_Rfc3986_Uri, withFragment)
675676

676677
static void throw_cannot_recompose_uri_to_string(zend_object *object)
677678
{
678-
zend_throw_exception_ex(NULL, 0, "Cannot recompose %s to a string", ZSTR_VAL(object->ce->name));
679+
zend_throw_exception_ex(uri_error_ce, 0, "Cannot recompose %s to a string", ZSTR_VAL(object->ce->name));
679680
}
680681

681682
static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, zend_object *that_object, zend_object *comparison_mode)
@@ -1105,6 +1106,7 @@ static PHP_MINIT_FUNCTION(uri)
11051106

11061107
uri_comparison_mode_ce = register_class_Uri_UriComparisonMode();
11071108
uri_exception_ce = register_class_Uri_UriException(zend_ce_exception);
1109+
uri_error_ce = register_class_Uri_UriError(zend_ce_error);
11081110
uri_invalid_uri_exception_ce = register_class_Uri_InvalidUriException(uri_exception_ce);
11091111
uri_whatwg_invalid_url_exception_ce = register_class_Uri_WhatWg_InvalidUrlException(uri_invalid_uri_exception_ce);
11101112
uri_whatwg_url_validation_error_ce = register_class_Uri_WhatWg_UrlValidationError();

ext/uri/php_uri.stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ class UriException extends \Exception
88
{
99
}
1010

11+
/** @strict-properties */
12+
class UriError extends \Error
13+
{
14+
}
15+
1116
/** @strict-properties */
1217
class InvalidUriException extends \Uri\UriException
1318
{

ext/uri/php_uri_arginfo.h

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/uri/php_uri_common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void uri_read_component(INTERNAL_FUNCTION_PARAMETERS, php_uri_property_name prop
5252
const php_uri_property_handler *property_handler = php_uri_parser_property_handler_by_name(internal_uri->parser, property_name);
5353

5454
if (UNEXPECTED(property_handler->read(internal_uri->uri, component_read_mode, return_value) == FAILURE)) {
55-
zend_throw_error(NULL, "The %s component cannot be retrieved", ZSTR_VAL(get_known_string_by_property_name(property_name)));
55+
zend_throw_exception_ex(uri_error_ce, 0, "The %s component cannot be retrieved", ZSTR_VAL(get_known_string_by_property_name(property_name)));
5656
RETURN_THROWS();
5757
}
5858
}

ext/uri/php_uri_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern zend_class_entry *uri_whatwg_url_ce;
2323
extern zend_object_handlers uri_whatwg_uri_object_handlers;
2424
extern zend_class_entry *uri_comparison_mode_ce;
2525
extern zend_class_entry *uri_exception_ce;
26+
extern zend_class_entry *uri_error_ce;
2627
extern zend_class_entry *uri_invalid_uri_exception_ce;
2728
extern zend_class_entry *uri_whatwg_invalid_url_exception_ce;
2829
extern zend_class_entry *uri_whatwg_url_validation_error_type_ce;

ext/uri/uri_parser_rfc3986.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static zend_result php_uri_parser_rfc3986_scheme_write(void *uri, zval *value, z
136136
return FAILURE;
137137
default:
138138
/* This should be unreachable in practice. */
139-
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the scheme", 0);
139+
zend_throw_exception(uri_error_ce, "Failed to update the scheme", 0);
140140
return FAILURE;
141141
}
142142
}
@@ -176,7 +176,7 @@ zend_result php_uri_parser_rfc3986_userinfo_write(void *uri, zval *value, zval *
176176
return FAILURE;
177177
default:
178178
/* This should be unreachable in practice. */
179-
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the userinfo", 0);
179+
zend_throw_exception(uri_error_ce, "Failed to update the userinfo", 0);
180180
return FAILURE;
181181
}
182182
}
@@ -271,7 +271,7 @@ static zend_result php_uri_parser_rfc3986_host_write(void *uri, zval *value, zva
271271
return FAILURE;
272272
default:
273273
/* This should be unreachable in practice. */
274-
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the host", 0);
274+
zend_throw_exception(uri_error_ce, "Failed to update the host", 0);
275275
return FAILURE;
276276
}
277277
}
@@ -331,7 +331,7 @@ static zend_result php_uri_parser_rfc3986_port_write(void *uri, zval *value, zva
331331
return FAILURE;
332332
default:
333333
/* This should be unreachable in practice. */
334-
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the port", 0);
334+
zend_throw_exception(uri_error_ce, "Failed to update the port", 0);
335335
return FAILURE;
336336
}
337337
}
@@ -383,7 +383,7 @@ static zend_result php_uri_parser_rfc3986_path_write(void *uri, zval *value, zva
383383
return FAILURE;
384384
default:
385385
/* This should be unreachable in practice. */
386-
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the path", 0);
386+
zend_throw_exception(uri_error_ce, "Failed to update the path", 0);
387387
return FAILURE;
388388
}
389389
}
@@ -420,7 +420,7 @@ static zend_result php_uri_parser_rfc3986_query_write(void *uri, zval *value, zv
420420
return FAILURE;
421421
default:
422422
/* This should be unreachable in practice. */
423-
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the query", 0);
423+
zend_throw_exception(uri_error_ce, "Failed to update the query", 0);
424424
return FAILURE;
425425
}
426426
}
@@ -457,7 +457,7 @@ static zend_result php_uri_parser_rfc3986_fragment_write(void *uri, zval *value,
457457
return FAILURE;
458458
default:
459459
/* This should be unreachable in practice. */
460-
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the fragment", 0);
460+
zend_throw_exception(uri_error_ce, "Failed to update the fragment", 0);
461461
return FAILURE;
462462
}
463463
}
@@ -484,7 +484,7 @@ php_uri_parser_rfc3986_uris *php_uri_parser_rfc3986_parse_ex(const char *uri_str
484484
break;
485485
default:
486486
/* This should be unreachable in practice. */
487-
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to parse the specified URI", 0);
487+
zend_throw_exception(uri_error_ce, "Failed to parse the specified URI", 0);
488488
break;
489489
}
490490
}
@@ -506,7 +506,7 @@ php_uri_parser_rfc3986_uris *php_uri_parser_rfc3986_parse_ex(const char *uri_str
506506
break;
507507
default:
508508
/* This should be unreachable in practice. */
509-
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to resolve the specified URI against the base URI", 0);
509+
zend_throw_exception(uri_error_ce, "Failed to resolve the specified URI against the base URI", 0);
510510
break;
511511
}
512512
}

0 commit comments

Comments
 (0)