Skip to content

Commit 5e89df4

Browse files
committed
Merge pull request restsharp#490 from Naliath/master
Prevent object is null or undefined exception when deserializing a response
2 parents e18460a + ef43aae commit 5e89df4

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

RestSharp/RestClient.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -503,15 +503,19 @@ private IRestResponse<T> Deserialize<T>(IRestRequest request, IRestResponse raw)
503503
// Only attempt to deserialize if the request has not errored due
504504
// to a transport or framework exception. HTTP errors should attempt to
505505
// be deserialized
506-
507506
if (response.ErrorException==null)
508507
{
509508
IDeserializer handler = GetHandler(raw.ContentType);
510-
handler.RootElement = request.RootElement;
511-
handler.DateFormat = request.DateFormat;
512-
handler.Namespace = request.XmlNamespace;
513-
514-
response.Data = handler.Deserialize<T>(raw);
509+
// Only continue if there is a handler defined else there is no way to deserialize the data.
510+
// This can happen when a request returns for example a 404 page instead of the requested JSON/XML resource
511+
if (handler != null)
512+
{
513+
handler.RootElement = request.RootElement;
514+
handler.DateFormat = request.DateFormat;
515+
handler.Namespace = request.XmlNamespace;
516+
517+
response.Data = handler.Deserialize<T>(raw);
518+
}
515519
}
516520
}
517521
catch (Exception ex)

0 commit comments

Comments
 (0)