Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions csharp/src/Drivers/Apache/ApacheUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Text.RegularExpressions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: sort

using Thrift.Transport;

namespace Apache.Arrow.Adbc.Drivers.Apache
{
Expand Down Expand Up @@ -157,12 +159,45 @@ internal static string FormatExceptionMessage(Exception exception)
if (exception is AggregateException aEx)
{
AggregateException flattenedEx = aEx.Flatten();
IEnumerable<string> messages = flattenedEx.InnerExceptions.Select((ex, index) => $"({index + 1}) {ex.Message}");
string fullMessage = $"{flattenedEx.Message}: {string.Join(", ", messages)}";
IEnumerable<string> messages = flattenedEx.InnerExceptions
.Select((ex, index) => $"({index + 1}) {FormatTransportExceptionMessage(ex)}");
string fullMessage = $"{FormatTransportExceptionMessage(flattenedEx)}: {string.Join(", ", messages)}";
return fullMessage;
}

return exception.Message;
return FormatTransportExceptionMessage(exception);
}

/// <summary>
/// Formats a transport exception message with custom handling for HTTP status codes (401, 403, 404).
/// </summary>
internal static string FormatTransportExceptionMessage(Exception exception)
{
var customMsg = "";
// Use ContainsException to robustly find TTransportException or HttpRequestException
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't line up well with what the code is doing.

if (exception is TTransportException transportEx)
{
// Try to extract status code from the message
var match = System.Text.RegularExpressions.Regex.Match(transportEx.Message, @"\b(\d{3})\b");
if (match.Success && int.TryParse(match.Value, out int code))
{
switch (code)
{
case 401:
customMsg = "Unauthorized (401): Please check your credentials.";
break;
case 403:
customMsg = "Forbidden (403): You do not have permission to access this resource.";
break;
case 404:
customMsg = "Not Found (404): The requested resource was not found on the server.";
break;
default:
break;
}
}
}
return $"{customMsg}{Environment.NewLine}{exception.Message}";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The newline here is being added even when customMsg is empty. Does that produce the desired experience?

}
}
}
Loading