Skip to content

Commit dd35ad4

Browse files
committed
Introduce domain exceptions for decoding and encoding
1 parent 00592ae commit dd35ad4

File tree

8 files changed

+77
-24
lines changed

8 files changed

+77
-24
lines changed

src/Codec/CodecLibrary.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
namespace MongoDB\Codec;
1919

2020
use MongoDB\Exception\InvalidArgumentException;
21-
use MongoDB\Exception\UnexpectedValueException;
22-
23-
use function get_debug_type;
24-
use function sprintf;
21+
use MongoDB\Exception\UnsupportedValueException;
2522

2623
class CodecLibrary implements Codec
2724
{
@@ -129,7 +126,7 @@ final public function decode($value)
129126
}
130127
}
131128

132-
throw new UnexpectedValueException(sprintf('No decoder found for value of type "%s"', get_debug_type($value)));
129+
throw UnsupportedValueException::invalidDecodableValue($value);
133130
}
134131

135132
/**
@@ -144,6 +141,6 @@ final public function encode($value)
144141
}
145142
}
146143

147-
throw new UnexpectedValueException(sprintf('No encoder found for value of type "%s"', get_debug_type($value)));
144+
throw UnsupportedValueException::invalidEncodableValue($value);
148145
}
149146
}

src/Codec/DecodeIfSupported.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace MongoDB\Codec;
1919

20+
use MongoDB\Exception\UnsupportedValueException;
21+
2022
/**
2123
* @psalm-template BSONType
2224
* @psalm-template NativeType
@@ -34,6 +36,7 @@ abstract public function canDecode($value): bool;
3436
* @psalm-param BSONType $value
3537
* @return mixed
3638
* @psalm-return NativeType
39+
* @throws UnsupportedValueException if the decoder does not support the value
3740
*/
3841
abstract public function decode($value);
3942

src/Codec/Decoder.php

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

1818
namespace MongoDB\Codec;
1919

20-
use MongoDB\Exception\InvalidArgumentException;
20+
use MongoDB\Exception\UnsupportedValueException;
2121

2222
/**
2323
* @psalm-template BSONType
@@ -41,7 +41,7 @@ public function canDecode($value): bool;
4141
* @psalm-param BSONType $value
4242
* @return mixed
4343
* @psalm-return NativeType
44-
* @throws InvalidArgumentException if the decoder does not support the value
44+
* @throws UnsupportedValueException if the decoder does not support the value
4545
*/
4646
public function decode($value);
4747

src/Codec/DocumentCodec.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace MongoDB\Codec;
1919

2020
use MongoDB\BSON\Document;
21+
use MongoDB\Exception\UnsupportedValueException;
2122

2223
/**
2324
* The DocumentCodec interface allows decoding BSON document data to native PHP
@@ -32,12 +33,14 @@ interface DocumentCodec extends Codec
3233
* @param mixed $value
3334
* @psalm-param Document $value
3435
* @psalm-return ObjectType
36+
* @throws UnsupportedValueException if the decoder does not support the value
3537
*/
3638
public function decode($value): object;
3739

3840
/**
3941
* @param mixed $value
4042
* @psalm-param ObjectType $value
43+
* @throws UnsupportedValueException if the encoder does not support the value
4144
*/
4245
public function encode($value): Document;
4346
}

src/Codec/EncodeIfSupported.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace MongoDB\Codec;
1919

20+
use MongoDB\Exception\UnsupportedValueException;
21+
2022
/**
2123
* @psalm-template BSONType
2224
* @psalm-template NativeType
@@ -34,6 +36,7 @@ abstract public function canEncode($value): bool;
3436
* @psalm-param NativeType $value
3537
* @return mixed
3638
* @psalm-return BSONType
39+
* @throws UnsupportedValueException if the encoder does not support the value
3740
*/
3841
abstract public function encode($value);
3942

src/Codec/Encoder.php

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

