Skip to content

Commit 53a381a

Browse files
committed
Improved some of the exceptions thrown during deserialization.
1 parent 8438008 commit 53a381a

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

PhpSerializerNET.Test/TestDeserializeErrors.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public void DeserializeMalformedString() {
5454
Assert.AreEqual("Malformed string at position 0", ex.Message);
5555

5656
ex = Assert.ThrowsException<DeserializationException>(() => PhpSerialization.Deserialize("s:30:\"\";"));
57-
Assert.AreEqual("Unexpected end of data at position. The string at position 0 pointed to out of bounds index 36.", ex.Message);
57+
Assert.AreEqual("Illegal length of 30. The string at position 0 pointed to out of bounds index 36.", ex.Message);
5858

5959
ex = Assert.ThrowsException<DeserializationException>(() => PhpSerialization.Deserialize("s:1:\"\";"));
60-
Assert.AreEqual("String at position 0 has an incorrect length.", ex.Message);
60+
Assert.AreEqual("String at position 0 has an incorrect length of 1: Expected double quote at position 6, found ';' instead.", ex.Message);
6161

6262
ex = Assert.ThrowsException<DeserializationException>(() => PhpSerialization.Deserialize("s:a:\"\";"));
6363
Assert.AreEqual("Malformed string at position 0", ex.Message);

PhpSerializerNET/PhpDeserializer.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private object DeserializeToken(Type targetType, PhpSerializeToken token) {
174174
return this.MakeStruct(targetType, token);
175175
}
176176
default:
177-
throw new Exception("Unsupported datatype.");
177+
throw new DeserializationException($"Unsupported datatype {targetType.Name}.");
178178
}
179179
}
180180

@@ -199,7 +199,14 @@ private object MakeStruct(Type targetType, PhpSerializeToken token) {
199199
if (field.GetCustomAttribute<PhpIgnoreAttribute>() != null) {
200200
break;
201201
}
202-
field.SetValue(result, this.DeserializeToken(field.FieldType, valueToken));
202+
try{
203+
field.SetValue(result, this.DeserializeToken(field.FieldType, valueToken));
204+
} catch(Exception exception){
205+
throw new DeserializationException(
206+
$"Exception encountered while trying to assign '{token.Value}' to {targetType.Name}.{field.Name}. See inner exception for details.",
207+
exception
208+
);
209+
}
203210
}
204211
return result;
205212
}
@@ -226,7 +233,14 @@ private object MakeObject(Type targetType, PhpSerializeToken token) {
226233
if (property.GetCustomAttribute<PhpIgnoreAttribute>() != null) {
227234
break;
228235
}
229-
property.SetValue(result, this.DeserializeToken(property.PropertyType, valueToken));
236+
try{
237+
property.SetValue(result, this.DeserializeToken(property.PropertyType, valueToken));
238+
} catch(Exception exception){
239+
throw new DeserializationException(
240+
$"Exception encountered while trying to assign '{token.Value}' to {targetType.Name}.{property.Name}. See inner exception for details.",
241+
exception
242+
);
243+
}
230244
}
231245
return result;
232246
}
@@ -243,7 +257,7 @@ private object MakeList(Type targetType, PhpSerializeToken token) {
243257
if (token.Children[i].Type != PhpSerializerType.Integer) {
244258
throw new DeserializationException(
245259
$"Can not deserialize array at position {token.Position} to list: " +
246-
$"It has a non-integer key at element {i} (position {token.Children[i].Position})."
260+
$"It has a non-integer key '{token.Value}' at element {i} (position {token.Children[i].Position})."
247261
);
248262
}
249263
}

PhpSerializerNET/PhpSerializationException.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ namespace PhpSerializerNET {
1010
public class DeserializationException : Exception {
1111
public DeserializationException(string message) : base(message) {
1212
}
13+
14+
public DeserializationException(string message, Exception innerException) : base(message, innerException) {
15+
}
1316
}
1417
}

PhpSerializerNET/PhpTokenizer.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ internal List<PhpSerializeToken> Tokenize() {
113113
return tokens;
114114
}
115115
if (_inputBytes.Length - 1 <= _position) {
116-
throw new DeserializationException($"Unexpected end of data at position { _position + 1}");
116+
throw new DeserializationException($"Unexpected end of data at position { _position}");
117117
}
118118
switch ((char)_inputBytes[_position]) {
119119
case 'N': {
@@ -163,12 +163,15 @@ internal List<PhpSerializeToken> Tokenize() {
163163

164164
if (valueStart + length >= _inputBytes.Length) {
165165
throw new DeserializationException(
166-
$"Unexpected end of data at position. The string at position {_position} pointed to out of bounds index {valueStart + length}."
166+
$"Illegal length of {length}. " +
167+
$"The string at position {_position} points to out of bounds index {valueStart + length}."
167168
);
168169
}
169-
if (_inputBytes[valueStart + length] != '"') {
170+
char stringEnd = (char)_inputBytes[valueStart + length];
171+
if (stringEnd != '"') {
170172
throw new DeserializationException(
171-
$"String at position {_position} has an incorrect length."
173+
$"String at position {_position} has an incorrect length of {length}: "+
174+
$"Expected double quote at position {valueStart + length}, found '{stringEnd}' instead."
172175
);
173176
}
174177

@@ -223,7 +226,8 @@ internal List<PhpSerializeToken> Tokenize() {
223226
};
224227
if (arrayToken.Length != arrayToken.Children.Count / 2) {
225228
throw new DeserializationException(
226-
$"Array at position {arrayToken.Position} should be of length {arrayToken.Length}, but actual length is {arrayToken.Children.Count / 2}."
229+
$"Array at position {arrayToken.Position} should be of length {arrayToken.Length}, " +
230+
$"but actual length is {arrayToken.Children.Count / 2}."
227231
);
228232
}
229233
tokens.Add(arrayToken);

0 commit comments

Comments
 (0)