-
Notifications
You must be signed in to change notification settings - Fork 173
feat(csharp/src/Drivers): Format thrift error #3019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Text.RegularExpressions; | ||
| using Thrift.Transport; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.Apache | ||
| { | ||
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The newline here is being added even when |
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: sort