Skip to content

feat: add custom error type for unsupported column types #1326

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

Merged
merged 1 commit into from
May 27, 2025

Conversation

dgiagio
Copy link
Contributor

@dgiagio dgiagio commented May 26, 2025

What this PR does / why we need it:
This PR introduces a custom error type in the package sqlutil to improve error handling when encountering unsupported SQL column types. This change provides better diagnostics and enables clearer error handling for consumers of the package.

Previously, when an SQL column had a type that couldn't be processed, the code would return a generic error message via fmt.Errorf. This made it difficult for consumers to programmatically handle this specific error case.

By introducing a structured error type, consumers can now:

  • Use type assertions or the errors.As function to detect this specific error condition
  • Extract the exact type and column information directly from the error

Which issue(s) this PR fixes:
https://github.com/grafana/plugins-private/issues/3032

@CLAassistant
Copy link

CLAassistant commented May 26, 2025

CLA assistant check
All committers have signed the CLA.

@dgiagio dgiagio force-pushed the dgiagio/custom-err-column-type-not-supported branch from 78f4da0 to 272cb56 Compare May 26, 2025 19:06
@dgiagio dgiagio marked this pull request as ready for review May 26, 2025 19:09
@dgiagio dgiagio requested a review from a team as a code owner May 26, 2025 19:09
@dgiagio dgiagio requested review from a team, wbrowne, briangann and s4kh and removed request for a team May 26, 2025 19:11
Copy link
Contributor

@njvrzm njvrzm left a comment

Choose a reason for hiding this comment

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

Lookgs good to me, and I'll approve as is since it's a reasonable change, but I have a question about an alternate approach.

@@ -87,7 +99,7 @@ func MakeScanRow(colTypes []*sql.ColumnType, colNames []string, converters ...Co
if !rc.hasConverter(i) {
scanTypeValue := colType.ScanType()
if scanTypeValue == nil {
return nil, fmt.Errorf(`type %s is not supported for column %s`, colType.DatabaseTypeName(), colName)
return nil, ErrColumnTypeNotSupported{Type: colType.DatabaseTypeName(), Column: colName}
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed that some database drivers - myqsl and pq, for example - use some variant of reflect.TypeOf(new(any)) for unknown column types, where others use nil like this. Currently we handle the "any" type ones in NewDefaultConverter by converting them to nullable strings (or nil if the conversion fails).

Wouldn't it make sense here to handle both these cases the same way? Whether unknown types are represented by nil or "any" seems to be a quirk of the driver.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Nathan,

Thanks for the comment. If I'm understanding correctly, your suggestion is to expand the scanTypeValue check to handle both nil and any. This would account for the different ways database drivers signal an inability to parse a column, returning either nil or an any type.

I agree that this is a a sensible approach for broader compatibility. However, the primary goal of this PR is to improve error handling specifically for the nil case, which addresses an immediate issue within the current implementation.

While a broader fix is a good idea, adding the any check might have unforeseen consequences that I haven't fully explored. I think it's best to keep this PR focused and consider the any handling in a future update.

Thanks for bringing this up!

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I guess I didn't actually make a suggestion, but what I meant to suggest was to go the other way: instead of considering a nil scanType an error, handle it the same way we handle any, and process the values into strings if we can.

Copy link
Contributor

Choose a reason for hiding this comment

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

But! This is a larger change, so please do feel free to merge as is and we can come back to this later.

@dgiagio dgiagio merged commit 20c9ac2 into main May 27, 2025
7 checks passed
@dgiagio dgiagio deleted the dgiagio/custom-err-column-type-not-supported branch May 27, 2025 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants