Skip to content
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

Merged
merged 7 commits into from May 26, 2018
Merged

Error messages. #25

merged 7 commits into from May 26, 2018

Conversation

ugnelis
Copy link
Owner

@ugnelis ugnelis commented May 22, 2018

Added functionality for handling errors. Solves #13.

@coveralls
Copy link

coveralls commented May 22, 2018

Pull Request Test Coverage Report for Build 98

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 86: 0.0%
Covered Lines: 99
Relevant Lines: 99

💛 - Coveralls

@@ -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();

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.

try {
outputString = api->translate(inputString, sourceLanguage, targetLanguage);
} catch (const InvalidArgumentException &e) {
showErrorBox("Text cannot be translated. Invalid API key.");

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.

Copy link
Owner Author

@ugnelis ugnelis May 25, 2018

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); }

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 }.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

@Edvinas01 Edvinas01 May 25, 2018

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.

std::string errorMessage = errorJsonObject["message"]
.toString()
.toStdString();
DLOG(INFO) << "Error " + std::to_string(errorCode) + ": " + errorMessage;

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; }

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.

Copy link
Owner Author

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.

@ugnelis ugnelis merged commit c7681cd into master May 26, 2018
@ugnelis ugnelis mentioned this pull request May 27, 2018
@ugnelis ugnelis deleted the 13-error-messages branch June 11, 2018 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants