-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace BabDev\WebSocket\Server\WAMP; | ||
|
||
use BabDev\WebSocket\Server\WAMP\Middleware\DispatchMessageToHandler; | ||
|
||
/** | ||
* The error URI resolver is responsible for resolving URIs for identifying errors, | ||
* which is provided as part of the "CALLERROR" WAMP message to the client. | ||
*/ | ||
final readonly class DefaultErrorUriResolver implements ErrorUriResolver | ||
{ | ||
/** | ||
* Resolve the error URI or CURIE for the given error type. | ||
* | ||
* Because the types of errors will be application dependent other than the "handler not found" | ||
* scenario in the default {@see DispatchMessageToHandler} middleware, a predefined list of error | ||
* types is not provided by this library. However, implementations should at a minimum support the | ||
* "not-found" type, and must return a generic type if they do not support creating a URI for a | ||
* specific type of error. | ||
* | ||
* @param non-empty-string $errorType | ||
* | ||
* @return non-empty-string | ||
*/ | ||
public function resolve(string $errorType): string | ||
{ | ||
if ($errorType === 'not-found') { | ||
return 'https://example.com/error#not-found'; | ||
} | ||
|
||
return 'https://example.com/error#generic'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace BabDev\WebSocket\Server\WAMP; | ||
|
||
use BabDev\WebSocket\Server\WAMP\Middleware\DispatchMessageToHandler; | ||
|
||
/** | ||
* The error URI resolver is responsible for resolving URIs for identifying errors, | ||
* which is provided as part of the "CALLERROR" WAMP message to the client. | ||
*/ | ||
interface ErrorUriResolver | ||
{ | ||
/** | ||
* Resolve the error URI or CURIE for the given error type. | ||
* | ||
* Because the types of errors will be application dependent other than the "handler not found" | ||
* scenario in the default {@see DispatchMessageToHandler} middleware, a predefined list of error | ||
* types is not provided by this library. However, implementations should at a minimum support the | ||
* "not-found" type, and must return a generic type if they do not support creating a URI for a | ||
* specific type of error. | ||
* | ||
* @param non-empty-string $errorType | ||
* | ||
* @return non-empty-string | ||
*/ | ||
public function resolve(string $errorType): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace BabDev\WebSocket\Server\Tests\WAMP; | ||
|
||
use BabDev\WebSocket\Server\WAMP\DefaultErrorUriResolver; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DefaultErrorUriResolverTest extends TestCase | ||
{ | ||
/** | ||
* @return \Generator<string, array{non-empty-string, non-empty-string}> | ||
*/ | ||
public static function dataSupportedErrors(): \Generator | ||
{ | ||
yield '"not-found" error' => ['not-found', 'https://example.com/error#not-found']; | ||
|
||
yield 'Unknown error' => ['unknown', 'https://example.com/error#generic']; | ||
} | ||
|
||
/** | ||
* @param non-empty-string $errorType | ||
* @param non-empty-string $expected | ||
*/ | ||
#[DataProvider('dataSupportedErrors')] | ||
public function testResolve(string $errorType, string $expected): void | ||
{ | ||
$this->assertSame($expected, (new DefaultErrorUriResolver())->resolve($errorType)); | ||
} | ||
} |