From 006260f07a49f74b1fe8fa7be39b0d34d14b8067 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Tue, 9 Mar 2010 05:19:20 +0000 Subject: [PATCH] THRIFT-659. php: Make php_thrift_protocol handle std::exception cleanly Any std::exceptions thrown from within the extension will now be converted into php exceptions. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@920662 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/ext/thrift_protocol/php_thrift_protocol.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/php/src/ext/thrift_protocol/php_thrift_protocol.cpp b/lib/php/src/ext/thrift_protocol/php_thrift_protocol.cpp index 6844fb30..e6ba94a9 100644 --- a/lib/php/src/ext/thrift_protocol/php_thrift_protocol.cpp +++ b/lib/php/src/ext/thrift_protocol/php_thrift_protocol.cpp @@ -399,6 +399,12 @@ void throw_tprotocolexception(char* what, long errorcode) { throw PHPExceptionWrapper(ex); } +// Sets EG(exception), call this and then RETURN_NULL(); +void throw_zend_exception_from_std_exception(const std::exception& ex) { + zend_throw_exception(zend_exception_get_default(TSRMLS_CC), const_cast(ex.what()), 0 TSRMLS_CC); +} + + void binary_deserialize(int8_t thrift_typeID, PHPInputTransport& transport, zval* return_value, HashTable* fieldspec) { zval** val_ptr; Z_TYPE_P(return_value) = IS_NULL; // just in case @@ -927,6 +933,9 @@ PHP_FUNCTION(thrift_protocol_write_binary) { } catch (const PHPExceptionWrapper& ex) { zend_throw_exception_object(ex TSRMLS_CC); RETURN_NULL(); + } catch (const std::exception& ex) { + throw_zend_exception_from_std_exception(ex); + RETURN_NULL(); } } @@ -1000,6 +1009,9 @@ PHP_FUNCTION(thrift_protocol_read_binary) { } catch (const PHPExceptionWrapper& ex) { zend_throw_exception_object(ex TSRMLS_CC); RETURN_NULL(); + } catch (const std::exception& ex) { + throw_zend_exception_from_std_exception(ex); + RETURN_NULL(); } } -- 2.17.1