New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error messages. #25
Error messages. #25
Conversation
src/apis/googleapi.cpp
Outdated
@@ -47,6 +47,11 @@ QString GoogleAPI::translate(const QString &input, | |||
QJsonDocument replyJsonDocument = QJsonDocument::fromJson(replyByteArray.data()); | |||
QJsonObject replyJsonObject = replyJsonDocument.object(); | |||
|
|||
// If error. | |||
if (replyJsonObject.contains("error")) { | |||
throw InvalidArgumentException(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to pass error
into InvalidArgumentException
.
src/mainwindow.cpp
Outdated
try { | ||
outputString = api->translate(inputString, sourceLanguage, targetLanguage); | ||
} catch (const InvalidArgumentException &e) { | ||
showErrorBox("Text cannot be translated. Invalid API key."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use message added into InvalidArgumentException
and add it to showErrorBox
as additional details.
Same with other instances of this exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a bit in a different way.
public: | ||
void raise() const { throw *this; } | ||
|
||
InvalidArgumentException *clone() const { return new InvalidArgumentException(*this); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return new...
should be in new line as well as ending bracket }
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for inline
functions, while this one is not an inline
function, its a stateful method.
src/apis/googleapi.cpp
Outdated
std::string errorMessage = errorJsonObject["message"] | ||
.toString() | ||
.toStdString(); | ||
DLOG(INFO) << "Error " + std::to_string(errorCode) + ": " + errorMessage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to use a single exception handler instead of littering code with logs. Also once you start reading the stack trace of InvalidArgumentException
, you wont notice this log line. What you'll see will be InvalidArgumentException
with no context. Due to this you'll have to look for additional log messages which will require a lot more time in order to get more details on what caused this issue.
Thats why InvalidArgumentException
should have a message inside it.
@@ -8,9 +8,9 @@ | |||
*/ | |||
class InvalidArgumentException : public QException { | |||
public: | |||
void raise() const { throw *this; } | |||
inline void raise() const { throw *this; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure these should be marked as inline
? Why not just use the regular way throw InvalidArgumentException
- what essentially is done by using inline
?
Seems like a very funky way to throw exceptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Specifying inline encourages the compiler to do a better job." from http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-inline.
Added functionality for handling errors. Solves #13.