Added UploaderErrorManager

This commit is contained in:
Jaex 2022-10-05 10:22:02 +03:00
parent 5f1a4efa8e
commit 810203a891
11 changed files with 156 additions and 25 deletions

View file

@ -41,7 +41,7 @@ public class Uploader
public event Action<string> EarlyURLCopyRequested;
public bool IsUploading { get; protected set; }
public List<string> Errors { get; private set; } = new List<string>();
public UploaderErrorManager Errors { get; private set; } = new UploaderErrorManager();
public bool IsError => !StopUploadRequested && Errors != null && Errors.Count > 0;
public int BufferSize { get; set; } = 8192;
@ -483,7 +483,7 @@ private string ProcessError(Exception e, string requestURL)
string errorText = sb.ToString();
if (Errors == null) Errors = new List<string>();
if (Errors == null) Errors = new UploaderErrorManager();
Errors.Add(errorText);
DebugHelper.WriteLine("Error:\r\n" + errorText);

View file

@ -303,7 +303,7 @@ public string CreateShareableLink(string path, bool directLink)
{
linkMetadata = JsonConvert.DeserializeObject<DropboxLinkMetadata>(response);
}
else if (IsError && Errors[Errors.Count - 1].Contains("\"shared_link_already_exists\"")) // Ugly workaround
else if (IsError && Errors.Errors[Errors.Count - 1].Text.Contains("\"shared_link_already_exists\"")) // Ugly workaround
{
DropboxListSharedLinksResult result = ListSharedLinks(path, true);

View file

@ -269,7 +269,7 @@ private string GetSummary(string issueId)
}
// This query can returns error so we have to remove last error from errors list
Errors.RemoveAt(Errors.Count - 1);
Errors.Errors.RemoveAt(Errors.Count - 1);
return null;
}

View file

