Refactor BasePublicController to reduce duplicated code#3996
Conversation
|
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. |
| return new JSONResponse(['message' => $e->getMessage()], $e->getStatus()); | ||
| } | ||
| } | ||
| return new JSONResponse(['message' => 'Unexpected error'], Http::STATUS_INTERNAL_SERVER_ERROR); |
There was a problem hiding this comment.
I am unsure, but I think 5xx is handled by the server itself and this way we would lose the trace log.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Better rethrow it IMHO, so this code is fine as it is.
Signed-off-by: George - David Apostolidis <geoapostolidis999@gmail.com>
I’ve refactored the other base controllers in the same way and signed off the commits. |
dartcafe
left a comment
There was a problem hiding this comment.
First quick look. Feel free to comment
Signed-off-by: George - David Apostolidis <geoapostolidis999@gmail.com>
|
I made the changes you suggested. Apologies in advance if anything isnt aligned with your expectations, this is my first open-source contribution |
Signed-off-by: dartcafe <github@dartcafe.de>
😆 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:
As result:
Further:
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. |
|
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! |
|
Hello there, 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.) |
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.