Solution:
You should detect the error before do anything.
Turn on WP debug mode and you will see what error do you have:
In wp-config.php:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );
More information about WP debugging: https://wordpress.org/support/article/debugging-in-wordpress/