Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PhutilInvalidStateException class
Summary: There's a bunch of `Call %s before calling %s!` exceptions that can be simplified with a custom script. Test Plan: Wrote unit tests. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: Korvin, epriestley Differential Revision: https://secure.phabricator.com/D12803
- Loading branch information
1 parent
211230c
commit f06c0b7
Showing
9 changed files
with
65 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
final class PhutilInvalidStateException extends Exception { | ||
private $callee; | ||
private $function; | ||
|
||
public function __construct($function, $callee = null) { | ||
if ($callee === null) { | ||
$callee = idx(debug_backtrace(), 1); | ||
$callee = idx($callee, 'function'); | ||
} | ||
|
||
$this->callee = $callee; | ||
$this->function = $function; | ||
|
||
parent::__construct( | ||
pht( | ||
'Call %s before calling %s!', | ||
$this->function.'()', | ||
$this->callee.'()')); | ||
} | ||
|
||
public function getCallee() { | ||
return $this->callee; | ||
} | ||
|
||
public function getFunction() { | ||
return $this->function; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/exception/__tests__/PhutilInvalidStateExceptionTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
final class PhutilInvalidStateExceptionTestCase extends PhutilTestCase { | ||
|
||
public function testException() { | ||
try { | ||
throw new PhutilInvalidStateException('someMethod'); | ||
} catch (PhutilInvalidStateException $ex) { | ||
$this->assertEqual( | ||
__FUNCTION__, | ||
$ex->getCallee()); | ||
$this->assertEqual( | ||
'someMethod', | ||
$ex->getFunction()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters