Skip to content

Commit 1151cb0

Browse files
committed
Adds contextual data to root exception class
1 parent ab9b1de commit 1151cb0

7 files changed

Lines changed: 71 additions & 7 deletions

File tree

src/AppConfig.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function getEnv(): Env
6565
public function withId(string $id): self
6666
{
6767
$this->id = $id;
68-
$this->getLogger()->withData(['app_id' => $id]);
68+
$this->getLogger()->addContext(['app_id' => $id]);
6969

7070
return $this;
7171
}
@@ -89,7 +89,7 @@ public function withAlias(string $alias): self
8989
$this->alias = $alias;
9090
$this->getLogger()
9191
->withName($alias)
92-
->withData(['app_name' => $alias]);
92+
->addContext(['app_name' => $alias]);
9393

9494
return $this;
9595
}
@@ -114,7 +114,7 @@ public function withLogger(LoggerInterface $logger): self
114114
{
115115
$this->logger = SlackLogger::wrap($logger)
116116
->withName($this->getAlias())
117-
->withData(array_filter([
117+
->addContext(array_filter([
118118
'app_id' => $this->getId(),
119119
'app_name' => $this->getAlias(),
120120
]));

src/Context.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function withAppConfig(AppConfig $config): self
111111

112112
// Update the Logger with Context data.
113113
$this->logger()
114-
->withData($this->payload->getSummary())
114+
->addContext($this->payload->getSummary())
115115
->debug('Incoming Slack request', [
116116
'context' => $this->toArray(),
117117
]);

src/Contexts/HasData.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ public function getOneOf(array $keys, bool $required = false)
8484
return null;
8585
}
8686

87+
/**
88+
* @param string[] $keys
89+
* @param bool $required Whether to throw an exception if any of the values are set.
90+
* @return array<string, string|array|int|float|bool|null>
91+
*/
92+
public function getAllOf(array $keys, bool $required = false): array
93+
{
94+
$values = [];
95+
$missing = [];
96+
foreach ($keys as $key) {
97+
$value = $this->get($key);
98+
if ($value === null) {
99+
$missing[] = $key;
100+
} else {
101+
$values[$key] = $value;
102+
}
103+
}
104+
105+
if ($required && !empty($missing)) {
106+
$class = static::class;
107+
$list = implode(', ', array_map(fn (string $key) => "\"{$key}\"", $missing));
108+
109+
throw new Exception("Missing required values from {$class}: all of {$list}.");
110+
}
111+
112+
return $values;
113+
}
114+
87115
/**
88116
* @param array $keys
89117
* @param array $data

src/Exception.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,27 @@
55
namespace SlackPhp\Framework;
66

77
use RuntimeException;
8+
use Throwable;
89

910
class Exception extends RuntimeException
1011
{
11-
// No difference from RuntimeException.
12+
protected array $context;
13+
14+
public function __construct($message = "", $code = 0, Throwable $previous = null, array $context = [])
15+
{
16+
$this->context = $context;
17+
parent::__construct($message, $code, $previous);
18+
}
19+
20+
public function addContext(array $context): self
21+
{
22+
$this->context = $context + $this->context;
23+
24+
return $this;
25+
}
26+
27+
public function getContext(): array
28+
{
29+
return $this->context;
30+
}
1231
}

src/Interceptors/Filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct($defaultListener = null)
2222
public function intercept(Context $context, Listener $listener): void
2323
{
2424
$matched = $this->matches($context);
25-
$context->logger()->withData(['filter:' . static::class => $matched ? 'match' : 'not-match']);
25+
$context->logger()->addContext(['filter:' . static::class => $matched ? 'match' : 'not-match']);
2626

2727
if (!$matched) {
2828
$listener = $this->defaultListener;

src/SlackLogger.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,25 @@ public function withName(?string $name): self
5858
return $this;
5959
}
6060

61+
/**
62+
* @deprecated use addContext() instead
63+
* @param array $context
64+
* @return $this
65+
*/
6166
public function withData(array $context): self
6267
{
6368
$this->context = $context + $this->context;
6469

6570
return $this;
6671
}
6772

73+
public function addContext(array $context): self
74+
{
75+
$this->context = $context + $this->context;
76+
77+
return $this;
78+
}
79+
6880
public function log($level, $message, array $context = []): void
6981
{
7082
$this->logger->log($level, "[{$this->name}] {$message}", $context + $this->context);

src/StderrLogger.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ public function log($level, $message, array $context = [])
5959

6060
// Apply special formatting for "exception" fields.
6161
if (isset($context['exception'])) {
62-
$context['exception'] = explode("\n", (string) $context['exception']);
62+
$exception = $context['exception'];
63+
if ($exception instanceof Exception) {
64+
$context = $exception->getContext() + $context;
65+
}
66+
67+
$context['exception'] = explode("\n", (string) $exception);
6368
}
6469

6570
fwrite($this->stream, json_encode(compact('level', 'message', 'context')) . "\n");

0 commit comments

Comments
 (0)