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

refactor: append return statements after response callback #218

Merged
merged 3 commits into from
Jun 25, 2020

Conversation

kylerwsm
Copy link
Contributor

@kylerwsm kylerwsm commented Jun 24, 2020

Problem

Some of the response statements are not followed by a return statement.

Solution

  • Append return statements to the next line of response statements.
  • Also added explicit status code to the existing res.json statement.
  • Disabled an Eslint rule that removes return statements which it deems unnecessary.

@JasonChong96
Copy link
Contributor

Technically in those cases the function call ends after the response statements. But I think its an improvement to have the explicit return statements to prevent bugs in case anyone adds any lines after.

@kylerwsm
Copy link
Contributor Author

kylerwsm commented Jun 24, 2020

@JasonChong96 I agree. Additionally, this issue might had been exacerbated by eslint, which right now removes return statements it deems unnecessary. This creates problems as we need to make conscious efforts to remember to add the return statements back when it becomes less unnecessary. I talked to @liangyuanruo about it, and am working on removing that eslint check.

Edit: After a closer inspection, not all function calls ends after the response statements. An example is LoginController. A double response can be happen if an error is thrown when generating an otp.

@kylerwsm kylerwsm requested a review from liangyuanruo June 24, 2020 10:12
@yong-jie
Copy link
Member

fired gitpod and did a grep project-wide grep check. confirmed that all relevant res.status and res.json, res.ok, res.badRequest etc calls now have the return statement

Comment on lines +77 to +82
if (error instanceof NotFoundError) {
res.unauthorized(jsonMessage('OTP expired/not found.'))
} else {
res.serverError(jsonMessage(error.message))
return
}
res.serverError(jsonMessage(error.message))
return
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should retain the else-if and else blocks here for greater code clarity.

Comment on lines +97 to +98
res.notFound(jsonMessage('User session not found'))
return
Copy link
Contributor

Choose a reason for hiding this comment

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

similarly here

Comment on lines +86 to +87
res.status(302).redirect(longUrl)
return
Copy link
Contributor

Choose a reason for hiding this comment

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

and here

Comment on lines +53 to +59
if (error instanceof AlreadyExistsError) {
res.badRequest(jsonMessage(error.message, MessageType.ShortUrlError))
} else {
logger.error(`Error creating short URL:\t${error}`)
res.badRequest(jsonMessage('Server error.'))
return
}
logger.error(`Error creating short URL:\t${error}`)
res.badRequest(jsonMessage('Server error.'))
return
Copy link
Contributor

Choose a reason for hiding this comment

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

and here

Comment on lines +93 to +95
logger.error(`Error editing URL:\t${error}`)
res.badRequest(jsonMessage(`Unable to edit short link "${shortUrl}"`))
return
Copy link
Contributor

Choose a reason for hiding this comment

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

and here

Comment on lines +122 to +128
if (error instanceof AlreadyOwnLinkError) {
res.badRequest(jsonMessage(error.message))
} else {
logger.error(`Error transferring ownership of short URL:\t${error}`)
res.badRequest(jsonMessage('An error has occured'))
return
}
logger.error(`Error transferring ownership of short URL:\t${error}`)
res.badRequest(jsonMessage('An error has occured'))
return
Copy link
Contributor

Choose a reason for hiding this comment

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

and here

Comment on lines +170 to +171
res.serverError(jsonMessage('Error retrieving URLs for user'))
return
Copy link
Contributor

Choose a reason for hiding this comment

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

and here

Copy link
Contributor

@liangyuanruo liangyuanruo left a comment

Choose a reason for hiding this comment

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

using else if may not change code behavior, but it will help developers grasp the branching pattern immediately.

@kylerwsm
Copy link
Contributor Author

Discussed with @liangyuanruo that the removal of else and else-if statements is because of an ESLint rule, and we will be keeping the rule for now.

@kylerwsm kylerwsm merged commit 4a7bf91 into develop Jun 25, 2020
@kylerwsm kylerwsm deleted the res-return branch June 25, 2020 03:59
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.

4 participants