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

feat: Added enumeration of HTTP response status codes #303

Merged
merged 15 commits into from
Jul 15, 2024

Conversation

mpa12
Copy link
Contributor

@mpa12 mpa12 commented Jul 9, 2024

Summary

Added enumeration of HTTP response status codes

Resolves #59

Type of change

  • New feature (added a non-breaking change which adds functionality)

How Has This Been Tested?

  • Ran tests with composer test

@DenverCoder1
Copy link
Owner

This is fine so far, though the main thing to resolve for this issue is to make our server return different response codes for invalid url combinations.

Currently every combination of parameters gives a 200 response, but there are some possible error cases where it is not "ok" and should set the http response code to something else.

For example:

@mpa12
Copy link
Contributor Author

mpa12 commented Jul 10, 2024

@DenverCoder1 As I understand it, you need query validation. In addition to statuses, maybe it can also return the error text? For example, if some parameter is missing, then write Missing parameter <parameter_name>

@DenverCoder1
Copy link
Owner

It already displays error messages, it just doesn't have different status codes for different errors, for example, this page shows the response code is 200, when it would be more accurate to say 422 since there was an error

image

One possible way to do this could be to pass the codes to the exceptions, and call http_response_code on the error's code before rendering an error svg.

This is a PR to a similar issue in a different repository for reference: https://github.com/DenverCoder1/github-readme-streak-stats/pull/192/files

@mpa12
Copy link
Contributor Author

mpa12 commented Jul 12, 2024

I didn't display an error message when I failed to get the font. This could cause animations to crash for some users. In the current version, if you can't get the desired font, the default font is used

@mpa12
Copy link
Contributor Author

mpa12 commented Jul 12, 2024

image

@DenverCoder1
Copy link
Owner

Thanks so much for all the work you've put in to solving this!

While this does work, as far as the implementation, there are a few things I'd do differently for code readability and maintainability.

Constructors should not typically have side effects, in other words, using the construction of the exception to change the response code is unexpected and as a principle should be avoided.

I think the most expected place to find this would be in the RenderedController class when setting the headers.

  • A $statusCode field could be added to RendererController defaulting to 200, but set in the constructor's catch block if an exception raised.
  • The status code field should be set based on the exception for extensibility. For example, using the exception code. If it's not set (i.e. it's 0), the status code could be set to 500 as that would indicate an unknown server error occurred. Alternatively, the class of the exception could be checked.
  • In the setHeaders() method of RendererController which is called from index.php, the http_status_code() function can be used to set the status code using the value in the status code field.

Feel free to let me know your thoughts and if you have any questions.

Thanks

@mpa12
Copy link
Contributor Author

mpa12 commented Jul 14, 2024

Quite a good point! I'll fix it

@DenverCoder1 DenverCoder1 temporarily deployed to typing-svg-main-svwr65eek6dpie July 15, 2024 09:24 Inactive
@DenverCoder1 DenverCoder1 temporarily deployed to typing-svg-main-epr8a90uyt1u4p July 15, 2024 09:32 Inactive
Copy link
Owner

@DenverCoder1 DenverCoder1 left a comment

Choose a reason for hiding this comment

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

Thanks for the continued work, looks great so far

src/exceptions/InvalidException.php Outdated Show resolved Hide resolved
src/models/RendererModel.php Outdated Show resolved Hide resolved
src/models/RendererModel.php Outdated Show resolved Hide resolved
src/models/RendererModel.php Outdated Show resolved Hide resolved
src/controllers/RendererController.php Outdated Show resolved Hide resolved
composer.json Show resolved Hide resolved
@mpa12
Copy link
Contributor Author

mpa12 commented Jul 15, 2024

Thank you for your attention to my work and valuable comments! I took all your recommendations into account and corrected the errors

@DenverCoder1 DenverCoder1 temporarily deployed to typing-svg-main-epr8a90uyt1u4p July 15, 2024 10:21 Inactive
Copy link
Owner

@DenverCoder1 DenverCoder1 left a comment

Choose a reason for hiding this comment

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

Looks perfect! Thanks for the contribution 🎉

@DenverCoder1 DenverCoder1 changed the title feat: Added enumeration of HTTP response status codes (#59) feat: Added enumeration of HTTP response status codes Jul 15, 2024
@DenverCoder1 DenverCoder1 merged commit 80b56b2 into DenverCoder1:main Jul 15, 2024
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.

Return proper HTTP response codes for errors
2 participants