Skip to content

Refactor BasePublicController to reduce duplicated code#3996

Merged
dartcafe merged 7 commits into
nextcloud:mainfrom
GeorgeApos:improve-duplication
May 14, 2025
Merged

Refactor BasePublicController to reduce duplicated code#3996
dartcafe merged 7 commits into
nextcloud:mainfrom
GeorgeApos:improve-duplication

Conversation

@GeorgeApos
Copy link
Copy Markdown
Contributor

While running a SonarQube scan on the project, I noticed that BasePublicController had 62.5% duplicated lines, primarily due to repeated try/catch logic in the response(), responseCreate(), and responseLong() methods.

This PR refactors the class by introducing a single private method, handleResponse(), which centralizes the core response logic while remaining flexible for different status codes and exception types.

The result is cleaner, more maintainable code that remains functionally identical.

@dartcafe
Copy link
Copy Markdown
Collaborator

dartcafe commented May 3, 2025

Nice. One item of the I-have-to-look-after-that-later-list. Do you think, you could the other base controllers as well?

And you should sign off the commits.

@dartcafe dartcafe self-requested a review May 3, 2025 19:36
Comment thread lib/Controller/BasePublicController.php Outdated
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}
return new JSONResponse(['message' => 'Unexpected error'], Http::STATUS_INTERNAL_SERVER_ERROR);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am unsure, but I think 5xx is handled by the server itself and this way we would lose the trace log.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right that catching \Throwable and returning a custom 5xx response might suppress the server's own error handling and logging. To avoid losing the trace log, I could either rethrow the exception after logging it, or limit the catch block to Exception (not \Throwable) and let fatal errors bubble up as usual.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Better rethrow it IMHO, so this code is fine as it is.

@GeorgeApos
Copy link
Copy Markdown
Contributor Author

Nice. One item of the I-have-to-look-after-that-later-list. Do you think, you could the other base controllers as well?

And you should sign off the commits.

I’ve refactored the other base controllers in the same way and signed off the commits.

@dartcafe dartcafe self-requested a review May 4, 2025 16:40
Copy link
Copy Markdown
Collaborator

@dartcafe dartcafe left a comment

Choose a reason for hiding this comment

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

First quick look. Feel free to comment

Comment thread lib/Controller/BaseApiV1Controller.php Outdated
Comment thread lib/Controller/BaseApiV1Controller.php Outdated
Comment thread lib/Controller/BaseApiV2Controller.php Outdated
Comment thread lib/Controller/BaseApiV2Controller.php Outdated
Comment thread lib/Controller/BaseApiV1Controller.php Outdated
Comment thread lib/Controller/BaseController.php Outdated
Comment thread lib/Controller/BasePublicController.php Outdated
Signed-off-by: George - David Apostolidis <geoapostolidis999@gmail.com>
@GeorgeApos
Copy link
Copy Markdown
Contributor Author

I made the changes you suggested. Apologies in advance if anything isnt aligned with your expectations, this is my first open-source contribution

@dartcafe dartcafe self-requested a review May 6, 2025 14:58
Signed-off-by: dartcafe <github@dartcafe.de>
@dartcafe
Copy link
Copy Markdown
Collaborator

@GeorgeApos

Apologies in advance if anything isnt aligned with your expectations, this is my first open-source contribution

😆 Don't care. There is no need for excuse. It is the way it is done; Everything is perfect.

But sorry, I hijacked your PR, but while reviewing it, my ambitions raised to optimize it further and it was more straight forward to implement rather than explain.

What I did:

  • Reduce all response calls to a single method per controller
    • Detect a long poll by the exception status (304)
    • Eliminate BaseController::responseDeleteTolerant() by defining the new Exception OCA\Polls\Exceptions\AlreadyDeletedException using 304 as Status as well
    • use your attempt to define the successCode but moved it to the Controllers as a parameter and delete responseCreate; Set 200 as default

As result:

  • eliminate the BasePublicController and use the BaseController as parent class for the public controllers since there was no more difference

Further:

  • Catch OCA\Polls\Exceptions\Exception instead of \Exception
  • remove BaseApiV1Controller, as it is no more used
  • removed leftover catching of \Throwable

As the result the controller should behave as before, but with much more leaner code. You had a nice attempt 👍

Tell me what you think of the changes, I made.

@dartcafe
Copy link
Copy Markdown
Collaborator

dartcafe commented May 14, 2025

Hey @GeorgeApos

Sorry, I don't want to rush you, you may have things to do for sure. But I am going to merge, because I want to avoid further merge conflicts.

Any occurring issues may be solved in a different fix. I reviewed the code several time, so I do not expect any problems.

And thank you for your support. That helped a lot. Any participation is welcome.

@dartcafe dartcafe merged commit 1de5a7a into nextcloud:main May 14, 2025
45 checks passed
@dartcafe dartcafe removed their request for review May 14, 2025 18:14
@GeorgeApos
Copy link
Copy Markdown
Contributor Author

Hey @GeorgeApos

Sorry, I don't want to rush you, you may have things to do for sure. But I am going to merge, because I want to avoid further merge conflicts.

Any occurring issues may be solved in a different fix. I reviewed the code several time, so I do not expect any problems.

And thank you for your support. That helped a lot. Any participation is welcome.

Hello, yeah of course, I saw the code, it turns out when you know the code base, you know how to make it perfect! Thank you for the merge and your help, if I find more problems I will point them out!

@github-actions
Copy link
Copy Markdown

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process.

Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6

Thank you for contributing to Nextcloud and we hope to hear from you soon!

(If you believe you should not receive this message, you can add yourself to the blocklist.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants