Skip to content
Merged
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: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ composer require boltics\http-exception

```php
use Boltics\HttpException\Exception;
use Symfony\Component\HttpFoundation\Response;;
use Symfony\Component\HttpFoundation\Response;
```


Expand Down Expand Up @@ -59,6 +59,10 @@ try {
- `__construct()` The constructor checks that the value exist in the enum
- `getHttpCode()` Returns http code
- `setHttpCode()` Set http code
- `getErrorCode()` Another approach to get exception code
- `setAdditionalData()` Set additional data
- `getAdditionalData()` Get additional data
- `appendAdditionalData()` Append data to the additional data

Static methods:

Expand Down
57 changes: 57 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class Exception extends \Exception
*/
protected $httpCode = null;

/**
* @var array Additional data you want to put
*/
protected $additionalData = [];

/**
*
*/
Expand Down Expand Up @@ -55,6 +60,58 @@ public function getHttpCode()
return $this->httpCode;
}

/**
* An alternative way to get exception error code
*
* @return integer
*/
public function getErrorCode(): int
{
return $this->getCode();
}

/**
* Set additionalData in exception
*
* @param array $additionalData
*
* @return HttpException\Exception
*/
public function setAdditionalData(array $additionalData)
{
$this->additionalData = $additionalData;
return $this;
}

/**
* Get additional data
*
* @return array
*/
public function getAdditionalData(): array
{
return $this->additionalData;
}

/**
* Append data into the additional data with key
*
* @param mixed $additionalData
* @param mixed $key = null
*
* @return HttpException\Exception
*/
public function appendAdditionalData($additionalData, $key = null)
{
if (isset($key)) {
$this->additionalData[$key] = $additionalData;
} else {
$this->additionalData[] = $additionalData;
}

return $this;
}

/**
* Initialized error message and code if their is no input or the type is
* not \Exception::__construct() supported
Expand Down
66 changes: 66 additions & 0 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testExceptionConstructor()
$this->assertEquals(Response::HTTP_BAD_REQUEST, $e->getHttpCode());
$this->assertEquals($errorInfo['message'], $e->getMessage());
$this->assertEquals($errorInfo['errorCode'], $e->getCode());
$this->assertEquals($errorInfo['errorCode'], $e->getErrorCode());
}

/**
Expand All @@ -53,6 +54,7 @@ public function testExceptionConstructingWithoutHttpCode()
$this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $e->getHttpCode());
$this->assertEquals($errorInfo['message'], $e->getMessage());
$this->assertEquals($errorInfo['errorCode'], $e->getCode());
$this->assertEquals($errorInfo['errorCode'], $e->getErrorCode());
}

/**
Expand Down Expand Up @@ -156,5 +158,69 @@ public function testInheritanceOfException()
$this->assertEquals(TestException::FIRST_ERROR['httpCode'], $e->getHttpCode());
$this->assertEquals(TestException::FIRST_ERROR['message'], $e->getMessage());
$this->assertEquals(TestException::FIRST_ERROR['errorCode'], $e->getCode());
$this->assertEquals(TestException::FIRST_ERROR['errorCode'], $e->getErrorCode());
}

/**
* @return void
*/
public function testGettingDefaultAdditionalData()
{
$e = new TestException(TestException::FIRST_ERROR);

$this->assertEquals([], $e->getAdditionalData());
}

/**
* @return void
*/
public function testGetterAndSetterOfAdditionalData()
{
$testingData = [__FUNCTION__];

$e = new TestException(TestException::FIRST_ERROR);
$e->setAdditionalData($testingData);

$this->assertEquals($testingData, $e->getAdditionalData());
}

/**
* @return void
*/
public function testAppendAdditionalData()
{
$testingData = [
'a' => __FUNCTION__
];

$e = new TestException(TestException::FIRST_ERROR);
$e->setAdditionalData($testingData);
$this->assertEquals($testingData, $e->getAdditionalData());

$testingData['b'] = __CLASS__;
$e->appendAdditionalData(__CLASS__, 'b');
$this->assertEquals($testingData, $e->getAdditionalData());
}

/**
* Test append additional data without key
*
* @return void
*/
public function testAppendAdditionalDataWithoutKey()
{
$testingData = [__FUNCTION__];

$e = new TestException(TestException::FIRST_ERROR);
$e->setAdditionalData($testingData);
$this->assertEquals($testingData, $e->getAdditionalData());

$testingData[] = __CLASS__;
$e->appendAdditionalData(__CLASS__);
$this->assertEquals($testingData, $e->getAdditionalData());

$testingData[] = __NAMESPACE__;
$e->appendAdditionalData(__NAMESPACE__);
$this->assertEquals($testingData, $e->getAdditionalData());
}
}