@ -25,7 +25,6 @@
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
namespace ShareX.UploadersLib.FileUploaders
{
@ -39,7 +38,7 @@ public static class SendSpaceManager
public static string Password;
public static SendSpace.UploadInfo UploadInfo;
public static List<string> PrepareUploadInfo(string apiKey, string username = null, string password = null)
public static UploaderErrorManager PrepareUploadInfo(string apiKey, string username = null, string password = null)
{
SendSpace sendSpace = new SendSpace(apiKey);

View file

@ -623,7 +623,7 @@ private async Task TestCustomUploader(CustomUploaderDestinationType type, int in
{
CustomImageUploader imageUploader = new CustomImageUploader(item);
result = imageUploader.Upload(stream, "Test.png");
result.Errors.AddRange(imageUploader.Errors);
result.Errors.Add(imageUploader.Errors);
}
break;
case CustomUploaderDestinationType.TextUploader:
@ -637,7 +637,7 @@ private async Task TestCustomUploader(CustomUploaderDestinationType type, int in
if (!string.IsNullOrEmpty(text))
{
result = textUploader.UploadText(text, "Test.txt");
result.Errors.AddRange(textUploader.Errors);
result.Errors.Add(textUploader.Errors);
}
}
}
@ -647,18 +647,18 @@ private async Task TestCustomUploader(CustomUploaderDestinationType type, int in
{
CustomFileUploader fileUploader = new CustomFileUploader(item);
result = fileUploader.Upload(stream, "Test.png");
result.Errors.AddRange(fileUploader.Errors);
result.Errors.Add(fileUploader.Errors);
}
break;
case CustomUploaderDestinationType.URLShortener:
CustomURLShortener urlShortener = new CustomURLShortener(item);
result = urlShortener.ShortenURL(Links.Website);
result.Errors.AddRange(urlShortener.Errors);
result.Errors.Add(urlShortener.Errors);
break;
case CustomUploaderDestinationType.URLSharingService:
CustomURLSharer urlSharer = new CustomURLSharer(item);
result = urlSharer.ShareURL(Links.Website);
result.Errors.AddRange(urlSharer.Errors);
result.Errors.Add(urlSharer.Errors);
break;
}
}

View file

@ -0,0 +1,42 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
namespace ShareX.UploadersLib
{
public class UploaderErrorInfo
{
public string Title { get; set; }
public string Text { get; set; }
public Exception Exception { get; set; }
public UploaderErrorInfo(string title, string text)
{
Title = title;
Text = text;
}
}
}

View file

@ -0,0 +1,74 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.UploadersLib.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ShareX.UploadersLib
{
public class UploaderErrorManager
{
public List<UploaderErrorInfo> Errors { get; private set; }
public int Count => Errors.Count;
public UploaderErrorManager()
{
Errors = new List<UploaderErrorInfo>();
}
public void Add(string text)
{
Add(Resources.Error, text);
}
public void Add(string title, string text)
{
Errors.Add(new UploaderErrorInfo(title, text));
}
public void Add(UploaderErrorManager manager)
{
Errors.AddRange(manager.Errors);
}
public void Insert(int index, string text)
{
Insert(index, Resources.Error, text);
}
public void Insert(int index, string title, string text)
{
Errors.Insert(index, new UploaderErrorInfo(title, text));
}
public override string ToString()
{
return string.Join(Environment.NewLine + Environment.NewLine, Errors.Select(x => x.Text));
}
}
}

View file

@ -254,6 +254,7 @@
<Compile Include="Helpers\MimeTypes.cs" />
<Compile Include="Helpers\ResponseInfo.cs" />
<Compile Include="Helpers\RequestHelpers.cs" />
<Compile Include="Helpers\UploaderErrorManager.cs" />
<Compile Include="OAuth\GoogleOAuth2.cs" />
<Compile Include="OAuth\IOAuthBase.cs" />
<Compile Include="OAuth\OAuth2ProofKey.cs" />
@ -284,6 +285,7 @@
<Compile Include="BaseServices\TextUploaderService.cs" />
<Compile Include="TextUploaders\Pastie.cs" />
<Compile Include="TextUploaders\TeknikPastebin.cs" />
<Compile Include="Helpers\UploaderErrorInfo.cs" />
<Compile Include="UploaderFactory.cs" />
<Compile Include="ImageUploaders\Chevereto.cs" />
<Compile Include="ImageUploaders\CheveretoUploader.cs" />

View file

@ -24,8 +24,6 @@
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.Text;
namespace ShareX.UploadersLib
@ -52,7 +50,7 @@ public bool IsSuccess
}
public string Response { get; set; }
public List<string> Errors { get; set; }
public UploaderErrorManager Errors { get; set; }
public bool IsURLExpected { get; set; }
public bool IsError
@ -67,7 +65,7 @@ public bool IsError
public UploadResult()
{
Errors = new List<string>();
Errors = new UploaderErrorManager();
IsURLExpected = true;
}
@ -104,7 +102,7 @@ public string ErrorsToString()
{
if (IsError)
{
return string.Join(Environment.NewLine + Environment.NewLine, Errors.ToArray());
return Errors.ToString();
}
return null;

View file

@ -300,7 +300,7 @@ private static void Task_TaskCompleted(WorkerTask task)
}
else if (task.Status == TaskStatus.Failed)
{
string errors = string.Join("\r\n\r\n", info.Result.Errors.ToArray());
string errors = info.Result.Errors.ToString();
DebugHelper.WriteLine($"Task failed. File name: {info.FileName}, Errors:\r\n{errors}");
@ -320,12 +320,18 @@ private static void Task_TaskCompleted(WorkerTask task)
if (info.Result.Errors.Count > 0)
{
string errorMessage = info.Result.Errors[0];
UploaderErrorInfo error = info.Result.Errors.Errors[0];
if (info.TaskSettings.GeneralSettings.ShowToastNotificationAfterTaskCompleted && !string.IsNullOrEmpty(errorMessage) &&
string title = error.Title;
if (string.IsNullOrEmpty(title))
{
title = Resources.TaskManager_task_UploadCompleted_Error;
}
if (info.TaskSettings.GeneralSettings.ShowToastNotificationAfterTaskCompleted && !string.IsNullOrEmpty(error.Text) &&
(!info.TaskSettings.GeneralSettings.DisableNotificationsOnFullscreen || !CaptureHelpers.IsActiveWindowFullscreen()))
{
TaskHelpers.ShowNotificationTip(errorMessage, "ShareX - " + Resources.TaskManager_task_UploadCompleted_Error, 5000);
TaskHelpers.ShowNotificationTip(error.Text, "ShareX - " + title, 5000);
}
}
}

View file

@ -526,7 +526,7 @@ private bool DoUpload(Stream data, string fileName, int retry = 0)
if (uploader != null)
{
AddErrorMessage(uploader.Errors.ToArray());
AddErrorMessage(uploader.Errors);
}
isError |= Info.Result.IsError;
@ -535,14 +535,24 @@ private bool DoUpload(Stream data, string fileName, int retry = 0)
return isError;
}
private void AddErrorMessage(params string[] errorMessages)
private void AddErrorMessage(UploaderErrorManager errors)
{
if (Info.Result == null)
{
Info.Result = new UploadResult();
}
Info.Result.Errors.AddRange(errorMessages);
Info.Result.Errors.Add(errors);
}
private void AddErrorMessage(string error)
{
if (Info.Result == null)
{
Info.Result = new UploadResult();
}
Info.Result.Errors.Add(error);
}
private bool DoThreadJob()
@ -840,7 +850,7 @@ private void DoAfterUploadJobs()
if (result != null)
{
Info.Result.ShortenedURL = result.ShortenedURL;
Info.Result.Errors.AddRange(result.Errors);
Info.Result.Errors.Add(result.Errors);
}
}
@ -850,7 +860,7 @@ private void DoAfterUploadJobs()
if (result != null)
{
Info.Result.Errors.AddRange(result.Errors);
Info.Result.Errors.Add(result.Errors);
}
if (Info.Job == TaskJob.ShareURL)