getParent throws exception

Should we get another Exception for if there's no parent? I think so,
because it's not really the same context as 'no result'.
This commit is contained in:
Mikael Nordfeldth 2013-10-05 12:30:14 +02:00
parent e1b5798ba4
commit 29de09a63d

View File

@ -920,22 +920,27 @@ class Notice extends Managed_DataObject
if ($root !== false && $root->inScope($profile)) { if ($root !== false && $root->inScope($profile)) {
return $root; return $root;
} else { }
$last = $this;
do { $last = $this;
while (true) {
try {
$parent = $last->getParent(); $parent = $last->getParent();
if (!empty($parent) && $parent->inScope($profile)) { if ($parent->inScope($profile)) {
common_debug(__METHOD__ . 'Parent of '.$last->id.' is '.$parent->id);
$last = $parent; $last = $parent;
continue; continue;
} else { }
} catch (Exception $e) {
// Latest notice has no parent
common_debug(__METHOD__ . 'Found no parent for '.$last->id);
}
// No parent, or parent out of scope
$root = $last; $root = $last;
break; break;
} }
} while (!empty($parent));
self::cacheSet($keypart, $root); self::cacheSet($keypart, $root);
}
return $root; return $root;
} }
@ -1302,17 +1307,15 @@ class Notice extends Managed_DataObject
$replied = array(); $replied = array();
// If it's a reply, save for the replied-to author // If it's a reply, save for the replied-to author
try {
if (!empty($this->reply_to)) { $author = $this->getParent()->getProfile();
$original = $this->getParent(); if ($author instanceof Profile) {
if (!empty($original)) { // that'd be weird
$author = $original->getProfile();
if (!empty($author)) {
$this->saveReply($author->id); $this->saveReply($author->id);
$replied[$author->id] = 1; $replied[$author->id] = 1;
self::blow('reply:stream:%d', $author->id); self::blow('reply:stream:%d', $author->id);
} }
} } catch (Exception $e) {
// Not a reply, since it has no parent!
} }
// @todo ideally this parser information would only // @todo ideally this parser information would only
@ -2532,12 +2535,15 @@ class Notice extends Managed_DataObject
public function getParent() public function getParent()
{ {
if (is_int($this->_parent) && $this->_parent == -1) {
if (empty($this->reply_to)) { if (empty($this->reply_to)) {
$this->_parent = null; // Should this also be NoResultException? I don't think so.
} else { throw new Exception('Notice has no parent');
} elseif ($this->_parent === -1) { // local object cache
$this->_parent = Notice::getKV('id', $this->reply_to); $this->_parent = Notice::getKV('id', $this->reply_to);
} }
if (!($this->_parent instanceof Notice)) {
throw new NoResultException($this->_parent);
} }
return $this->_parent; return $this->_parent;
} }