Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Error handler per fatal error

    Ciao a tutti ho una classe che trasforma tutti gli errori in eccezioni ma ho un problema con i fatal error.
    Se ne lancio uno con trigger_error nel try viene trasformato correttamente in eccezione e catturato mentre se è l'interprete che lo lancia questo non succede.
    Questo è il codice che lancia il fatal error che non viene catturato.
    Codice PHP:
    try {
         
    $comp = new $className ();
    } catch (
    ErrorToException $e) {
         echo 
    $e->getMessage();

    Se $className non è il nome di una classe conosciuta mi restituisce un fatal error non catturato dal catch.

    L'errore dovrebbe essere in fase di esecuzione e non in fase di compilazione giusto?
    Coltiva Linux, Windows si pianta da solo!

  2. #2
    Ho visto in giro che php non permette di catturare i fatal error, e quindi per non usare strani trucchetti tipo questo (un po' difficile da applicare visto che il buffering dell'output mi serve) mi conviene usare class_exists() o avete qualche altro trucchetto?
    Coltiva Linux, Windows si pianta da solo!

  3. #3
    è l'unico modo ... ma in realtà non ti devi preoccupare perché l'output buffering può essere eseguito in cascata ... quindi tu puoi semplicemente avviare l'ob per la cattura degli errori e poi, nel momento in cui si verifica l'errore, leggi il contenuto di quello corrente cercando il fatal error per poi ciclarli tutti e chiuderli
    The fastest Redis alternative ... cachegrand! https://github.com/danielealbano/cachegrand

  4. #4
    Ok allora uso quel metodo, ma come faccio a lanciare un eccezione e cosa deve restituire la funzione fatal_error_handler per far continuare l'esecuzione e visualizzare la mia pagina di errore?
    Se lancio un eccezione all'interno di fatal_error_handler non si vede proprio niente perchè interrompo il buffer prima che venga inviato. (Nel codice il pattern di ereg sarebbe senza lo spazio)
    Codice PHP:
    function fatal_error_handler($buffer) {
      if (
    ereg("(error[/b]: )(.+)(<br)"$buffer$regs) ) {
        
    $err preg_replace("/<.*?>/","",$regs[2]);
        
    error_log($err);
        return 
    "ERROR CAUGHT check log file";
      }
      return 
    $buffer;
    }

    function 
    handle_error ($errno$errstr$errfile$errline)
    {
        
    error_log("$errstr in $errfile on line $errline");
        if(
    $errno == FATAL || $errno == ERROR){
            
    ob_end_flush();
            echo 
    "ERROR CAUGHT check log file";
            exit(
    0);
        }
    }

    ob_start("fatal_error_handler");
    set_error_handler("handle_error");

    //causes a warning
    preg_replace();

    //would normally cause a fatal error, but instead our output handler will be called allowing us to handle the error.
    somefunction();
    ob_end_flush(); 
    Coltiva Linux, Windows si pianta da solo!

  5. #5
    Codice PHP:
    function exceptions_error_handler($severity$message$filename$lineno) {
      if (
    error_reporting() == 0) {
        return;
      }
      if (
    error_reporting() & $severity) {
        throw new 
    ErrorException($message0$severity$filename$lineno);
      }
    }
    set_error_handler('exceptions_error_handler');

    // This is a default exceptions-handler. For debugging, it's practical to get a readable
    // trace dumped out at the top level, rather than just a blank screen.
    // If you use something like Xdebug, you may want to skip this part, since it already gives
    // a similar output.
    // For production, you should replace this handler with something, which logs the error,
    // and doesn't dump a trace. Failing to do so could be a security risk.
    function debug_exception_handler($ex) {
        if (
    php_sapi_name() == 'cli') {
            echo 
    "Error (code:".$ex->getCode().") :".$ex->getMessage()."\n at line ".$ex->getLine            ()." in file ".$ex->getFile()."\n";
            echo 
    $ex->getTraceAsString()."\n";
          } else {
        echo 
    "<p style='font-family:helvetica,sans-serif'>\n";
        echo 
    "[b]Error :[/b]".$ex->getMessage()."
    \n"
    ;
        echo 
    "[b]Code :[/b]".$ex->getCode()."
    \n"
    ;
        echo 
    "[b]File :[/b]".$ex->getFile()."
    \n"
    ;
        echo 
    "[b]Line :[/b]".$ex->getLine()."</p>\n";
        echo 
    "<div style='font-family:garamond'>".nl2br(htmlspecialchars($ex->getTraceAsString()))."</div>\n";
      }
      exit(-
    1);
    }


    function 
    prodution_exception_handler($ex) {
        
    $msg "Error :".$ex->getMessage().PHP_EOL;
         
    $msg .= "Code :".$ex->getCode().PHP_EOL;
         
    $msg .= "File :".$ex->getFile().PHP_EOL;
          
    $msg .= "Line :".$ex->getLine().PHP_EOL;
         
    $msg .= $ex->getTraceAsString();
         
    $logDir= new w_FindPath('.','log');
        
    error_log($msg3$logDir->get().'index.log');
        
    $output= <<<EOD
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta name="robots" content="noindex,nofollow" />
    <meta name="content-language" content="it" />
    <meta name="description" content="Error page" />
    <meta name="keywords" content="Error page" />
    <link rel="shortcut icon" href="favicon/favicon.ico" />
    <title>Â_Bracelli Pagina di Errore</title>
    <link rel="stylesheet" type="text/css" href="css/main.css" />
    </head>
    <body>
    <div id="container">
    <div id="header">
    <h1>[url="/"][/url]</h1>
    </div><div id="content">
    <h1 style="margin-top:70px;width:800px" class="center">Server troppo occupato</h1> 
    <h2 style="width:800px" class="center">Spiacente, il sito non disponibile.</h2> 
    <h2 style="width:800px" class="center">Per favore, contatta il web master.</h2> 
    </div> 
    <div id="footer">

    </div> 
    </div> 
    </body>
    </html>
    EOD;
        echo 
    $output;
        exit(-
    1);
    }
    (
    true)?set_exception_handler('debug_exception_handler'):set_exception_handler('prodution_exception_handler'); 

    Update from http://konstrukt.dk/




    Dai un occhio al codice di

    http://forum.html.it/forum/showthrea...readid=1356221
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.