Skip to content

Commit

Permalink
Remove InvalidCodeException
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Leonovich committed May 4, 2018
1 parent 531486e commit 8cb8cd7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 68 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,10 @@ $date = $unpacker->reset($packed)->unpack();
If an error occurs during packing/unpacking, a `PackingFailedException` or `UnpackingFailedException` will be thrown,
respectively.

In addition, there are three more exceptions that can be thrown during unpacking:
In addition, there are two more exceptions that can be thrown during unpacking:

* `InsufficientDataException`
* `IntegerOverflowException`
* `InvalidCodeException`

An `InvalidOptionException` will be thrown in case an invalid option (or a combination of mutually exclusive options)
is used.
Expand Down
22 changes: 11 additions & 11 deletions src/BufferUnpacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use MessagePack\Exception\InsufficientDataException;
use MessagePack\Exception\IntegerOverflowException;
use MessagePack\Exception\InvalidCodeException;
use MessagePack\Exception\UnpackingFailedException;
use MessagePack\TypeTransformer\Extension;

class BufferUnpacker
Expand Down Expand Up @@ -178,7 +178,7 @@ public function unpack()
case 0xc9: return $this->unpackExtData($this->unpackUint32());
}

throw InvalidCodeException::unknownCode($c);
throw UnpackingFailedException::unknownCode($c);
}

public function unpackNil()
Expand All @@ -193,7 +193,7 @@ public function unpackNil()
return null;
}

throw InvalidCodeException::mismatchedType('nil', \ord($this->buffer[$this->offset++]));
throw UnpackingFailedException::unexpectedCode(\ord($this->buffer[$this->offset++]), 'nil');
}

public function unpackBool()
Expand All @@ -212,7 +212,7 @@ public function unpackBool()
return true;
}

throw InvalidCodeException::mismatchedType('bool', $c);
throw UnpackingFailedException::unexpectedCode($c, 'bool');
}

public function unpackInt()
Expand Down Expand Up @@ -247,7 +247,7 @@ public function unpackInt()
case 0xd3: return $this->unpackInt64();
}

throw InvalidCodeException::mismatchedType('int', $c);
throw UnpackingFailedException::unexpectedCode($c, 'int');
}

public function unpackFloat()
Expand All @@ -266,7 +266,7 @@ public function unpackFloat()
return $this->unpackFloat32();
}

throw InvalidCodeException::mismatchedType('float', $c);
throw UnpackingFailedException::unexpectedCode($c, 'float');
}

public function unpackStr()
Expand All @@ -291,7 +291,7 @@ public function unpackStr()
return $this->unpackStrData($this->unpackUint32());
}

throw InvalidCodeException::mismatchedType('str', $c);
throw UnpackingFailedException::unexpectedCode($c, 'str');
}

public function unpackBin()
Expand All @@ -313,7 +313,7 @@ public function unpackBin()
return $this->unpackStrData($this->unpackUint32());
}

throw InvalidCodeException::mismatchedType('bin', $c);
throw UnpackingFailedException::unexpectedCode($c, 'bin');
}

public function unpackArray()
Expand Down Expand Up @@ -347,7 +347,7 @@ public function unpackArrayHeader()
return $this->unpackUint32();
}

throw InvalidCodeException::mismatchedType('array header', $c);
throw UnpackingFailedException::unexpectedCode($c, 'array header');
}

public function unpackMap()
Expand Down Expand Up @@ -381,7 +381,7 @@ public function unpackMapHeader()
return $this->unpackUint32();
}

throw InvalidCodeException::mismatchedType('map header', $c);
throw UnpackingFailedException::unexpectedCode($c, 'map header');
}

public function unpackExt()
Expand All @@ -404,7 +404,7 @@ public function unpackExt()
case 0xc9: return $this->unpackExtData($this->unpackUint32());
}

throw InvalidCodeException::mismatchedType('ext header', $c);
throw UnpackingFailedException::unexpectedCode($c, 'ext header');
}

private function unpackUint8()
Expand Down
25 changes: 0 additions & 25 deletions src/Exception/InvalidCodeException.php

This file was deleted.

9 changes: 9 additions & 0 deletions src/Exception/UnpackingFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@

class UnpackingFailedException extends \RuntimeException
{
public static function unknownCode(int $code) : self
{
return new self(\sprintf('Unknown code: 0x%x.', $code));
}

public static function unexpectedCode(int $code, string $type) : self
{
return new self(\sprintf('Unexpected %s code: 0x%x.', $type, $code));
}
}
50 changes: 20 additions & 30 deletions tests/Unit/BufferUnpackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use MessagePack\BufferUnpacker;
use MessagePack\Exception\InsufficientDataException;
use MessagePack\Exception\IntegerOverflowException;
use MessagePack\Exception\InvalidCodeException;
use MessagePack\Exception\InvalidOptionException;
use MessagePack\Exception\UnpackingFailedException;
use MessagePack\Ext;
use MessagePack\TypeTransformer\Extension;
use MessagePack\UnpackOptions;
Expand Down Expand Up @@ -90,9 +90,8 @@ public function provideInsufficientData() : array

