Skip to content

Commit 415bdb4

Browse files
committed
Handle format exceptions and provide some more information.
1 parent 3286f1e commit 415bdb4

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Future:
22
- Improved performance of the validation step of deserialization.
33
- Speed up deserializing into explicit types (particularly structs and classes) significantly.
4-
- Improved exception messages on malformed input when deserializing.
4+
- Improved exception messages on malformed inputs when deserializing.
5+
- Cleaner exception when trying to deserialize into incompatible types (i.e. "a" to int32.).
56

67
# 0.7.4:
78
- Improved deserialization performance.

PhpSerializerNET.Test/Deserialize/Options/EmptyStringToDefault.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,38 @@ public void Enabled_StringToDouble() {
2929

3030
[TestMethod]
3131
public void Disabled_EmptyStringToInt() {
32-
var ex = Assert.ThrowsException<System.FormatException>(
32+
var exception = Assert.ThrowsException<DeserializationException>(
3333
() => PhpSerialization.Deserialize<int>("s:0:\"\";", new PhpDeserializationOptions(){EmptyStringToDefault = false})
3434
);
35-
// TODO: Rethrow the exception in the code with a useful error message, then test that here.
35+
36+
Assert.AreEqual(
37+
"Exception encountered while trying to assign '' to type Int32. See inner exception for details.",
38+
exception.Message
39+
);
3640
}
3741

3842
[TestMethod]
3943
public void Disabled_StringToBool() {
40-
var ex = Assert.ThrowsException<System.FormatException>(
44+
var exception = Assert.ThrowsException<DeserializationException>(
4145
() => PhpSerialization.Deserialize<bool>("s:0:\"\";", new PhpDeserializationOptions(){EmptyStringToDefault = false})
4246
);
43-
// TODO: Rethrow the exception in the code with a useful error message, then test that here.
47+
48+
Assert.AreEqual(
49+
"Exception encountered while trying to assign '' to type Boolean. See inner exception for details.",
50+
exception.Message
51+
);
4452
}
4553

4654
[TestMethod]
4755
public void Disabled_StringToDouble() {
48-
var ex = Assert.ThrowsException<System.FormatException>(
56+
var exception = Assert.ThrowsException<DeserializationException>(
4957
() => PhpSerialization.Deserialize<double>("s:0:\"\";", new PhpDeserializationOptions(){EmptyStringToDefault = false})
5058
);
51-
// TODO: Rethrow the exception in the code with a useful error message, then test that here.
59+
60+
Assert.AreEqual(
61+
"Exception encountered while trying to assign '' to type Double. See inner exception for details.",
62+
exception.Message
63+
);
5264
}
5365
}
5466
}

PhpSerializerNET.Test/Deserialize/Options/NumberStringToBool.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ public void Enabled_Deserializes() {
2222

2323
[TestMethod]
2424
public void Disabled_Throws() {
25-
var exception = Assert.ThrowsException<System.FormatException>(
25+
var exception = Assert.ThrowsException<DeserializationException>(
2626
() => PhpSerialization.Deserialize<bool>(
2727
"s:1:\"0\";",
2828
new PhpDeserializationOptions() { NumberStringToBool = false }
2929
)
3030
);
3131

32-
// TODO: Rethrow the format exception and assert the proper text.
32+
Assert.AreEqual(
33+
"Exception encountered while trying to assign '0' to type Boolean. See inner exception for details.",
34+
exception.Message
35+
);
3336
}
3437
}
3538
}

PhpSerializerNET/PhpDeserializer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@ private object DeserializeToken(Type targetType, PhpSerializeToken token) {
156156
return token.ToBool();
157157
}
158158
}
159-
return ((IConvertible)token.Value).ToType(targetType, CultureInfo.InvariantCulture);
159+
160+
try{
161+
return ((IConvertible)token.Value).ToType(targetType, CultureInfo.InvariantCulture);
162+
} catch(Exception exception){
163+
throw new DeserializationException(
164+
$"Exception encountered while trying to assign '{token.Value}' to type {targetType.Name}. See inner exception for details.",
165+
exception
166+
);
167+
}
160168
} else if(targetType == typeof(System.Guid)) {
161169
return new Guid(token.Value);
162170
} else {

0 commit comments

Comments
 (0)