Quello che faccio è avere una classe per creare eccezioni personalizzate, ma per standardizzare le cose, ho semplicemente un argomento addizionale, che è un oggetto (beh, finora è sempre stato un array), che mi permette di specificare quantità illimitata di dati di eccezione (proprio come un'eccezione javascript).
uscita:
Fatal error: Uncaught SqlProxyException 'Duplicate entry '1' for key 'PRIMARY'' in /usr/src/wrangler/trunk/common/SqlProxy.php(356)
#0 /usr/src/wrangler/trunk/common/SqlProxy.php(341): SqlProxy::Update('INSERT into tes...')
#1 /usr/src/wrangler/trunk/common/SqlProxy.php(455): SqlProxy::Insert('INSERT into tes...')
#2 {main}
Array
(
[sql] => INSERT into test SET test='1'
[errorObject] => Array
(
[status] => UNKNOWN
[data] => Array
(
[rowsAffected] => -1
[errorMsg] => Duplicate entry '1' for key 'PRIMARY'
[errorCode] => 1062
....
Ciò si ottiene il seguente nel mio codice:
<? require_once "CustomException.php";
## Define the custom exception
class SqlProxyException extends CustomException {}
## Throw a custom exception
throw new SqlProxyException($errorMsg, $errorCode, null,
array("sql" => $query, "errorObject" => $result)
);
## Catch the custom exception
try {
SqlProxy::Insert($argv[2]);
} catch (SqlProxyException $e) {
fprintf(STDERR, "Fatal error: Uncaught SqlProxyException '%s' in %s(%s)\n%s\n%s\n",
$e->getMessage(), $e->getFile(), $e->getLine(),
$e->getTraceAsString(),
$e->getObject() ? print_r($e->getObject(), 1) : ""
);
exit(1);
}
Non troppo difficile ... e la magia dietro CustomException.php è
<?php
interface IException
{
/* Protected methods inherited from Exception class */
public function getMessage(); // Exception message
public function getCode(); // User-defined Exception code
public function getFile(); // Source filename
public function getLine(); // Source line
public function getTrace(); // An array of the backtrace()
public function getTraceAsString(); // Formated string of trace
/* Overrideable methods inherited from Exception class */
public function __toString(); // formated string for display
public function __construct($message = null, $code = 0);
}
abstract class CustomException extends Exception implements IException
{
protected $message = 'Unknown exception'; // Exception message
private $string; // Unknown
protected $code = 0; // User-defined exception code
protected $file; // Source filename of exception
protected $line; // Source line of exception
protected $object = null; // Extra information in an object (array) js style
private $trace; // Unknown
public function __construct($message = null, $code = 0, Exception $previous = null, $eventObject = null)
{
if (!$message) {
throw new $this('Unknown '. get_class($this));
}
parent::__construct($message, $code, $previous);
$this->object = $eventObject;
}
public function __toString()
{
return get_class($this) . " '{$this->message}' in {$this->file}({$this->line})\n"
. "{$this->getTraceAsString()}";
}
/* Additional custom method */
public function getObject() // Return object (array) of extra info (js style)
{
return $this->object;
}
}
Come lo uso? se lancio una nuova FieldException ('message', 'field'); Ottengo parametri errati per Eccezione – dazz
@Dazz 'new FieldMessage ('message', 0, NULL, 'field')'. Il costruttore per le eccezioni richiede il messaggio, ma consente anche due argomenti facoltativi. La modifica dell'ordine potrebbe interrompere la gestione delle eccezioni in applicazioni complesse in cui le eccezioni vengono ridisposte, quindi per assicurarti che non si interrompa nulla, aggiungi il param come quarto (se usi questa soluzione) – Gordon
Ho dovuto lasciare fuori la parte precedente di Exception $, questo è solo per php 5.3.0+ e io uso 5.2.9. e nel costrutto $ this-> field dovrebbe essere $ this -> _ field. Quindi funziona perfettamente! Tnx – dazz