Use custom exceptions, so we can selectively catch and extract information

Registered by Johannes Keukelaar on 2009-03-19

CoughPhp should throw its own custom exceptions, subclasses of Exception, so code using CoughPhp can selectively catch certain types of exceptions. This would also allow the users to extract information from the exceptions by other means than parsing the message. A prime candidate for this would be the exception thrown by As_Database::generateError, where it would be very nice to have a way to extract the query and the sql error message.

Blueprint information

Status:
Started
Approver:
None
Priority:
Medium
Drafter:
None
Direction:
Needs approval
Assignee:
None
Definition:
Approved
Series goal:
Accepted for 1.4
Implementation:
Beta Available
Milestone target:
milestone icon 1.4
Started by
Anthony Bush on 2009-03-30

Related branches

Sprints

Whiteboard

An example CoughException that we could throw anywhere we currently throw Exception:

/**
 * A base class for all exceptions thrown by CoughPhp. Only difference to Exception is that message is not optional.
 */
class CoughException extends Exception {
    public function __construct($message, $code = 0) {
        parent::__construct($message, $code);
    }
}

And a sample CoughDatabaseException that we could throw in the specific case mentioned above:

class CoughDatabaseException extends CoughException {
  private $type;
  private $sqlError;
  private $sql;

  public function __construct($type, $sqlError, $sql = "[SQL N/A]") {
    parent::__construct("As_Database Error [" . $type . "]: " . $sqlError . " -- QUERY: " . $sql);
    $this->type = $type;
    $this->sqlError = $sqlError;
    $this->sql = $sql;
  }

  public function getType() {
    return $this->type;
  }

  public function getSqlError() {
    return $this->sqlError;
  }

  public function getSql() {
    return $this->sql;
  }
}

This would allow users to figure out the sql error that happened, and the query that was run (if any) without having to parse the message of the exception.

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.