Minor errors refactoring

This commit is contained in:
vladar 2015-08-16 16:39:30 +06:00
parent 1b2f74f115
commit 698b2cb862
4 changed files with 107 additions and 40 deletions

View File

@ -3,7 +3,7 @@ namespace GraphQL;
use GraphQL\Language\Source; use GraphQL\Language\Source;
// /graphql-js/src/error/index.js // /graphql-js/src/error/GraphQLError.js
class Error extends \Exception class Error extends \Exception
{ {
@ -12,11 +12,6 @@ class Error extends \Exception
*/ */
public $message; public $message;
/**
* @var string
*/
public $stack;
/** /**
* @var array * @var array
*/ */
@ -25,62 +20,108 @@ class Error extends \Exception
/** /**
* @var array * @var array
*/ */
public $positions; private $positions;
/** /**
* @var array<SourceLocation> * @var array<SourceLocation>
*/ */
public $locations; private $locations;
/** /**
* @var Source|null * @var Source|null
*/ */
public $source; private $source;
/** /**
* Given an arbitrary Error, presumably thrown while attempting to execute a
* GraphQL operation, produce a new GraphQLError aware of the location in the
* document responsible for the original Error.
*
* @param $error * @param $error
* @return FormattedError * @param array|null $nodes
* @return Error
*/ */
public static function formatError($error) public static function createLocatedError($error, array $nodes = null)
{ {
if (is_array($error)) { if ($error instanceof \Exception) {
$message = isset($error['message']) ? $error['message'] : null; $message = $error->getMessage();
$locations = isset($error['locations']) ? $error['locations'] : null; $previous = $error;
} else if ($error instanceof Error) {
$message = $error->message;
$locations = $error->locations;
} else { } else {
$message = (string) $error; $message = (string) $error;
$locations = null; $previous = null;
} }
return new FormattedError($message, $locations); return new Error($message, $nodes, $previous);
} }
/** /**
* @param string $message * @param Error $error
* @param array|null $nodes * @return FormattedError
* @param null $stack
*/ */
public function __construct($message, array $nodes = null, $stack = null) public static function formatError(Error $error)
{ {
$this->message = $message; return new FormattedError($error->getMessage(), $error->getLocations());
$this->stack = $stack ?: $message; }
if ($nodes) { /**
$this->nodes = $nodes; * @param string|\Exception $message
$positions = array_map(function($node) { return isset($node->loc) ? $node->loc->start : null; }, $nodes); * @param array|null $nodes
$positions = array_filter($positions); * @param Source $source
* @param null $positions
*/
public function __construct($message, array $nodes = null, \Exception $previous = null, Source $source = null, $positions = null)
{
parent::__construct($message, 0, $previous);
if (!empty($positions)) { $this->nodes = $nodes;
$this->positions = $positions; $this->source = $source;
$loc = $nodes[0]->loc; }
$source = $loc ? $loc->source : null;
if ($source) { /**
$this->locations = array_map(function($pos) use($source) {return $source->getLocation($pos);}, $positions); * @return Source|null
$this->source = $source; */
} public function getSource()
{
if (null === $this->source) {
if (!empty($this->nodes[0]) && !empty($this->nodes[0]->loc)) {
$this->source = $this->nodes[0]->loc->source;
} }
} }
return $this->source;
}
/**
* @return array
*/
public function getPositions()
{
if (null === $this->positions) {
if (!empty($this->nodes)) {
$positions = array_map(function($node) { return isset($node->loc) ? $node->loc->start : null; }, $this->nodes);
$this->positions = array_filter($positions);
}
}
return $this->positions;
}
/**
* @return array<SourceLocation>
*/
public function getLocations()
{
if (null === $this->locations) {
$positions = $this->getPositions();
$source = $this->getSource();
if ($positions && $source) {
$this->locations = array_map(function ($pos) use ($source) {
return $source->getLocation($pos);
}, $positions);
} else {
$this->locations = [];
}
}
return $this->locations;
} }
} }

View File

@ -372,7 +372,7 @@ class Executor
$exeContext->schema $exeContext->schema
); );
} catch (\Exception $error) { } catch (\Exception $error) {
throw new Error($error->getMessage(), [$fieldAST], $error->getTrace()); throw Error::createLocatedError($error, [$fieldAST]);
} }
return self::completeField( return self::completeField(

View File

@ -3,6 +3,9 @@ namespace GraphQL;
class FormattedError class FormattedError
{ {
/**
* @var string
*/
public $message; public $message;
/** /**
@ -10,9 +13,24 @@ class FormattedError
*/ */
public $locations; public $locations;
public function __construct($message, $locations = null) /**
* @param $message
* @param array<Language\SourceLocation> $locations
*/
public function __construct($message, $locations = [])
{ {
$this->message = $message; $this->message = $message;
$this->locations = $locations; $this->locations = array_map(function($loc) { return $loc->toArray();}, $locations);
}
/**
* @return array
*/
public function toArray()
{
return [
'message' => $this->message,
'locations' => $this->locations
];
} }
} }

View File

@ -11,4 +11,12 @@ class SourceLocation
$this->line = $line; $this->line = $line;
$this->column = $col; $this->column = $col;
} }
public function toArray()
{
return [
'line' => $this->line,
'column' => $this->column
];
}
} }