Come posso gestire in modo elegante una condizione di errore?
14 gen 2011, 22:25:25
Visualizzazioni: 325
Voti: 1
Il mio plugin sta bloccando l'esecuzione della pagina nel punto in cui chiama le funzioni qui sotto.
Ho due problemi...
1) Come posso riscrivere la funzione in modo che, in caso di errore, il plugin non blocchi il caricamento della pagina ma restituisca invece un messaggio di errore?
2) Come posso ottenere informazioni su quale potrebbe essere l'errore? Attualmente si blocca semplicemente senza mostrare l'errore.
function rseo_get_seo($check, $post){
//ritorna false;
switch ($check)
{
case "h1": return rseo_doTheParse('h1', $post);
case "h2": return rseo_doTheParse('h2', $post);
case "h3": return rseo_doTheParse('h3', $post);
case "img-alt": return rseo_doTheParse('img-alt', $post);
}
}
function rseo_doTheParse($heading, $post)
{
$content = $post->post_content;
if($content=="") return false;
$keyword = trim(strtolower(rseo_getKeyword($post)));
@$dom = new DOMDocument;
@$dom->loadHTML(strtolower($post->post_content));
$xPath = new DOMXPath(@$dom);
switch ($heading)
{
case "img-alt": return $xPath->evaluate('boolean(//img[contains(@alt, "'.$keyword.'")])');
default: return $xPath->evaluate('boolean(/html/body//'.$heading.'[contains(.,"'.$keyword.'")])');
}
}
Ecco il mio tentativo di modificare la seconda funzione con try catch ma ottengo un errore fatale durante l'attivazione del plugin...
function rseo_doTheParse($heading, $post){
try { //Ottengo un errore FATALE qui. unexpected '{'
$content = $post->post_content;
if($content=="") return false;
$keyword = trim(strtolower(rseo_getKeyword($post)));
@$dom = new DOMDocument;
@$dom->loadHTML(strtolower($post->post_content));
$xPath = new DOMXPath(@$dom);
switch ($heading)
{
case "img-alt": return $xPath->evaluate('boolean(//img[contains(@alt, "'.$keyword.'")])');
default: return $xPath->evaluate('boolean(/html/body//'.$heading.'[contains(.,"'.$keyword.'")])');
}
}
catch (Exception $e)
{
echo 'Eccezione catturata: ', $e->getMessage(), "\n";
}
}

Scott B
5.7K
Commenti
Tutte le risposte alla domanda
2
Domande correlate