Skip to content

Commit 466405e

Browse files
committed
Update all exceptions to be named appropriately
1 parent a50004e commit 466405e

19 files changed

+83
-69
lines changed

src/Connection/ImapConnection.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
use DirectoryTree\ImapEngine\Connection\Streams\StreamInterface;
1313
use DirectoryTree\ImapEngine\Connection\Tokens\Token;
1414
use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier;
15-
use DirectoryTree\ImapEngine\Exceptions\CommandFailedException;
16-
use DirectoryTree\ImapEngine\Exceptions\ConnectionClosedException;
17-
use DirectoryTree\ImapEngine\Exceptions\ConnectionFailedException;
18-
use DirectoryTree\ImapEngine\Exceptions\ConnectionTimedOutException;
15+
use DirectoryTree\ImapEngine\Exceptions\ImapCommandException;
16+
use DirectoryTree\ImapEngine\Exceptions\ImapConnectionClosedException;
17+
use DirectoryTree\ImapEngine\Exceptions\ImapConnectionException;
18+
use DirectoryTree\ImapEngine\Exceptions\ImapConnectionFailedException;
19+
use DirectoryTree\ImapEngine\Exceptions\ImapConnectionTimedOutException;
1920
use DirectoryTree\ImapEngine\Exceptions\Exception;
21+
use DirectoryTree\ImapEngine\Exceptions\ImapResponseException;
22+
use DirectoryTree\ImapEngine\Exceptions\ImapStreamException;
2023
use DirectoryTree\ImapEngine\Exceptions\RuntimeException;
2124
use DirectoryTree\ImapEngine\Support\Str;
2225
use Generator;
26+
use LogicException;
2327

2428
class ImapConnection implements ConnectionInterface
2529
{
@@ -88,7 +92,7 @@ public function connect(string $host, ?int $port = null, array $options = []): v
8892
$this->assertNextResponse(
8993
fn (Response $response) => $response instanceof UntaggedResponse,
9094
fn (UntaggedResponse $response) => $response->type()->is('OK'),
91-
fn () => new ConnectionFailedException("Connection to $host:$port failed")
95+
fn () => new ImapConnectionFailedException("Connection to $host:$port failed")
9296
);
9397

9498
if ($transport === 'starttls') {
@@ -150,7 +154,7 @@ public function login(string $user, string $password): TaggedResponse
150154
$this->send('LOGIN', Str::literal([$user, $password]), $tag);
151155

152156
return $this->assertTaggedResponse($tag, fn (TaggedResponse $response) => (
153-
CommandFailedException::make($this->result->command()->redacted(), $response)
157+
ImapCommandException::make($this->result->command()->redacted(), $response)
154158
));
155159
}
156160

@@ -177,7 +181,7 @@ public function authenticate(string $user, string $token): TaggedResponse
177181
$this->send('AUTHENTICATE', ['XOAUTH2', Str::credentials($user, $token)], $tag);
178182

179183
return $this->assertTaggedResponse($tag, fn (TaggedResponse $response) => (
180-
CommandFailedException::make($this->result->command()->redacted(), $response)
184+
ImapCommandException::make($this->result->command()->redacted(), $response)
181185
));
182186
}
183187

@@ -190,11 +194,7 @@ public function startTls(): void
190194

191195
$this->assertTaggedResponse($tag);
192196

193-
$this->stream->setSocketSetCrypto(true, match (true) {
194-
defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT') => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
195-
defined('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT') => STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT,
196-
default => STREAM_CRYPTO_METHOD_TLS_CLIENT,
197-
});
197+
$this->stream->setSocketSetCrypto(true, STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT);
198198
}
199199

200200
/**
@@ -522,7 +522,7 @@ public function idle(int $timeout): Generator
522522
$this->assertNextResponse(
523523
fn (Response $response) => $response instanceof ContinuationResponse,
524524
fn (ContinuationResponse $response) => true,
525-
fn (ContinuationResponse $response) => CommandFailedException::make(new ImapCommand('', 'IDLE'), $response),
525+
fn (ContinuationResponse $response) => ImapCommandException::make(new ImapCommand('', 'IDLE'), $response),
526526
);
527527

528528
while ($response = $this->nextReply()) {
@@ -543,7 +543,7 @@ public function done(): void
543543
$this->assertNextResponse(
544544
fn (Response $response) => $response instanceof TaggedResponse,
545545
fn (TaggedResponse $response) => $response->successful(),
546-
fn (TaggedResponse $response) => CommandFailedException::make(new ImapCommand('', 'DONE'), $response),
546+
fn (TaggedResponse $response) => ImapCommandException::make(new ImapCommand('', 'DONE'), $response),
547547
);
548548
}
549549

@@ -575,7 +575,7 @@ public function send(string $name, array $tokens = [], ?string &$tag = null): vo
575575
protected function write(string $data): void
576576
{
577577
if ($this->stream->fwrite($data."\r\n") === false) {
578-
throw new RuntimeException('Failed to write data to stream');
578+
throw new ImapStreamException('Failed to write data to stream');
579579
}
580580

581581
$this->logger?->sent($data);
@@ -658,7 +658,7 @@ protected function assertTaggedResponse(string $tag, ?callable $exception = null
658658
$response->successful()
659659
),
660660
$exception ?? fn (TaggedResponse $response) => (
661-
CommandFailedException::make($this->result->command(), $response)
661+
ImapCommandException::make($this->result->command(), $response)
662662
),
663663
);
664664
}
@@ -681,7 +681,7 @@ protected function assertNextResponse(callable $filter, callable $assertion, cal
681681
throw $exception($response);
682682
}
683683

684-
throw new RuntimeException('No matching response found');
684+
throw new ImapResponseException('No matching response found');
685685
}
686686

687687
/**
@@ -695,7 +695,7 @@ protected function assertNextResponse(callable $filter, callable $assertion, cal
695695
protected function nextResponse(callable $filter): Response
696696
{
697697
if (! $this->parser) {
698-
throw new RuntimeException('No parser instance set');
698+
throw new LogicException('No parser instance set');
699699
}
700700

701701
while ($response = $this->nextReply()) {
@@ -710,7 +710,7 @@ protected function nextResponse(callable $filter): Response
710710
}
711711
}
712712

713-
throw new RuntimeException('No matching response found');
713+
throw new ImapResponseException('No matching response found');
714714
}
715715

716716
/**
@@ -722,9 +722,9 @@ protected function nextReply(): Data|Token|Response|null
722722
$meta = $this->stream->meta();
723723

724724
throw match (true) {
725-
$meta['timed_out'] ?? false => new ConnectionTimedOutException('Stream timed out, no response'),
726-
$meta['eof'] ?? false => new ConnectionClosedException('Server closed the connection (EOF)'),
727-
default => new RuntimeException('Unknown read error, no response: '.json_encode($meta)),
725+
$meta['timed_out'] ?? false => new ImapConnectionTimedOutException('Stream timed out, no response'),
726+
$meta['eof'] ?? false => new ImapConnectionClosedException('Server closed the connection (EOF)'),
727+
default => new ImapConnectionException('Unknown stream error. Metadata: '.json_encode($meta)),
728728
};
729729
}
730730

src/Connection/ImapParser.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use DirectoryTree\ImapEngine\Connection\Tokens\ResponseCodeClose;
1717
use DirectoryTree\ImapEngine\Connection\Tokens\ResponseCodeOpen;
1818
use DirectoryTree\ImapEngine\Connection\Tokens\Token;
19-
use DirectoryTree\ImapEngine\Exceptions\ImapParseException;
19+
use DirectoryTree\ImapEngine\Exceptions\ImapParserException;
2020

2121
class ImapParser
2222
{
@@ -55,7 +55,7 @@ public function next(): Data|Token|Response|null
5555
}
5656

5757
if (! $this->currentToken) {
58-
throw new ImapParseException('Empty response');
58+
throw new ImapParserException('Empty response');
5959
}
6060

6161
// If the token indicates the beginning of a list, parse it.
@@ -105,7 +105,7 @@ protected function parseUntaggedResponse(): UntaggedResponse
105105
if ($this->currentToken && $this->isEndOfResponseToken($this->currentToken)) {
106106
$this->currentToken = null;
107107
} else {
108-
throw new ImapParseException('Unterminated untagged response');
108+
throw new ImapParserException('Unterminated untagged response');
109109
}
110110

111111
return new UntaggedResponse($elements);
@@ -134,7 +134,7 @@ protected function parseContinuationResponse(): ContinuationResponse
134134
if ($this->currentToken && $this->isEndOfResponseToken($this->currentToken)) {
135135
$this->currentToken = null;
136136
} else {
137-
throw new ImapParseException('Unterminated continuation response');
137+
throw new ImapParserException('Unterminated continuation response');
138138
}
139139

140140
return new ContinuationResponse($elements);
@@ -161,7 +161,7 @@ protected function parseTaggedResponse(): TaggedResponse
161161
if ($this->currentToken && $this->isEndOfResponseToken($this->currentToken)) {
162162
$this->currentToken = null;
163163
} else {
164-
throw new ImapParseException('Unterminated tagged response');
164+
throw new ImapParserException('Unterminated tagged response');
165165
}
166166

167167
return new TaggedResponse($tokens);
@@ -186,7 +186,7 @@ protected function parseBracketGroup(): ResponseCodeData
186186
}
187187

188188
if ($this->currentToken === null) {
189-
throw new ImapParseException('Unterminated bracket group in response');
189+
throw new ImapParserException('Unterminated bracket group in response');
190190
}
191191

192192
// Consume the closing ']' token.
@@ -200,7 +200,7 @@ protected function parseBracketGroup(): ResponseCodeData
200200
*
201201
* Lists are handled recursively: a list may contain nested lists.
202202
*
203-
* @throws ImapParseException if the list is unterminated.
203+
* @throws ImapParserException if the list is unterminated.
204204
*/
205205
protected function parseList(): ListData
206206
{
@@ -220,7 +220,7 @@ protected function parseList(): ListData
220220

221221
// If we reached the end without finding a closing ')', throw an exception.
222222
if ($this->currentToken === null) {
223-
throw new ImapParseException('Unterminated list in response');
223+
throw new ImapParserException('Unterminated list in response');
224224
}
225225

226226
// Consume the closing ')' token.

src/Connection/ImapTokenizer.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use DirectoryTree\ImapEngine\Connection\Tokens\ResponseCodeClose;
1414
use DirectoryTree\ImapEngine\Connection\Tokens\ResponseCodeOpen;
1515
use DirectoryTree\ImapEngine\Connection\Tokens\Token;
16-
use DirectoryTree\ImapEngine\Exceptions\ImapParseException;
16+
use DirectoryTree\ImapEngine\Exceptions\ImapParserException;
1717
use DirectoryTree\ImapEngine\Exceptions\ImapStreamException;
1818

1919
class ImapTokenizer
@@ -73,7 +73,7 @@ public function nextToken(): ?Token
7373
$this->ensureBuffer(1);
7474

7575
if ($this->currentChar() !== "\n") {
76-
throw new ImapParseException('Expected LF after CR');
76+
throw new ImapParserException('Expected LF after CR');
7777
}
7878

7979
$this->advance(); // Consume LF (\n)
@@ -177,7 +177,7 @@ protected function readQuotedString(): QuotedString
177177
$char = $this->currentChar();
178178

179179
if ($char === null) {
180-
throw new ImapParseException(sprintf(
180+
throw new ImapParserException(sprintf(
181181
'Unterminated quoted string at buffer offset %d. Buffer: "%s"',
182182
$this->position,
183183
substr($this->buffer, max(0, $this->position - 10), 20)
@@ -192,7 +192,7 @@ protected function readQuotedString(): QuotedString
192192
$escapedChar = $this->currentChar();
193193

194194
if ($escapedChar === null) {
195-
throw new ImapParseException('Unterminated escape sequence in quoted string');
195+
throw new ImapParserException('Unterminated escape sequence in quoted string');
196196
}
197197

198198
$value .= $escapedChar;
@@ -236,7 +236,7 @@ protected function readLiteral(): Literal
236236
$char = $this->currentChar();
237237

238238
if ($char === null) {
239-
throw new ImapParseException('Unterminated literal specifier');
239+
throw new ImapParserException('Unterminated literal specifier');
240240
}
241241

242242
if ($char === '}') {
@@ -257,7 +257,7 @@ protected function readLiteral(): Literal
257257
$crlf = substr($this->buffer, $this->position, 2);
258258

259259
if ($crlf !== "\r\n") {
260-
throw new ImapParseException('Expected CRLF after literal specifier');
260+
throw new ImapParserException('Expected CRLF after literal specifier');
261261
}
262262

263263
// Skip the CRLF.
@@ -296,7 +296,7 @@ protected function readLiteral(): Literal
296296

297297
// Verify that the literal length matches the expected length.
298298
if (strlen($literal) !== $length) {
299-
throw new ImapParseException(sprintf(
299+
throw new ImapParserException(sprintf(
300300
'Literal length mismatch: expected %d, got %d',
301301
$length,
302302
strlen($literal)
@@ -353,7 +353,7 @@ protected function readEmailAddress(): ?EmailAddress
353353
$char = $this->currentChar();
354354

355355
if ($char === null) {
356-
throw new ImapParseException('Unterminated email address, expected ">"');
356+
throw new ImapParserException('Unterminated email address, expected ">"');
357357
}
358358

359359
if ($char === '>') {

src/Connection/Streams/ImapStream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace DirectoryTree\ImapEngine\Connection\Streams;
44

5-
use DirectoryTree\ImapEngine\Exceptions\ConnectionFailedException;
5+
use DirectoryTree\ImapEngine\Exceptions\ImapConnectionFailedException;
66

77
class ImapStream implements StreamInterface
88
{
@@ -28,7 +28,7 @@ public function open(string $transport, string $host, int $port, int $timeout, a
2828
);
2929

3030
if (! $this->stream) {
31-
throw new ConnectionFailedException('Stream failed to open: '.$errstr, $errno);
31+
throw new ImapConnectionFailedException('Stream failed to open: '.$errstr, $errno);
3232
}
3333

3434
return true;

src/Exceptions/ConnectionTimedOutException.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/Exceptions/ConnectionClosedException.php renamed to src/Exceptions/ImapCapabilityException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace DirectoryTree\ImapEngine\Exceptions;
44

5-
class ConnectionClosedException extends Exception {}
5+
class ImapCapabilityException extends Exception {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use DirectoryTree\ImapEngine\Connection\ImapCommand;
66
use DirectoryTree\ImapEngine\Connection\Responses\Response;
77

8-
class CommandFailedException extends Exception
8+
class ImapCommandException extends Exception
99
{
1010
/**
1111
* Make a new instance from a failed command and response.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace DirectoryTree\ImapEngine\Exceptions;
4+
5+
class ImapConnectionClosedException extends Exception {}

src/Exceptions/ConnectionFailedException.php renamed to src/Exceptions/ImapConnectionException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace DirectoryTree\ImapEngine\Exceptions;
44

5-
class ConnectionFailedException extends Exception {}
5+
class ImapConnectionException extends Exception {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace DirectoryTree\ImapEngine\Exceptions;
4+
5+
class ImapConnectionFailedException extends Exception {}

0 commit comments

Comments
 (0)