Skip to content

Commit b914cc0

Browse files
committed
Use LoaderException instead of generic Exception for loader errors
This make it easier to handle the errors and could help with phpmyadmin/phpmyadmin#12791 Signed-off-by: Michal Čihař <[email protected]>
1 parent b9d3d9d commit b914cc0

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [Unreleased]
44

5+
* Use custom LoaderException for context loading errors.
6+
57
## [4.1.9] - 2017-07-12
68

79
* Various code cleanups.

src/Context.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace PhpMyAdmin\SqlParser;
1111

12+
use PhpMyAdmin\SqlParser\Exceptions\LoaderException;
13+
1214
/**
1315
* Holds the configuration of the context that is currently used.
1416
*
@@ -443,7 +445,7 @@ public static function isSeparator($str)
443445
* @param string $context name of the context or full class name that
444446
* defines the context
445447
*
446-
* @throws \Exception if the specified context doesn't exist
448+
* @throws LoaderException if the specified context doesn't exist
447449
*/
448450
public static function load($context = '')
449451
{
@@ -455,8 +457,9 @@ public static function load($context = '')
455457
$context = self::$contextPrefix . $context;
456458
}
457459
if (!class_exists($context)) {
458-
throw new \Exception(
459-
'Specified context ("' . $context . '") does not exist.'
460+
throw new LoaderException(
461+
'Specified context ("' . $context . '") does not exist.',
462+
$context
460463
);
461464
}
462465
self::$loadedContext = $context;
@@ -491,7 +494,7 @@ public static function loadClosest($context = '')
491494
try {
492495
// Trying to load the new context.
493496
static::load($context);
494-
} catch (\Exception $e) {
497+
} catch (LoaderException $e) {
495498
// If it didn't work, we are looking for a new one and skipping
496499
// over to the next generation that will try the new context.
497500
$context = preg_replace(

src/Exceptions/LoaderException.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* Exception thrown by the lexer.
5+
*/
6+
7+
namespace PhpMyAdmin\SqlParser\Exceptions;
8+
9+
/**
10+
* Exception thrown by the lexer.
11+
*
12+
* @category Exceptions
13+
*
14+
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
15+
*/
16+
class LoaderException extends \Exception
17+
{
18+
/**
19+
* The failed load name.
20+
*
21+
* @var string
22+
*/
23+
public $name;
24+
25+
/**
26+
* Constructor.
27+
*
28+
* @param string $msg the message of this exception
29+
* @param string $ch the character that produced this exception
30+
* @param int $pos the position of the character
31+
* @param int $code the code of this error
32+
*/
33+
public function __construct($msg = '', $name = '', $code = 0)
34+
{
35+
parent::__construct($msg, $code);
36+
$this->name = $name;
37+
}
38+
}

0 commit comments

Comments
 (0)