public function testUnpackUnknownCode() : void
{
$this->expectException(InvalidCodeException::class);
$this->expectException(UnpackingFailedException::class);
$this->expectExceptionMessage('Unknown code: 0xc1.');
$this->expectExceptionCode(193);

$this->unpacker->reset("\xc1")->unpack();
}
Expand Down Expand Up @@ -291,9 +290,8 @@ public function testUnpackInsufficientNil() : void

public function testUnpackInvalidNil() : void
{
$this->expectException(InvalidCodeException::class);
$this->expectExceptionMessage('Invalid nil code: 0xc1.');
$this->expectExceptionCode(193);
$this->expectException(UnpackingFailedException::class);
$this->expectExceptionMessage('Unexpected nil code: 0xc1.');

$this->unpacker->reset("\xc1")->unpackNil();
}
Expand All @@ -316,9 +314,8 @@ public function testUnpackInsufficientBool() : void

public function testUnpackInvalidBool() : void
{
$this->expectException(InvalidCodeException::class);
$this->expectExceptionMessage('Invalid bool code: 0xc1.');
$this->expectExceptionCode(193);
$this->expectException(UnpackingFailedException::class);
$this->expectExceptionMessage('Unexpected bool code: 0xc1.');

$this->unpacker->reset("\xc1")->unpackBool();
}
Expand All @@ -341,9 +338,8 @@ public function testUnpackInsufficientInt() : void

public function testUnpackInvalidInt() : void
{
$this->expectException(InvalidCodeException::class);
$this->expectExceptionMessage('Invalid int code: 0xc1.');
$this->expectExceptionCode(193);
$this->expectException(UnpackingFailedException::class);
$this->expectExceptionMessage('Unexpected int code: 0xc1.');

$this->unpacker->reset("\xc1")->unpackInt();
}
Expand All @@ -366,9 +362,8 @@ public function testUnpackInsufficientFloat() : void

public function testUnpackInvalidFloat() : void
{
$this->expectException(InvalidCodeException::class);
$this->expectExceptionMessage('Invalid float code: 0xc1.');
$this->expectExceptionCode(193);
$this->expectException(UnpackingFailedException::class);
$this->expectExceptionMessage('Unexpected float code: 0xc1.');

$this->unpacker->reset("\xc1")->unpackFloat();
}
Expand All @@ -391,9 +386,8 @@ public function testUnpackInsufficientStr() : void

public function testUnpackInvalidStr() : void
{
$this->expectException(InvalidCodeException::class);
$this->expectExceptionMessage('Invalid str code: 0xc1.');
$this->expectExceptionCode(193);
$this->expectException(UnpackingFailedException::class);
$this->expectExceptionMessage('Unexpected str code: 0xc1.');

$this->unpacker->reset("\xc1")->unpackStr();
}
Expand All @@ -416,9 +410,8 @@ public function testUnpackInsufficientBin() : void

public function testUnpackInvalidBin() : void
{
$this->expectException(InvalidCodeException::class);
$this->expectExceptionMessage('Invalid bin code: 0xc1.');
$this->expectExceptionCode(193);
$this->expectException(UnpackingFailedException::class);
$this->expectExceptionMessage('Unexpected bin code: 0xc1.');

$this->unpacker->reset("\xc1")->unpackBin();
}
Expand All @@ -441,9 +434,8 @@ public function testUnpackInsufficientArray() : void

public function testUnpackInvalidArray() : void
{
$this->expectException(InvalidCodeException::class);
$this->expectExceptionMessage('Invalid array header code: 0xc1.');
$this->expectExceptionCode(193);
$this->expectException(UnpackingFailedException::class);
$this->expectExceptionMessage('Unexpected array header code: 0xc1.');

$this->unpacker->reset("\xc1")->unpackArray();
}
Expand All @@ -466,9 +458,8 @@ public function testUnpackInsufficientMap() : void

public function testUnpackInvalidMap() : void
{
$this->expectException(InvalidCodeException::class);
$this->expectExceptionMessage('Invalid map header code: 0xc1.');
$this->expectExceptionCode(193);
$this->expectException(UnpackingFailedException::class);
$this->expectExceptionMessage('Unexpected map header code: 0xc1.');

$this->unpacker->reset("\xc1")->unpackMap();
}
Expand All @@ -491,9 +482,8 @@ public function testUnpackInsufficientExt() : void

public function testUnpackInvalidExt() : void
{
$this->expectException(InvalidCodeException::class);
$this->expectExceptionMessage('Invalid ext header code: 0xc1.');
$this->expectExceptionCode(193);
$this->expectException(UnpackingFailedException::class);
$this->expectExceptionMessage('Unexpected ext header code: 0xc1.');

$this->unpacker->reset("\xc1")->unpackExt();
}
Expand Down

0 comments on commit 8cb8cd7

Please sign in to comment.