Skip to content

Commit

Permalink
Add headers support in HTTP Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
nguereza-tony committed Nov 20, 2023
1 parent f98037a commit 4f3cbea
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/Http/Exception/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,30 @@ class HttpException extends Exception
*/
protected string $description = '';

/**
* Additional headers to add into error response
* @var array<string, mixed>
*/
protected array $headers = [];

/**
* Create new instance
* @param ServerRequestInterface $request
* @param string $message
* @param int $code
* @param Throwable|null $previous
* @param array<string, mixed> $headers
*/
public function __construct(
ServerRequestInterface $request,
string $message = '',
int $code = 0,
?Throwable $previous = null
?Throwable $previous = null,
array $headers = []
) {
parent::__construct($message, $code, $previous);
$this->request = $request;
$this->headers = $headers;
}

/**
Expand Down Expand Up @@ -128,4 +137,24 @@ public function setDescription(string $description): self

return $this;
}

/**
* Return headers
* @return array<string, mixed>
*/
public function getHeaders(): array
{
return $this->headers;
}

/**
* Set the headers
* @param array<string, mixed> $headers
* @return $this
*/
public function setHeaders(array $headers): self
{
$this->headers = $headers;
return $this;
}
}
2 changes: 1 addition & 1 deletion tests/Console/Command/VendorPublishCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function testExecutePublishOnlySomeFiles(): void
'templates',
$rootDir->url()
]

]
]);
$writer = $this->getWriterInstance();
Expand Down
13 changes: 13 additions & 0 deletions tests/Http/Exception/HttpExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@ public function testTitleAndDescription(): void
$this->assertEquals('exception description', $o->getDescription());
$this->assertEquals('exception title', $o->getTitle());
}

public function testHeaders(): void
{
$request = $this->getMockInstance(ServerRequest::class);
$o = new HttpException($request);
$o->setHeaders(['foo' => 'bar']);

$headers = $o->getHeaders();

$this->assertCount(1, $headers);
$this->assertArrayHasKey('foo', $headers);
$this->assertEquals('bar', $headers['foo']);
}
}

0 comments on commit 4f3cbea

Please sign in to comment.