Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit b7d6bb7

Browse files
authored
Merge pull request #51 from aldump/issue-50/debug-mode-for-server
Server error details #50
2 parents 33f8cc2 + 49c351c commit b7d6bb7

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

example/server/worker.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ini_set('display_errors', 'stderr');
1010
require "vendor/autoload.php";
1111

12+
//To run server in debug mode - new \Spiral\GRPC\Server(null, ['debug' => true]);
1213
$server = new \Spiral\GRPC\Server();
1314
$server->registerService(\Service\EchoInterface::class, new EchoService());
1415

src/Server.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ final class Server
2929
/** @var ServiceWrapper[] */
3030
private $services = [];
3131

32+
/**
33+
* @var array
34+
*/
35+
private $options;
36+
3237
/**
3338
* @param InvokerInterface|null $invoker
39+
* @param array $options
3440
*/
35-
public function __construct(InvokerInterface $invoker = null)
41+
public function __construct(InvokerInterface $invoker = null, array $options = [])
3642
{
3743
$this->invoker = $invoker ?? new Invoker();
44+
$this->options = $options;
3845
}
3946

4047
/**
@@ -86,7 +93,13 @@ public function serve(Worker $worker, callable $finalize = null): void
8693
} catch (GRPCException $e) {
8794
$worker->error($this->packError($e));
8895
} catch (\Throwable $e) {
89-
$worker->error((string)$e);
96+
if ($this->isDebugMode()) {
97+
$errorText = $e->__toString();
98+
} else {
99+
$errorText = $e->getMessage();
100+
}
101+
102+
$worker->error($errorText);
90103
} finally {
91104
if ($finalize !== null) {
92105
call_user_func($finalize, $e ?? null);
@@ -148,4 +161,20 @@ private function packError(GRPCException $e): string
148161

149162
return implode('|:|', $data);
150163
}
164+
165+
/**
166+
* If server runs in debug mode
167+
*
168+
* @return bool
169+
*/
170+
private function isDebugMode(): bool
171+
{
172+
$debug = false;
173+
174+
if (isset($this->options['debug'])) {
175+
$debug = filter_var($this->options['debug'], FILTER_VALIDATE_BOOLEAN);
176+
}
177+
178+
return $debug;
179+
}
151180
}

tests/GRPC/ServerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ public function testNotFound2(): void
8585
$this->assertTrue($w->done());
8686
}
8787

88+
public function testServerDebugModeNotEnabled(): void
89+
{
90+
$s = new Server();
91+
$s->registerService(TestInterface::class, new TestService());
92+
93+
$w = new TestWorker($this, [
94+
[
95+
'ctx' => [
96+
'service' => 'service.Test',
97+
'method' => 'Throw',
98+
'context' => [],
99+
],
100+
'send' => $this->packMessage('regularException'),
101+
'error' => 'Just another exception'
102+
]
103+
]);
104+
105+
$s->serve($w);
106+
107+
$this->assertTrue($w->done());
108+
}
109+
88110
private function packMessage(string $message): string
89111
{
90112
$m = new Message();

tests/src/Test/TestService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public function Throw(ContextInterface $ctx, Message $in): Message
3333
$grpcException = new GRPCException("main exception message", 3, [$detailsMessage]);
3434

3535
throw $grpcException;
36+
case "regularException": {
37+
throw new \Exception("Just another exception");
38+
}
3639
}
3740

3841
return $out;

0 commit comments

Comments
 (0)