Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Errors/BadRequestError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class BadRequestError extends Error
{
protected $field = null;

public function __construct($message, $code, $httpStatusCode, $field = null)
public function __construct($message, $code, $httpStatusCode, $field = null, array $context = [])
{
parent::__construct($message, $code, $httpStatusCode);
parent::__construct($message, $code, $httpStatusCode, $context);

$this->field = $field;
}
Expand All @@ -17,4 +17,4 @@ public function getField()
{
return $this->field;
}
}
}
33 changes: 28 additions & 5 deletions src/Errors/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,41 @@
class Error extends Exception
{
protected $httpStatusCode;
protected $context = [];

public function __construct($message, $code, $httpStatusCode)
public function __construct($message, $code, $httpStatusCode, array $context = [])
{
$this->code = $code;

$this->message = $message;
parent::__construct($message);

$this->code = $code;
$this->httpStatusCode = $httpStatusCode;
$this->context = $context;
}

public function getHttpStatusCode()
{
return $this->httpStatusCode;
}
}

public function getSource()
{
return $this->context['source'] ?? null;
}

public function getStep()
{
return $this->context['step'] ?? null;
}

public function getReason()
{
return $this->context['reason'] ?? null;
}

public function getMetadata()
{
$metadata = $this->context['metadata'] ?? null;

return is_array($metadata) ? $metadata : null;
}
}
20 changes: 12 additions & 8 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,22 @@ protected function processError($body, $httpStatusCode, $response)
// This is the fully qualified error class name
$error = __NAMESPACE__.'\Errors\\' . $error;

$description = $body['error']['description'];
$err = $body['error'];
$description = $err['description'] ?? '';

$field = null;
if (isset($body['error']['field']))
{
$field = $body['error']['field'];
$context = [
'source' => $err['source'] ?? null,
'step' => $err['step'] ?? null,
'reason' => $err['reason'] ?? null,
'metadata' => isset($err['metadata']) && is_array($err['metadata']) ? $err['metadata'] : null,
];

// Create an instance of the error and then throw it
throw new $error($description, $code, $httpStatusCode, $field);
if ($error === __NAMESPACE__.'\Errors\BadRequestError')
{
throw new $error($description, $code, $httpStatusCode, $err['field'] ?? null, $context);
}

throw new $error($description, $code, $httpStatusCode);
throw new $error($description, $code, $httpStatusCode, $context);
}

protected function throwServerError($body, $httpStatusCode)
Expand Down
Loading