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

Add Exception Handling for jsonOptions() Method #53056

Conversation

shamimulalam
Copy link
Contributor

@shamimulalam shamimulalam commented Oct 8, 2024

This PR improves the jsonOptions() method by adding error handling for potential \ReflectionException cases. Previously, if the $collects class was not defined or could not be instantiated via reflection, the method would throw an unhandled exception. This could lead to unexpected application crashes.

Changes could be make:

Wrapped the reflection logic in a try-catch block to catch and handle \ReflectionException.

Motivation:

This update ensures that the application can handle scenarios where reflection fails without causing a hard failure in the system. By returning a default value, the method can still provide a consistent response, preventing runtime errors.

@crynobone
Copy link
Member

Wrapped the reflection logic in a try-catch block to catch and handle \ReflectionException.

Wouldn't this the better options?

@shamimulalam
Copy link
Contributor Author

Wrapped the reflection logic in a try-catch block to catch and handle \ReflectionException.

Wouldn't this the better options?

Definitely that is :)

@crynobone
Copy link
Member

    return rescue(function () use ($collects) {
        return (new ReflectionClass($collects))
              ->newInstanceWithoutConstructor()
              ->jsonOptions();
    }, 0);

This would ensure jsonOptions() return 0 as if $collects is empty and still log the exception for further handling.

@crynobone crynobone self-requested a review October 8, 2024 12:22
@taylorotwell taylorotwell merged commit 0a6b981 into laravel:11.x Oct 8, 2024
33 checks passed
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.

3 participants