From 52db0b4eb5534d263f715bc72bbb7be73bcd208d Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 19 Dec 2024 02:12:14 +0000 Subject: [PATCH] ext/intl: TimeZone address todo to throw exceptions on error. --- .../tests/dateformat_setTimeZone_error.phpt | 20 +++++++----- ext/intl/timezone/timezone_class.cpp | 31 +++---------------- 2 files changed, 17 insertions(+), 34 deletions(-) diff --git a/ext/intl/tests/dateformat_setTimeZone_error.phpt b/ext/intl/tests/dateformat_setTimeZone_error.phpt index 4467204134062..cafd342e3fb55 100644 --- a/ext/intl/tests/dateformat_setTimeZone_error.phpt +++ b/ext/intl/tests/dateformat_setTimeZone_error.phpt @@ -10,15 +10,19 @@ ini_set("date.timezone", 'Atlantic/Azores'); $df = new IntlDateFormatter(NULL, 0, 0); -var_dump($df->setTimeZone(array())); -var_dump($df->setTimeZone('non existing timezone')); +try { + $df->setTimeZone(array()); +} catch (Throwable $e) { + echo $e->getMessage() . PHP_EOL; +} +try { + $df->setTimeZone('non existing timezone'); +} catch (Throwable $e) { + echo $e->getMessage(); +} ?> --EXPECTF-- Warning: Array to string conversion in %s on line %d - -Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'Array' in %s on line %d -bool(false) - -Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'non existing timezone' in %s on line %d -bool(false) +datefmt_set_timezone: No such time zone: 'Array' +datefmt_set_timezone: No such time zone: 'non existing timezone' diff --git a/ext/intl/timezone/timezone_class.cpp b/ext/intl/timezone/timezone_class.cpp index f7b3d4eeb08f2..bba53c8753a17 100644 --- a/ext/intl/timezone/timezone_class.cpp +++ b/ext/intl/timezone/timezone_class.cpp @@ -147,24 +147,15 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone, instanceof_function(Z_OBJCE_P(zv_timezone), TimeZone_ce_ptr)) { TimeZone_object *to = Z_INTL_TIMEZONE_P(zv_timezone); - /* TODO Throw proper Error exceptions for uninitialized classes and failure to clone */ if (to->utimezone == NULL) { - spprintf(&message, 0, "%s: passed IntlTimeZone is not " + zend_throw_error(NULL, "%s: passed IntlTimeZone is not " "properly constructed", func); - if (message) { - intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1); - efree(message); - } zval_ptr_dtor_str(&local_zv_tz); return NULL; } timeZone = to->utimezone->clone(); if (UNEXPECTED(timeZone == NULL)) { - spprintf(&message, 0, "%s: could not clone TimeZone", func); - if (message) { - intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1); - efree(message); - } + zend_throw_error(NULL, "%s: could not clone TimeZone", func); zval_ptr_dtor_str(&local_zv_tz); return NULL; } @@ -185,32 +176,20 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone, } if (intl_stringFromChar(id, Z_STRVAL_P(zv_timezone), Z_STRLEN_P(zv_timezone), &status) == FAILURE) { - spprintf(&message, 0, "%s: Time zone identifier given is not a " + zend_throw_error(NULL, "%s: Time zone identifier given is not a " "valid UTF-8 string", func); - if (message) { - intl_errors_set(outside_error, status, message, 1); - efree(message); - } zval_ptr_dtor_str(&local_zv_tz); return NULL; } timeZone = TimeZone::createTimeZone(id); if (UNEXPECTED(timeZone == NULL)) { - spprintf(&message, 0, "%s: Could not create time zone", func); - if (message) { - intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1); - efree(message); - } + zend_throw_error(NULL, "%s: Could not create time zone", func); zval_ptr_dtor_str(&local_zv_tz); return NULL; } if (*timeZone == TimeZone::getUnknown()) { - spprintf(&message, 0, "%s: No such time zone: '%s'", + zend_throw_error(NULL, "%s: No such time zone: '%s'", func, Z_STRVAL_P(zv_timezone)); - if (message) { - intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1); - efree(message); - } zval_ptr_dtor_str(&local_zv_tz); delete timeZone; return NULL;