lukevp / ESC-POS-.NET

Efficient, Easy to Use Thermal Printing & POS (Windows/Linux/OSX, WiFi/BT/USB/Ethernet)
MIT License
535 stars 180 forks source link

This causes task cancelled unhandled exception #283

Open ALSULTAN opened 1 month ago

ALSULTAN commented 1 month ago

https://github.com/lukevp/ESC-POS-.NET/blame/ac185fc58bf9e5ad937750b8fc92c02baf344cc9/ESCPOS_NET/Printers/ImmediateNetworkPrinter.cs#L49

ALSULTAN commented 1 month ago
System.AggregateException: TaskExceptionHolder_UnhandledException (Operation canceled)
 ---> System.Net.Sockets.SocketException (89): Operation canceled
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 )
   at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
--- End of stack trace from previous location ---
   at System.Net.Sockets.TcpClient.CompleteConnectAsync(Task )
   Exception_EndOfInnerExceptionStack
igorocampos commented 1 month ago

Not sure what you meant, but the code you linked is inside a try catch

catch (Exception e)
{
    Logging.Logger?.LogError(e, "[{Function}]:[{PrinterName}] Unable to connect to printer.", $"{this}.{MethodBase.GetCurrentMethod().Name}", _settings.PrinterName);
    client.Close();
    throw;
}

You can check the logs for the error details, but sounds like you are unable to connect to your printer...

ALSULTAN commented 1 month ago

Actually, there is a throw on catch statement and the method RunTask is throwing another ThrowIfCancellationRequested hope this helps. anyone who knows the code well can see the exception is propagating to the caller application

igorocampos commented 1 month ago

Yep, the throw is on purpose, so the exception is not swallowed by the library, and the lib user can act upon it, and actually know there was a connection error, otherwise it would just silently fail for you when such a lack of connection happens.

If you have a better proposal of how to handle that, you are welcome to open a PR as this is an open source project :)