diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -190,6 +190,8 @@ 'PhutilInRequestKeyValueCache' => 'cache/PhutilInRequestKeyValueCache.php', 'PhutilInteractiveEditor' => 'console/PhutilInteractiveEditor.php', 'PhutilInvalidRuleParserGeneratorException' => 'parser/generator/exception/PhutilInvalidRuleParserGeneratorException.php', + 'PhutilInvalidStateException' => 'exception/PhutilInvalidStateException.php', + 'PhutilInvalidStateExceptionTestCase' => 'exception/__tests__/PhutilInvalidStateExceptionTestCase.php', 'PhutilInvisibleSyntaxHighlighter' => 'markup/syntax/highlighter/PhutilInvisibleSyntaxHighlighter.php', 'PhutilIrreducibleRuleParserGeneratorException' => 'parser/generator/exception/PhutilIrreducibleRuleParserGeneratorException.php', 'PhutilJIRAAuthAdapter' => 'auth/PhutilJIRAAuthAdapter.php', @@ -621,6 +623,8 @@ 'PhutilIPAddressTestCase' => 'PhutilTestCase', 'PhutilInRequestKeyValueCache' => 'PhutilKeyValueCache', 'PhutilInvalidRuleParserGeneratorException' => 'PhutilParserGeneratorException', + 'PhutilInvalidStateException' => 'Exception', + 'PhutilInvalidStateExceptionTestCase' => 'PhutilTestCase', 'PhutilIrreducibleRuleParserGeneratorException' => 'PhutilParserGeneratorException', 'PhutilJIRAAuthAdapter' => 'PhutilOAuth1AuthAdapter', 'PhutilJSONParserException' => 'Exception', diff --git a/src/auth/PhutilOAuthAuthAdapter.php b/src/auth/PhutilOAuthAuthAdapter.php --- a/src/auth/PhutilOAuthAuthAdapter.php +++ b/src/auth/PhutilOAuthAuthAdapter.php @@ -151,8 +151,7 @@ protected function loadAccessTokenData() { $code = $this->getCode(); if (!$code) { - throw new Exception( - pht('Call %s before accessing adapter information.', 'setCode()')); + throw new PhutilInvalidStateException('setCode'); } $params = array( diff --git a/src/cache/PhutilDirectoryKeyValueCache.php b/src/cache/PhutilDirectoryKeyValueCache.php --- a/src/cache/PhutilDirectoryKeyValueCache.php +++ b/src/cache/PhutilDirectoryKeyValueCache.php @@ -169,10 +169,7 @@ */ private function getCacheDirectory() { if (!$this->cacheDirectory) { - throw new Exception( - pht( - 'Call %s before using a directory cache!', - 'setCacheDirectory()')); + throw new PhutilInvalidStateException('setCacheDirectory'); } return $this->cacheDirectory; } @@ -237,11 +234,7 @@ */ private function unlockCache() { if (!$this->lock) { - throw new Exception( - pht( - 'Call %s before %s!', - 'lockCache()', - __FUNCTION__.'()')); + throw new PhutilInvalidStateException('lockCache'); } $this->lock->unlock(); diff --git a/src/cache/PhutilMemcacheKeyValueCache.php b/src/cache/PhutilMemcacheKeyValueCache.php --- a/src/cache/PhutilMemcacheKeyValueCache.php +++ b/src/cache/PhutilMemcacheKeyValueCache.php @@ -114,10 +114,7 @@ $n = count($this->servers); if (!$n) { - throw new Exception( - pht( - 'Call %s before using Memcache!', - 'setServers()')); + throw new PhutilInvalidStateException('setServers'); } foreach ($keys as $key) { diff --git a/src/cache/PhutilOnDiskKeyValueCache.php b/src/cache/PhutilOnDiskKeyValueCache.php --- a/src/cache/PhutilOnDiskKeyValueCache.php +++ b/src/cache/PhutilOnDiskKeyValueCache.php @@ -177,11 +177,7 @@ */ private function saveCache() { if (!$this->lock) { - throw new Exception( - pht( - 'Call %s before %s!', - 'loadCache($hold_lock=true)', - __FUNCTION__.'()')); + throw new PhutilInvalidStateException('loadCache'); } // We're holding a lock so we're safe to do a write to a well-known file. @@ -201,10 +197,7 @@ */ private function getCacheFile() { if (!$this->cacheFile) { - throw new Exception( - pht( - 'Call %s before using a disk cache!', - 'setCacheFile()')); + throw new PhutilInvalidStateException('setCacheFile'); } return $this->cacheFile; } diff --git a/src/exception/PhutilInvalidStateException.php b/src/exception/PhutilInvalidStateException.php new file mode 100644 --- /dev/null +++ b/src/exception/PhutilInvalidStateException.php @@ -0,0 +1,30 @@ +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; + } +} diff --git a/src/exception/__tests__/PhutilInvalidStateExceptionTestCase.php b/src/exception/__tests__/PhutilInvalidStateExceptionTestCase.php new file mode 100644 --- /dev/null +++ b/src/exception/__tests__/PhutilInvalidStateExceptionTestCase.php @@ -0,0 +1,17 @@ +assertEqual( + __FUNCTION__, + $ex->getCallee()); + $this->assertEqual( + 'someMethod', + $ex->getFunction()); + } + } +} diff --git a/src/parser/PhutilParserGenerator.php b/src/parser/PhutilParserGenerator.php --- a/src/parser/PhutilParserGenerator.php +++ b/src/parser/PhutilParserGenerator.php @@ -118,44 +118,28 @@ public function getEOFSymbol() { if ($this->eofSymbol === null) { - throw new Exception( - pht( - 'Call %s before %s!', - 'processGrammar()', - __FUNCTION__.'()')); + throw new PhutilInvalidStateException('processGrammar'); } return $this->eofSymbol; } public function getInitSymbol() { if ($this->initSymbol === null) { - throw new Exception( - pht( - 'Call %s before %s!', - 'processGrammar()', - __FUNCTION__.'()')); + throw new PhutilInvalidStateException('processGrammar'); } return $this->initSymbol; } public function getEpsilonSymbol() { if ($this->epsilonSymbol === null) { - throw new Exception( - pht( - 'Call %s before %s!', - 'processGrammar()', - __FUNCTION__.'()')); + throw new PhutilInvalidStateException('processGrammar'); } return $this->epsilonSymbol; } public function getEndSymbol() { if ($this->endSymbol === null) { - throw new Exception( - pht( - 'Call %s before %s!', - 'processGrammar()', - __FUNCTION__.'()')); + throw new PhutilInvalidStateException('processGrammar'); } return $this->endSymbol; } @@ -905,11 +889,7 @@ */ public function inspectRules() { if (!$this->rulesValidated) { - throw new Exception( - pht( - 'Call %s before %s!', - 'processGrammar()', - __FUNCTION__.'()')); + throw new PhutilInvalidStateException('processGrammar'); } return $this->rules; } @@ -920,11 +900,7 @@ */ public function inspectFirstTable() { if ($this->firstTable === null) { - throw new Exception( - pht( - 'Call %s before %s!', - 'processGrammar()', - __FUNCTION__.'()')); + throw new PhutilInvalidStateException('processGrammar'); } return $this->firstTable; } diff --git a/src/utils/PhutilEditDistanceMatrix.php b/src/utils/PhutilEditDistanceMatrix.php --- a/src/utils/PhutilEditDistanceMatrix.php +++ b/src/utils/PhutilEditDistanceMatrix.php @@ -165,8 +165,7 @@ private function requireSequences() { if ($this->x === null) { - throw new Exception( - pht('Call %s before performing useful work!', 'setSequences()')); + throw new PhutilInvalidStateException('setSequences'); } } @@ -292,8 +291,7 @@ private function getTypeMatrix() { if (!$this->computeString) { - throw new Exception( - pht('Call %s before %s.', 'setComputeString()', 'getTypeMatrix()')); + throw new PhutilInvalidStateException('setComputeString'); } if ($this->typeMatrix === null) { $this->computeMatrix($this->x, $this->y);