1818
namespace MongoDB\Codec;
1919

20-
use MongoDB\Exception\InvalidArgumentException;
20+
use MongoDB\Exception\UnsupportedValueException;
2121

2222
/**
2323
* @psalm-template BSONType
@@ -41,7 +41,7 @@ public function canEncode($value): bool;
4141
* @psalm-param NativeType $value
4242
* @return mixed
4343
* @psalm-return BSONType
44-
* @throws InvalidArgumentException if the decoder does not support the value
44+
* @throws UnsupportedValueException if the encoder does not support the value
4545
*/
4646
public function encode($value);
4747

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/*
3+
* Copyright 2023-present MongoDB, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace MongoDB\Exception;
19+
20+
use InvalidArgumentException;
21+
22+
use function get_debug_type;
23+
use function sprintf;
24+
25+
class UnsupportedValueException extends InvalidArgumentException implements Exception
26+
{
27+
/** @var mixed */
28+
private $value;
29+
30+
/** @return mixed */
31+
public function getValue()
32+
{
33+
return $this->value;
34+
}
35+
36+
/** @param mixed $value */
37+
public static function invalidDecodableValue($value): self
38+
{
39+
return new self(sprintf('Could not decode value of type "%s".', get_debug_type($value)), $value);
40+
}
41+
42+
/** @param mixed $value */
43+
public static function invalidEncodableValue($value): self
44+
{
45+
return new self(sprintf('Could not encode value of type "%s".', get_debug_type($value)), $value);
46+
}
47+
48+
/** @param mixed $value */
49+
private function __construct(string $message, $value)
50+
{
51+
parent::__construct($message);
52+
53+
$this->value = $value;
54+
}
55+
}

tests/Codec/CodecLibraryTest.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use MongoDB\Codec\DecodeIfSupported;
88
use MongoDB\Codec\EncodeIfSupported;
99
use MongoDB\Codec\KnowsCodecLibrary;
10-
use MongoDB\Exception\UnexpectedValueException;
10+
use MongoDB\Exception\UnsupportedValueException;
1111
use MongoDB\Tests\TestCase;
1212

1313
class CodecLibraryTest extends TestCase
@@ -36,17 +36,13 @@ public function testDecodeNull(): void
3636

3737
$this->assertFalse($codec->canDecode(null));
3838

39-
$this->expectException(UnexpectedValueException::class);
40-
$this->expectExceptionMessage('No decoder found for value of type "null"');
41-
42-
$this->assertNull($codec->decode(null));
39+
$this->expectExceptionObject(UnsupportedValueException::invalidDecodableValue(null));
40+
$codec->decode(null);
4341
}
4442

4543
public function testDecodeUnsupportedValue(): void
4644
{
47-
$this->expectException(UnexpectedValueException::class);
48-
$this->expectExceptionMessage('No decoder found for value of type "string"');
49-
45+
$this->expectExceptionObject(UnsupportedValueException::invalidDecodableValue('foo'));
5046
$this->getCodecLibrary()->decode('foo');
5147
}
5248

@@ -74,17 +70,13 @@ public function testEncodeNull(): void
7470

7571
$this->assertFalse($codec->canEncode(null));
7672

77-
$this->expectException(UnexpectedValueException::class);
78-
$this->expectExceptionMessage('No encoder found for value of type "null"');
79-
73+
$this->expectExceptionObject(UnsupportedValueException::invalidEncodableValue(null));
8074
$codec->encode(null);
8175
}
8276

8377
public function testEncodeUnsupportedValue(): void
8478
{
85-
$this->expectException(UnexpectedValueException::class);
86-
$this->expectExceptionMessage('No encoder found for value of type "string"');
87-
79+
$this->expectExceptionObject(UnsupportedValueException::invalidEncodableValue('foo'));
8880
$this->getCodecLibrary()->encode('foo');
8981
}
9082

0 commit comments

Comments
 (0)