What is the best way to handle errors in a shortcode? For example:
public function handleCode($atts, $content)
{
//Get attributes
extract( shortcode_atts( array( "type" => "good" ), $atts ) );
//We only accept type of "good" or "bad"
if ( $type !== "good" && $type !== "bad" )
{
//Throw?
//Return null?
}
}
What is the proper way to let the PHP client/user know an error occurred?
I've been redirected on this page as I asked the same question and hadn't found this answer.
Looking at the accepted answer, I don't quite agree but it lets me figure out what to do. (I don't even speak of throwing and exception which is meaningless).
The real question in fact is to wonder who will be the recipient of the error messages ? : The author who's entitled to publish a post or a page.
So it's not the admin and that erroneous code won't go in production.
This error message should appear when the publisher writing the shortcode in his page/post uses it wrongly. In fact, at preview time. If the error is logged silently in the php log, the user may not even know that it exists! He may conclude that the plugin doesn't work and will remove it. I think it's not the desired aim.
So definitely, I think the best solution is to print the error in the produced page.
As I see that the answer dates from 2014, and nothing has been documented since in the page https://developer.wordpress.org/plugins/the-basics/best-practices/, I think it could deserve a little addendum.
Throwing an exception is a bad idea, the shortcode will be used in post content so there's no practical way to catch it and it will produce a fatal error. This is bad because
Most people don't know how to deal with this (see all the questions on here about errors, or blank pages). The admin installing your plugin / theme is going to have a bad time trying to work out what just bricked her site.
There is the potential to effectively disable a large section of the site, if the shortcode is called in a post on the home page for example. Is it worth causing a fatal error because your shortcode couldn't complete properly?
Much better to fail 'quietly' and either
Write to the php log. Use
error_log
etc to record a log message while outputting nothing to the site. Has the advantage of not showing any ugly errors to users, but the disadvantage of again being difficult for your average wordpress admin to work out what is going onPrint the error. Print out a descriptive error message. Gives the admin some idea of what is going on, and where the error is coming from, and avoids them coming here with 'shortcode not working' questions. However this might not be acceptable in production code, as it presents an error to the user, and is a bit ugly.
Which one you choose depends on what the shortcode does I guess, and how critical it is. Without knowing more about what you're writing I can't give a hard and fast answer, and even then it's going to be subjective. But seriously don't throw an exception.
Why not add an option so the admin can choose what happens when something goes wrong?