You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by mreve <gi...@git.apache.org> on 2015/04/29 00:27:34 UTC
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
GitHub user mreve opened a pull request:
https://github.com/apache/thrift/pull/469
Thrift 727 - support C++ native exception message
Added code generating implementation of std::exception::what() function for exceptions.
I needed to modify the tutorial's IDL file, as well as CppClient and CppServer files, because member field of the exception defined & used there is named "what", and that made the compiler complain.
The modification in CppClient contains only a comment - it can be uncommented and used as a test, to see the result of my change.
Please view the discussion in Jira:
https://issues.apache.org/jira/browse/THRIFT-727
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/mreve/thrift THRIFT-727
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/thrift/pull/469.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #469
----
commit 8ca78b09e4540cd89f87bbb6ecd9854e5e5d4acd
Author: mreve <aa...@gmail.com>
Date: 2015-04-28T21:54:37Z
Change InvalidOperation field "what" to "what_op"
Because it can't coexist with generated method what().
commit 5cfd3c82095f8499f87c17a245e6d06a6d7d77ac
Author: mreve <aa...@gmail.com>
Date: 2015-04-28T21:57:24Z
Update CppClient.cpp
Added a comment that suggests using cpp-native std::exception::what() method to obtain information about an error.
commit e49f5ce883e879e6796f8992c6550eaa33fef298
Author: mreve <aa...@gmail.com>
Date: 2015-04-28T21:59:29Z
Use the right InvalidOperation member field name
Change from "what" to "what_op" as what is now a function.
commit 73f8240b65a5eebd8ee54ef5df2e08325ee98361
Author: mreve <aa...@gmail.com>
Date: 2015-04-28T22:09:44Z
Generate std::exception::what() implementation for exceptions
commit 160528fb5db2388aaec8b0efede6023be12dd41c
Author: mreve <aa...@gmail.com>
Date: 2015-04-28T22:17:50Z
Fix the typo
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by mreve <gi...@git.apache.org>.
Github user mreve commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29555100
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1699,6 +1723,24 @@ void t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out, t_str
out << "}" << endl << endl;
}
+/**
+ * Generates what() method for exceptions
+ */
+void t_cpp_generator::generate_exception_what_method(std::ofstream& out, t_struct* tstruct) {
+ out << indent();
+ generate_exception_what_method_decl(out, tstruct, true);
+ out << " {" << endl;
+
+ indent_up();
+
+ out << indent() << "std::stringstream ss;" << endl;
+ out << indent() << "ss << \"TException - service has thrown: \" << *this;" << endl;
+ out << indent() << "return ss.str().c_str();" << endl;
--- End diff --
I can't find where & how to add unit test. Would you mind giving me any hint on that subject, or pointing me to the right resources?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by hcorg <gi...@git.apache.org>.
Github user hcorg commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29297804
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1626,6 +1640,16 @@ void t_cpp_generator::generate_struct_ostream_operator_decl(std::ofstream& out,
out << "std::ostream& operator<<(std::ostream& out, const " << tstruct->get_name() << "& obj)";
}
+void t_cpp_generator::generate_exception_what_method_decl(std::ofstream& out,
+ t_struct* tstruct,
+ bool external) {
+ std::string prefix = "";
+ if (external) {
+ prefix = string(tstruct->get_name() + "::");
+ }
+ out << "const char* " << prefix << "what() const throw()";
--- End diff --
throw() is deprecated (especially that you concatenate strings there and you cannot guarantee "no throw" behavior)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by hcorg <gi...@git.apache.org>.
Github user hcorg commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29373647
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1699,6 +1723,24 @@ void t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out, t_str
out << "}" << endl << endl;
}
+/**
+ * Generates what() method for exceptions
+ */
+void t_cpp_generator::generate_exception_what_method(std::ofstream& out, t_struct* tstruct) {
+ out << indent();
+ generate_exception_what_method_decl(out, tstruct, true);
+ out << " {" << endl;
+
+ indent_up();
+
+ out << indent() << "std::stringstream ss;" << endl;
+ out << indent() << "ss << \"TException - service has thrown: \" << *this;" << endl;
+ out << indent() << "return ss.str().c_str();" << endl;
--- End diff --
this can cause Segmenation Fault, as ss will be destroyed on function exit but pointer to it external contents is returned...
Please do try making a unit test for that functionality
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by mreve <gi...@git.apache.org>.
Github user mreve commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29554836
--- Diff: tutorial/tutorial.thrift ---
@@ -113,7 +113,7 @@ struct Work {
* Structs can also be exceptions, if they are nasty.
*/
exception InvalidOperation {
- 1: i32 what,
+ 1: i32 what_op,
--- End diff --
done.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by hcorg <gi...@git.apache.org>.
Github user hcorg commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29559168
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1699,6 +1723,24 @@ void t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out, t_str
out << "}" << endl << endl;
}
+/**
+ * Generates what() method for exceptions
+ */
+void t_cpp_generator::generate_exception_what_method(std::ofstream& out, t_struct* tstruct) {
+ out << indent();
+ generate_exception_what_method_decl(out, tstruct, true);
+ out << " {" << endl;
+
+ indent_up();
+
+ out << indent() << "std::stringstream ss;" << endl;
+ out << indent() << "ss << \"TException - service has thrown: \" << *this;" << endl;
+ out << indent() << "return ss.str().c_str();" << endl;
--- End diff --
I think mutable std::string field named texceptionMessageHolder (or even longer name) is a good solution - mutable s a keyword so it's not that much a trick - we still do not change exception contents, but helper field.
It seems very unlikely (and quite ugly) for someone to store result of what() as a const char* and call what() again... "what()" is usually used for logging, and as such is rarely called twice, nor someone stores it direct result.
Whenever in doubt - choose std::string over const char* - it's better to have handle object to manage memory etc. In very rare situations memory array may be better (efficiency, memory consumption etc.) yet it has to be first proven to be so.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by hcorg <gi...@git.apache.org>.
Github user hcorg commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29555269
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1699,6 +1723,24 @@ void t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out, t_str
out << "}" << endl << endl;
}
+/**
+ * Generates what() method for exceptions
+ */
+void t_cpp_generator::generate_exception_what_method(std::ofstream& out, t_struct* tstruct) {
+ out << indent();
+ generate_exception_what_method_decl(out, tstruct, true);
+ out << " {" << endl;
+
+ indent_up();
+
+ out << indent() << "std::stringstream ss;" << endl;
+ out << indent() << "ss << \"TException - service has thrown: \" << *this;" << endl;
+ out << indent() << "return ss.str().c_str();" << endl;
--- End diff --
see how unit testing of generated code is done in lib/cpp/test
for example: https://github.com/apache/thrift/blob/master/lib/cpp/test/ToStringTest.cpp
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by mreve <gi...@git.apache.org>.
Github user mreve commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29554841
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1626,6 +1640,16 @@ void t_cpp_generator::generate_struct_ostream_operator_decl(std::ofstream& out,
out << "std::ostream& operator<<(std::ostream& out, const " << tstruct->get_name() << "& obj)";
}
+void t_cpp_generator::generate_exception_what_method_decl(std::ofstream& out,
+ t_struct* tstruct,
+ bool external) {
+ std::string prefix = "";
+ if (external) {
+ prefix = string(tstruct->get_name() + "::");
+ }
+ out << "const char* " << prefix << "what() const throw()";
--- End diff --
It may be, but TException implements what() using this definition. Removing "throw()" results in compilation error: "overriding 'virtual const char* apache::thrift::TException::what() const throw ()'".
However, I can just implement it without concatenating strings specifically.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by mreve <gi...@git.apache.org>.
Github user mreve commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29554828
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1626,6 +1640,16 @@ void t_cpp_generator::generate_struct_ostream_operator_decl(std::ofstream& out,
out << "std::ostream& operator<<(std::ostream& out, const " << tstruct->get_name() << "& obj)";
}
+void t_cpp_generator::generate_exception_what_method_decl(std::ofstream& out,
+ t_struct* tstruct,
+ bool external) {
+ std::string prefix = "";
+ if (external) {
+ prefix = string(tstruct->get_name() + "::");
+ }
+ out << "const char* " << prefix << "what() const throw()";
--- End diff --
It may be, but TException implements what() using this definition. Removing "throw()" results in compilation error: "overriding 'virtual const char* apache::thrift::TException::what() const throw ()'".
However, I believe I can just implement it without concatenating strings specifically.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by hcorg <gi...@git.apache.org>.
Github user hcorg commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29963417
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1699,6 +1723,24 @@ void t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out, t_str
out << "}" << endl << endl;
}
+/**
+ * Generates what() method for exceptions
+ */
+void t_cpp_generator::generate_exception_what_method(std::ofstream& out, t_struct* tstruct) {
+ out << indent();
+ generate_exception_what_method_decl(out, tstruct, true);
+ out << " {" << endl;
+
+ indent_up();
+
+ out << indent() << "std::stringstream ss;" << endl;
+ out << indent() << "ss << \"TException - service has thrown: \" << *this;" << endl;
+ out << indent() << "return ss.str().c_str();" << endl;
--- End diff --
C++11 will be covered with cpp_v2 lib, which will generate this method with noexcept.
Yet stringstream and string does not give noexcept guarantee (for example - they probably allocate memory). So this will require some more thought in C++11
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by hcorg <gi...@git.apache.org>.
Github user hcorg commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29297937
--- Diff: tutorial/tutorial.thrift ---
@@ -113,7 +113,7 @@ struct Work {
* Structs can also be exceptions, if they are nasty.
*/
exception InvalidOperation {
- 1: i32 what,
+ 1: i32 what_op,
--- End diff --
1. this naming convention is inconsistent with rest of tutorial.thrift file (camelCase)
2. changing it here requires update in all tutorials, not only C++
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by mreve <gi...@git.apache.org>.
Github user mreve commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29559030
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1699,6 +1723,24 @@ void t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out, t_str
out << "}" << endl << endl;
}
+/**
+ * Generates what() method for exceptions
+ */
+void t_cpp_generator::generate_exception_what_method(std::ofstream& out, t_struct* tstruct) {
+ out << indent();
+ generate_exception_what_method_decl(out, tstruct, true);
+ out << " {" << endl;
+
+ indent_up();
+
+ out << indent() << "std::stringstream ss;" << endl;
+ out << indent() << "ss << \"TException - service has thrown: \" << *this;" << endl;
+ out << indent() << "return ss.str().c_str();" << endl;
--- End diff --
Hey, thanks for help! However, before I begin to write it - I believe it can cause sometimes SegFault even if it would pass the unit test. And, what more, we'll encounter this problem no matter what we return here (any string created in what() will be destroyed the same way as the ss, won't it?).
So, the only way to make it work is to store the returned text in a field of the custom exception class, am I right? Then we can return a pointer to this field, and it will remain until the exception is not destroyed. BUT - what is a const function. So, I can't modify the object itself inside what().
There is a tricky way to get around it...we can keep a mutable variable as a data member field. Then in what() we can generate the message, store it in mutable field (which we can modify no matter const function), and return the pointer to this field. But again, if somebody calls what() twice, the second call will override the message so the first obtained pointer can become invalid... And another thing, this mutable field, what type should it be? String, and we'd return .c_str() from it?
What do you think would be a solution? I understand that the scenarios I'm talking about aren't very typical for the exceptions, but I think they still should be discussed...
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by jeking3 <gi...@git.apache.org>.
Github user jeking3 commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29955740
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1699,6 +1723,24 @@ void t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out, t_str
out << "}" << endl << endl;
}
+/**
+ * Generates what() method for exceptions
+ */
+void t_cpp_generator::generate_exception_what_method(std::ofstream& out, t_struct* tstruct) {
+ out << indent();
+ generate_exception_what_method_decl(out, tstruct, true);
+ out << " {" << endl;
+
+ indent_up();
+
+ out << indent() << "std::stringstream ss;" << endl;
+ out << indent() << "ss << \"TException - service has thrown: \" << *this;" << endl;
+ out << indent() << "return ss.str().c_str();" << endl;
--- End diff --
You could store the content of the string in a std::string member variable on construction then return mString.c_str(), holding the string variable. The specification (even C++11) is very flimsy about specifying the valid lifetime of this pointer.
Also we will need to revisit this when we enable a feature (documented in other Jira tickets) to set the minimum required C++ version of the resulting generated code. Before C++11, what is specified as throw(), with C++11 this changes to noexcept;
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by jeking3 <gi...@git.apache.org>.
Github user jeking3 commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29954704
--- Diff: tutorial/tutorial.thrift ---
@@ -113,7 +113,7 @@ struct Work {
* Structs can also be exceptions, if they are nasty.
*/
exception InvalidOperation {
- 1: i32 what,
+ 1: i32 whatOp,
--- End diff --
"what" needs to be added to the compiler's list of keywords as part of this work.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/thrift/pull/469
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by hcorg <gi...@git.apache.org>.
Github user hcorg commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29963228
--- Diff: tutorial/tutorial.thrift ---
@@ -113,7 +113,7 @@ struct Work {
* Structs can also be exceptions, if they are nasty.
*/
exception InvalidOperation {
- 1: i32 what,
+ 1: i32 whatOp,
--- End diff --
no, it should not - as a rule: Thrift does not keep track of language specific keywords.
This decision was made some time ago mostly because of two reasons:
- with current language and platform coverage list of such keywords would be very long and hard to maintain
- someone using Thrift to connect Java with Python should not be bothered with keywords coming from C++ library specifics
Proper solution was proposed: each generator should provide list of keywords extending "core Thrift keywords" and when generating code should check identifiers against those keywords (by some generic mechanisms). That would require some refactoring of generators in compiler, yet maybe someday..
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] thrift pull request: Thrift 727 - support C++ native exception mes...
Posted by jeking3 <gi...@git.apache.org>.
Github user jeking3 commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29992107
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1699,6 +1723,24 @@ void t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out, t_str
out << "}" << endl << endl;
}
+/**
+ * Generates what() method for exceptions
+ */
+void t_cpp_generator::generate_exception_what_method(std::ofstream& out, t_struct* tstruct) {
+ out << indent();
+ generate_exception_what_method_decl(out, tstruct, true);
+ out << " {" << endl;
+
+ indent_up();
+
+ out << indent() << "std::stringstream ss;" << endl;
+ out << indent() << "ss << \"TException - service has thrown: \" << *this;" << endl;
+ out << indent() << "return ss.str().c_str();" << endl;
--- End diff --
So in this case you would prefer a buffer allocated in the constructor, filled in, so what() cannot throw - sounds fair.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---