Skip to content

Commit b196bdf

Browse files
authored
Merge pull request #152 from bakaphp/feat-add-session-not-found
feat : session not found exception
2 parents 100ed40 + 117bf9b commit b196bdf

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

src/Exception/HttpException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class HttpException extends Exception
1515
/**
1616
* Get the http status code of the exception.
1717
*
18-
* @return string
18+
* @return int
1919
*/
2020
public function getHttpCode() : int
2121
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baka\Http\Exception;
6+
7+
use Baka\Exception\HttpException;
8+
use Baka\Http\Response\Phalcon as Response;
9+
10+
class SessionNotFound extends HttpException
11+
{
12+
/**
13+
* Code 499 , is a specific http code for kanvas frontend,
14+
* when they see this error they will know they have to clear user session on device.
15+
*/
16+
protected int $httpCode = Response::SESSION_NOT_FOUND;
17+
protected string $httpMessage = 'Unauthorized';
18+
protected string $severity = 'warning';
19+
}

src/Http/Response/Phalcon.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Baka\Http\Request\Phalcon as Request;
1010
use Error;
1111
use Phalcon\Http\Response;
12-
1312
use Throwable;
1413

1514
class Phalcon extends Response
@@ -26,6 +25,7 @@ class Phalcon extends Response
2625
const FORBIDDEN = 403;
2726
const NOT_FOUND = 404;
2827
const NOT_ACCEPTABLE = 406;
28+
const SESSION_NOT_FOUND = 499;
2929
const INTERNAL_SERVER_ERROR = 500;
3030
const NOT_IMPLEMENTED = 501;
3131
const BAD_GATEWAY = 502;
@@ -42,6 +42,7 @@ class Phalcon extends Response
4242
403 => 'Forbidden',
4343
404 => 'Not Found',
4444
422 => 'Unprocessable Entity',
45+
499 => 'Session Not Found',
4546
500 => 'Internal Server Error',
4647
501 => 'Not Implemented',
4748
502 => 'Bad Gateway',
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baka\Test\Unit\Http\Request;
6+
7+
use Baka\Http\Exception\BadRequestException;
8+
use Baka\Http\Exception\ForbiddenException;
9+
use Baka\Http\Exception\InternalServerErrorException;
10+
use Baka\Http\Exception\NotFoundException;
11+
use Baka\Http\Exception\SessionNotFound;
12+
use Baka\Http\Exception\UnauthorizedException;
13+
use PhalconUnitTestCase;
14+
15+
class PhalconExceptionTestTest extends PhalconUnitTestCase
16+
{
17+
public function testSessionDoesNotExist()
18+
{
19+
try {
20+
throw new SessionNotFound('Session not found');
21+
} catch (SessionNotFound $e) {
22+
$this->assertTrue($e->getHttpCode() === 499);
23+
}
24+
25+
try {
26+
throw new BadRequestException('Session not found');
27+
} catch (BadRequestException $e) {
28+
$this->assertTrue($e->getHttpCode() === 400);
29+
}
30+
31+
try {
32+
throw new ForbiddenException('Session not found');
33+
} catch (ForbiddenException $e) {
34+
$this->assertTrue($e->getHttpCode() === 403);
35+
}
36+
37+
try {
38+
throw new InternalServerErrorException('Session not found');
39+
} catch (InternalServerErrorException $e) {
40+
$this->assertTrue($e->getHttpCode() === 500);
41+
}
42+
43+
try {
44+
throw new NotFoundException('Session not found');
45+
} catch (NotFoundException $e) {
46+
$this->assertTrue($e->getHttpCode() === 404);
47+
}
48+
49+
try {
50+
throw new UnauthorizedException('Session not found');
51+
} catch (UnauthorizedException $e) {
52+
$this->assertTrue($e->getHttpCode() === 401);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)