mirror of
https://github.com/JustArchiNET/ArchiSteamFarm.git
synced 2026-01-01 14:10:53 +00:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user