Catch exceptions when creating HtmlDocument from stream (#1725)

* Catch exceptions when creating HtmlDocument from stream

* Use proper retry logic if exception occured

* Add null check for document
This commit is contained in:
Vitaliy
2020-04-07 23:12:01 +03:00
committed by GitHub
parent c852f024a2
commit 5e39731a27

View File

@@ -105,9 +105,44 @@ namespace ArchiSteamFarm {
return null;
}
using StreamResponse response = await UrlGetToStream(request, referer, requestOptions, maxTries).ConfigureAwait(false);
HtmlDocumentResponse result = null;
return response != null ? await HtmlDocumentResponse.Create(response).ConfigureAwait(false) : null;
for (int i = 0; i < maxTries; i++) {
using StreamResponse response = await UrlGetToStream(request, referer, requestOptions, maxTries).ConfigureAwait(false);
if (response == null) {
return null;
}
if (response.StatusCode.IsClientErrorCode()) {
if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) {
result = new HtmlDocumentResponse(response);
}
break;
}
if (response.Content == null) {
continue;
}
try {
result = await HtmlDocumentResponse.Create(response).ConfigureAwait(false);
} catch (Exception e) {
ArchiLogger.LogGenericWarningException(e);
continue;
}
return result;
}
if (maxTries > 1) {
ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries));
ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
}
return result;
}
[ItemCanBeNull]
@@ -149,13 +184,9 @@ namespace ArchiSteamFarm {
JsonSerializer serializer = new JsonSerializer();
obj = serializer.Deserialize<T>(jsonReader);
} catch (JsonException e) {
} catch (Exception e) {
ArchiLogger.LogGenericWarningException(e);
if (Debugging.IsUserDebugging) {
ArchiLogger.LogGenericDebug(string.Format(Strings.Content, response.Content));
}
continue;
}
@@ -305,9 +336,44 @@ namespace ArchiSteamFarm {
return null;
}
using StreamResponse response = await UrlPostToStream(request, data, referer, requestOptions, maxTries).ConfigureAwait(false);
HtmlDocumentResponse result = null;
return response != null ? await HtmlDocumentResponse.Create(response).ConfigureAwait(false) : null;
for (int i = 0; i < maxTries; i++) {
using StreamResponse response = await UrlPostToStream(request, data, referer, requestOptions, maxTries).ConfigureAwait(false);
if (response == null) {
return null;
}
if (response.StatusCode.IsClientErrorCode()) {
if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) {
result = new HtmlDocumentResponse(response);
}
break;
}
if (response.Content == null) {
continue;
}
try {
result = await HtmlDocumentResponse.Create(response).ConfigureAwait(false);
} catch (Exception e) {
ArchiLogger.LogGenericWarningException(e);
continue;
}
return result;
}
if (maxTries > 1) {
ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries));
ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
}
return result;
}
[ItemCanBeNull]
@@ -348,13 +414,9 @@ namespace ArchiSteamFarm {
JsonSerializer serializer = new JsonSerializer();
obj = serializer.Deserialize<T>(jsonReader);
} catch (JsonException e) {
} catch (Exception e) {
ArchiLogger.LogGenericWarningException(e);
if (Debugging.IsUserDebugging) {
ArchiLogger.LogGenericDebug(string.Format(Strings.Content, response.Content));
}
continue;
}
@@ -749,18 +811,33 @@ namespace ArchiSteamFarm {
[PublicAPI]
public readonly IDocument Content;
private HtmlDocumentResponse(BasicResponse streamResponse, IDocument document) : base(streamResponse) => Content = document;
internal HtmlDocumentResponse([NotNull] BasicResponse basicResponse) : base(basicResponse) { }
[ItemNotNull]
private HtmlDocumentResponse([NotNull] StreamResponse streamResponse, [NotNull] IDocument document) : base(streamResponse) {
if ((streamResponse == null) || (document == null)) {
throw new ArgumentNullException(nameof(streamResponse) + " || " + nameof(document));
}
Content = document;
}
[ItemCanBeNull]
internal static async Task<HtmlDocumentResponse> Create([NotNull] StreamResponse streamResponse) {
if (streamResponse == null) {
throw new ArgumentNullException(nameof(streamResponse));
}
IBrowsingContext context = BrowsingContext.New(Configuration.Default.WithXPath());
IDocument document = await context.OpenAsync(req => req.Content(streamResponse.Content, true)).ConfigureAwait(false);
return new HtmlDocumentResponse(streamResponse, document);
try {
IDocument document = await context.OpenAsync(req => req.Content(streamResponse.Content, true)).ConfigureAwait(false);
return new HtmlDocumentResponse(streamResponse, document);
} catch (Exception e) {
ASF.ArchiLogger.LogGenericWarningException(e);
return null;
}
}
}