Skip to content

Commit ef43aae

Browse files
committed
Prevent object is null or undefined exception when the request returns an unexpected type before deserializing (type for which no handler is defined)
1 parent e18460a commit ef43aae

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

RestSharp/RestClient.cs

Lines changed: 10 additions & 6 deletions
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)