From 6591f2cf68887e24da87fbb755e4f2c1a7ce8190 Mon Sep 17 00:00:00 2001 From: Michael Babker Date: Wed, 13 Dec 2023 17:37:06 -0500 Subject: [PATCH] Add an error URI resolver --- src/WAMP/DefaultErrorUriResolver.php | 34 +++++++++++++++++++ src/WAMP/ErrorUriResolver.php | 27 +++++++++++++++ .../Middleware/DispatchMessageToHandler.php | 7 ++-- tests/WAMP/DefaultErrorUriResolverTest.php | 30 ++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/WAMP/DefaultErrorUriResolver.php create mode 100644 src/WAMP/ErrorUriResolver.php create mode 100644 tests/WAMP/DefaultErrorUriResolverTest.php diff --git a/src/WAMP/DefaultErrorUriResolver.php b/src/WAMP/DefaultErrorUriResolver.php new file mode 100644 index 0000000..69a9f03 --- /dev/null +++ b/src/WAMP/DefaultErrorUriResolver.php @@ -0,0 +1,34 @@ +callError( $id, - 'https://example.com/error#not-found', // TODO - Make the error URI customizable + $this->errorUriResolver->resolve('not-found'), sprintf('Could not find a message handler for URI "%s".', $resolvedUri), [ 'code' => 404, @@ -126,7 +129,7 @@ public function onCall(WAMPConnection $connection, string $id, string $resolvedU } catch (WebSocketException $exception) { $connection->callError( $id, - 'https://example.com/error#not-found', // TODO - Make the error URI customizable + $this->errorUriResolver->resolve('not-found'), sprintf('Could not find a message handler for URI "%s".', $resolvedUri), [ 'code' => 404, diff --git a/tests/WAMP/DefaultErrorUriResolverTest.php b/tests/WAMP/DefaultErrorUriResolverTest.php new file mode 100644 index 0000000..245ab03 --- /dev/null +++ b/tests/WAMP/DefaultErrorUriResolverTest.php @@ -0,0 +1,30 @@ + + */ + 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)); + } +}