Skip to content

Commit

Permalink
Add a better error message for worker crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Aug 3, 2024
1 parent c275304 commit 4bc82ef
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 14 deletions.
18 changes: 18 additions & 0 deletions NAPS2.Sdk/Lang/Resources/SdkResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions NAPS2.Sdk/Lang/Resources/SdkResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,10 @@
<data name="OcrTimeout" xml:space="preserve">
<value>The OCR operation timed out.</value>
</data>
<data name="WorkerCrash" xml:space="preserve">
<value>The worker process crashed.</value>
</data>
<data name="WorkerCrashWindows" xml:space="preserve">
<value>The worker process crashed. Check the Windows event viewer.</value>
</data>
</root>
2 changes: 2 additions & 0 deletions NAPS2.Sdk/Platform/ISystemCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal interface ISystemCompat

bool SupportsWinX86Worker { get; }

string WorkerCrashMessage { get; }

string[] ExeSearchPaths { get; }

string[] LibrarySearchPaths { get; }
Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Sdk/Platform/LinuxSystemCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ internal class LinuxSystemCompat : ISystemCompat

public bool SupportsWinX86Worker => false;

public string WorkerCrashMessage => SdkResources.WorkerCrash;

public string[] ExeSearchPaths => LibrarySearchPaths;

public string[] LibrarySearchPaths => new[]
Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Sdk/Platform/MacSystemCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ internal class MacSystemCompat : ISystemCompat

public bool SupportsWinX86Worker => false;

public string WorkerCrashMessage => SdkResources.WorkerCrash;

public string[] ExeSearchPaths => LibrarySearchPaths;

public string[] LibrarySearchPaths
Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Sdk/Platform/WindowsSystemCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ internal abstract class WindowsSystemCompat : ISystemCompat

public bool SupportsWinX86Worker => true;

public string WorkerCrashMessage => SdkResources.WorkerCrashWindows;

public abstract string[] ExeSearchPaths { get; }

public abstract string[] LibrarySearchPaths { get; }
Expand Down
74 changes: 60 additions & 14 deletions NAPS2.Sdk/Remoting/Worker/WorkerServiceAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NAPS2.ImportExport.Email;
using NAPS2.ImportExport.Email.Mapi;
using NAPS2.Scan;
using NAPS2.Scan.Exceptions;
using NAPS2.Scan.Internal;
using NAPS2.Scan.Internal.Twain;
using NAPS2.Scan.Internal.Wia;
Expand Down Expand Up @@ -60,6 +61,10 @@ public async Task GetDevices(ScanOptions options, CancellationToken cancelToken,
}
catch (RpcException ex)
{
if (ex.StatusCode == StatusCode.Unavailable)
{
throw new ScanDriverUnknownException(PlatformCompat.System.WorkerCrashMessage, ex);
}
if (ex.Status.StatusCode != StatusCode.Cancelled)
{
throw;
Expand All @@ -69,10 +74,21 @@ public async Task GetDevices(ScanOptions options, CancellationToken cancelToken,

public async Task<ScanCaps> GetCaps(ScanOptions options, CancellationToken cancelToken)
{
var req = new GetCapsRequest { OptionsXml = options.ToXml() };
var resp = await _client.GetCapsAsync(req, cancellationToken: cancelToken);
RemotingHelper.HandleErrors(resp.Error);
return resp.ScanCapsXml.FromXml<ScanCaps>();
try
{
var req = new GetCapsRequest { OptionsXml = options.ToXml() };
var resp = await _client.GetCapsAsync(req, cancellationToken: cancelToken);
RemotingHelper.HandleErrors(resp.Error);
return resp.ScanCapsXml.FromXml<ScanCaps>();
}
catch (RpcException ex)
{
if (ex.StatusCode == StatusCode.Unavailable)
{
throw new ScanDriverUnknownException(PlatformCompat.System.WorkerCrashMessage, ex);
}
throw;
}
}

public async Task Scan(ScanningContext scanningContext, ScanOptions options, CancellationToken cancelToken,
Expand Down Expand Up @@ -107,7 +123,11 @@ public async Task Scan(ScanningContext scanningContext, ScanOptions options, Can
}
catch (RpcException ex)
{
if (ex.Status.StatusCode != StatusCode.Cancelled)
if (ex.StatusCode == StatusCode.Unavailable)
{
throw new ScanDriverUnknownException(PlatformCompat.System.WorkerCrashMessage, ex);
}
if (ex.StatusCode != StatusCode.Cancelled)
{
throw;
}
Expand Down Expand Up @@ -194,7 +214,11 @@ public async Task TwainScan(ScanOptions options, CancellationToken cancelToken,
}
catch (RpcException ex)
{
if (ex.Status.StatusCode != StatusCode.Cancelled)
if (ex.StatusCode == StatusCode.Unavailable)
{
throw new ScanDriverUnknownException(PlatformCompat.System.WorkerCrashMessage, ex);
}
if (ex.StatusCode != StatusCode.Cancelled)
{
throw;
}
Expand All @@ -203,18 +227,40 @@ public async Task TwainScan(ScanOptions options, CancellationToken cancelToken,

public async Task<List<ScanDevice>> TwainGetDeviceList(ScanOptions options)
{
var req = new GetDeviceListRequest { OptionsXml = options.ToXml() };
var resp = await _client.TwainGetDeviceListAsync(req);
RemotingHelper.HandleErrors(resp.Error);
return resp.DeviceListXml.FromXml<List<ScanDevice>>();
try
{
var req = new GetDeviceListRequest { OptionsXml = options.ToXml() };
var resp = await _client.TwainGetDeviceListAsync(req);
RemotingHelper.HandleErrors(resp.Error);
return resp.DeviceListXml.FromXml<List<ScanDevice>>();
}
catch (RpcException ex)
{
if (ex.StatusCode == StatusCode.Unavailable)
{
throw new ScanDriverUnknownException(PlatformCompat.System.WorkerCrashMessage, ex);
}
throw;
}
}

public async Task<ScanCaps> TwainGetCaps(ScanOptions options)
{
var req = new GetCapsRequest { OptionsXml = options.ToXml() };
var resp = await _client.TwainGetCapsAsync(req);
RemotingHelper.HandleErrors(resp.Error);
return resp.ScanCapsXml.FromXml<ScanCaps>();
try
{
var req = new GetCapsRequest { OptionsXml = options.ToXml() };
var resp = await _client.TwainGetCapsAsync(req);
RemotingHelper.HandleErrors(resp.Error);
return resp.ScanCapsXml.FromXml<ScanCaps>();
}
catch (RpcException ex)
{
if (ex.StatusCode == StatusCode.Unavailable)
{
throw new ScanDriverUnknownException(PlatformCompat.System.WorkerCrashMessage, ex);
}
throw;
}
}

public ProcessedImage ImportPostProcess(ScanningContext scanningContext, ProcessedImage img, int? thumbnailSize,
Expand Down

0 comments on commit 4bc82ef

Please sign in to comment.