Skip to content

Commit cbeace7

Browse files
committed
Make use of ErrorResponse handling
By using DomainException that we'll get caught automatically.
1 parent 13092e7 commit cbeace7

19 files changed

+128
-10
lines changed

assets/js/ConnectFour/Timer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ customElements.define('connect-four-timer', class extends HTMLElement {
1414
this._panicLevelThreeBelowMs = parseInt(this.getAttribute('panic-three-below-ms') || 3000);
1515
this._currentTickSound = null;
1616

17-
window.requestAnimationFrame(this._render);
17+
this._animationFrameId = window.requestAnimationFrame(this._render);
1818

1919
sse.subscribe(`connect-four-${this.getAttribute('game-id')}`, {
2020
'ConnectFour.PlayerJoined': this._onPlayerJoined,
@@ -29,6 +29,7 @@ customElements.define('connect-four-timer', class extends HTMLElement {
2929

3030
disconnectedCallback() {
3131
this._sseAbortController.abort();
32+
cancelAnimationFrame(this._animationFrameId);
3233
}
3334

3435
_render = () => {
@@ -57,7 +58,7 @@ customElements.define('connect-four-timer', class extends HTMLElement {
5758
scriptune.play(`#BPM 300\nC5:s ${isPanicLevelThree ? '-:s C5:s' : ''}`)
5859
}
5960

60-
window.requestAnimationFrame(this._render);
61+
this._animationFrameId = window.requestAnimationFrame(this._render);
6162
}
6263

6364
_onPlayerJoined = e => {

src/ConnectFour/Domain/Game/Board/Size.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ final class Size
1818
public function __construct(int $width, int $height)
1919
{
2020
if ($width < 2 || $height < 2) {
21-
throw new InvalidSizeException('Width and height must be greater then 1.');
21+
throw InvalidSizeException::tooSmall($width, $height);
2222
}
2323

2424
if (($width * $height) % 2 !== 0) {
25-
throw new InvalidSizeException('Product of width and height must be an even number.');
25+
throw InvalidSizeException::productNotEven($width, $height);
2626
}
2727

2828
$this->height = $height;

src/ConnectFour/Domain/Game/Exception/ColumnAlreadyFilledException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
namespace Gaming\ConnectFour\Domain\Game\Exception;
66

7+
use Gaming\Common\Domain\Exception\Violation;
8+
use Gaming\Common\Domain\Exception\Violations;
9+
710
final class ColumnAlreadyFilledException extends GameException
811
{
12+
public function __construct()
13+
{
14+
parent::__construct(new Violations(new Violation('column_already_filled')));
15+
}
916
}

src/ConnectFour/Domain/Game/Exception/GameException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Gaming\ConnectFour\Domain\Game\Exception;
66

7-
use Exception;
7+
use Gaming\Common\Domain\Exception\DomainException;
88

9-
class GameException extends Exception
9+
class GameException extends DomainException
1010
{
1111
}

src/ConnectFour/Domain/Game/Exception/GameFinishedException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
namespace Gaming\ConnectFour\Domain\Game\Exception;
66

7+
use Gaming\Common\Domain\Exception\Violation;
8+
use Gaming\Common\Domain\Exception\Violations;
9+
710
final class GameFinishedException extends GameException
811
{
12+
public function __construct()
13+
{
14+
parent::__construct(new Violations(new Violation('game_already_finished')));
15+
}
916
}

src/ConnectFour/Domain/Game/Exception/GameNotFoundException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
namespace Gaming\ConnectFour\Domain\Game\Exception;
66

7+
use Gaming\Common\Domain\Exception\Violation;
8+
use Gaming\Common\Domain\Exception\Violations;
9+
710
final class GameNotFoundException extends GameException
811
{
12+
public function __construct()
13+
{
14+
parent::__construct(new Violations(new Violation('game_not_found')));
15+
}
916
}

src/ConnectFour/Domain/Game/Exception/GameNotRunningException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
namespace Gaming\ConnectFour\Domain\Game\Exception;
66

7+
use Gaming\Common\Domain\Exception\Violation;
8+
use Gaming\Common\Domain\Exception\Violations;
9+
710
final class GameNotRunningException extends GameException
811
{
12+
public function __construct()
13+
{
14+
parent::__construct(new Violations(new Violation('game_not_running')));
15+
}
916
}

src/ConnectFour/Domain/Game/Exception/GameRunningException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
namespace Gaming\ConnectFour\Domain\Game\Exception;
66

7+
use Gaming\Common\Domain\Exception\Violation;
8+
use Gaming\Common\Domain\Exception\Violations;
9+
710
final class GameRunningException extends GameException
811
{
12+
public function __construct()
13+
{
14+
parent::__construct(new Violations(new Violation('game_already_running')));
15+
}
916
}

src/ConnectFour/Domain/Game/Exception/InvalidSizeException.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@
44

55
namespace Gaming\ConnectFour\Domain\Game\Exception;
66

7+
use Gaming\Common\Domain\Exception\Violation;
8+
use Gaming\Common\Domain\Exception\ViolationParameter;
9+
use Gaming\Common\Domain\Exception\Violations;
10+
711
final class InvalidSizeException extends GameException
812
{
13+
public function __construct(string $identifier, int $width = 0, int $height = 0)
14+
{
15+
parent::__construct(
16+
new Violations(
17+
new Violation($identifier, [
18+
new ViolationParameter('width', $width),
19+
new ViolationParameter('height', $height)
20+
])
21+
)
22+
);
23+
}
24+
25+
public static function productNotEven(int $width, int $height): self
26+
{
27+
return new self('invalid_size.not_even', $width, $height);
28+
}
29+
30+
public static function tooSmall(int $width, int $height): self
31+
{
32+
return new self('invalid_size.too_small', $width, $height);
33+
}
934
}

src/ConnectFour/Domain/Game/Exception/NoTimeoutException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
namespace Gaming\ConnectFour\Domain\Game\Exception;
66

7+
use Gaming\Common\Domain\Exception\Violation;
8+
use Gaming\Common\Domain\Exception\Violations;
9+
710
final class NoTimeoutException extends GameException
811
{
12+
public function __construct()
13+
{
14+
parent::__construct(new Violations(new Violation('no_timeout')));
15+
}
916
}

0 commit comments

Comments
 (0)