-
Notifications
You must be signed in to change notification settings - Fork 372
fix: improve error messages for missing database tables #2775
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
Open
ivanauth
wants to merge
7
commits into
authzed:main
Choose a base branch
from
ivanauth:fix/issue-2749-complete-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,033
−8
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5c953de
fix: improve error messages for missing database tables
ivanauth 54d14ab
fix: address PR #2775 review feedback
ivanauth 39804a8
chore: update TODO to reference authzed/api#159
ivanauth b019d31
feat: centralize migration error handling with gRPC readiness middleware
ivanauth 24d2ead
fix: correct import order in defaults.go
ivanauth 2eefa26
fix: remove private fields from generated ToOption method
ivanauth f918ab5
retrigger CI
ivanauth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc/codes" | ||
| ) | ||
|
|
||
| func TestSchemaNotInitializedError(t *testing.T) { | ||
| underlyingErr := fmt.Errorf("relation \"caveat\" does not exist (SQLSTATE 42P01)") | ||
| err := NewSchemaNotInitializedError(underlyingErr) | ||
|
|
||
| t.Run("error message contains migration instructions", func(t *testing.T) { | ||
| require.Contains(t, err.Error(), "spicedb datastore migrate") | ||
| // The error message now includes the underlying error first, followed by the instruction | ||
| require.Contains(t, err.Error(), "relation \"caveat\" does not exist") | ||
| }) | ||
|
|
||
| t.Run("unwrap returns underlying error", func(t *testing.T) { | ||
| var schemaErr SchemaNotInitializedError | ||
| require.ErrorAs(t, err, &schemaErr) | ||
| require.ErrorIs(t, schemaErr.Unwrap(), underlyingErr) | ||
| }) | ||
|
|
||
| t.Run("grpc status is FailedPrecondition", func(t *testing.T) { | ||
| var schemaErr SchemaNotInitializedError | ||
| require.ErrorAs(t, err, &schemaErr) | ||
| status := schemaErr.GRPCStatus() | ||
| require.Equal(t, codes.FailedPrecondition, status.Code()) | ||
| }) | ||
|
|
||
| t.Run("can be detected with errors.As", func(t *testing.T) { | ||
| var schemaErr SchemaNotInitializedError | ||
| require.ErrorAs(t, err, &schemaErr) | ||
| }) | ||
|
|
||
| t.Run("wrapped error preserves chain", func(t *testing.T) { | ||
| wrappedErr := fmt.Errorf("outer: %w", err) | ||
| var schemaErr SchemaNotInitializedError | ||
| require.ErrorAs(t, wrappedErr, &schemaErr) | ||
| }) | ||
|
|
||
| t.Run("grpc status extractable from wrapped error", func(t *testing.T) { | ||
| // This tests the scenario where SchemaNotInitializedError is wrapped | ||
| // by another fmt.Errorf (e.g., in crdb/caveat.go). The gRPC library | ||
| // uses errors.As to extract GRPCStatus from wrapped errors. | ||
| wrappedErr := fmt.Errorf("outer context: %w", err) | ||
| var schemaErr SchemaNotInitializedError | ||
| require.ErrorAs(t, wrappedErr, &schemaErr) | ||
| status := schemaErr.GRPCStatus() | ||
| require.Equal(t, codes.FailedPrecondition, status.Code()) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/go-sql-driver/mysql" | ||
|
|
||
| dscommon "github.com/authzed/spicedb/internal/datastore/common" | ||
| ) | ||
|
|
||
| const ( | ||
| // mysqlMissingTableErrorNumber is the MySQL error number for "table doesn't exist". | ||
| // This corresponds to MySQL error 1146 (ER_NO_SUCH_TABLE) with SQLSTATE 42S02. | ||
| mysqlMissingTableErrorNumber = 1146 | ||
| ) | ||
|
|
||
| // IsMissingTableError returns true if the error is a MySQL error indicating a missing table. | ||
| // This typically happens when migrations have not been run. | ||
| func IsMissingTableError(err error) bool { | ||
| var mysqlErr *mysql.MySQLError | ||
| return errors.As(err, &mysqlErr) && mysqlErr.Number == mysqlMissingTableErrorNumber | ||
| } | ||
|
|
||
| // WrapMissingTableError checks if the error is a missing table error and wraps it with | ||
| // a helpful message instructing the user to run migrations. If it's not a missing table error, | ||
| // it returns nil. If it's already a SchemaNotInitializedError, it returns the original error | ||
| // to preserve the wrapped error through the call chain. | ||
| func WrapMissingTableError(err error) error { | ||
| // Don't double-wrap if already a SchemaNotInitializedError - return original to preserve it | ||
| var schemaErr dscommon.SchemaNotInitializedError | ||
| if errors.As(err, &schemaErr) { | ||
| return err | ||
| } | ||
| if IsMissingTableError(err) { | ||
| return dscommon.NewSchemaNotInitializedError(err) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/go-sql-driver/mysql" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| dscommon "github.com/authzed/spicedb/internal/datastore/common" | ||
| ) | ||
|
|
||
| func TestIsMissingTableError(t *testing.T) { | ||
| t.Run("returns true for missing table error", func(t *testing.T) { | ||
| mysqlErr := &mysql.MySQLError{ | ||
| Number: mysqlMissingTableErrorNumber, | ||
| Message: "Table 'spicedb.caveat' doesn't exist", | ||
| } | ||
| require.True(t, IsMissingTableError(mysqlErr)) | ||
| }) | ||
|
|
||
| t.Run("returns false for other mysql errors", func(t *testing.T) { | ||
| mysqlErr := &mysql.MySQLError{ | ||
| Number: 1062, // Duplicate entry error | ||
| Message: "Duplicate entry '1' for key 'PRIMARY'", | ||
| } | ||
| require.False(t, IsMissingTableError(mysqlErr)) | ||
| }) | ||
|
|
||
| t.Run("returns false for non-mysql errors", func(t *testing.T) { | ||
| err := fmt.Errorf("some other error") | ||
| require.False(t, IsMissingTableError(err)) | ||
| }) | ||
|
|
||
| t.Run("returns false for nil error", func(t *testing.T) { | ||
| require.False(t, IsMissingTableError(nil)) | ||
| }) | ||
|
|
||
| t.Run("returns true for wrapped missing table error", func(t *testing.T) { | ||
| mysqlErr := &mysql.MySQLError{ | ||
| Number: mysqlMissingTableErrorNumber, | ||
| Message: "Table 'spicedb.caveat' doesn't exist", | ||
| } | ||
| wrappedErr := fmt.Errorf("query failed: %w", mysqlErr) | ||
| require.True(t, IsMissingTableError(wrappedErr)) | ||
| }) | ||
| } | ||
|
|
||
| func TestWrapMissingTableError(t *testing.T) { | ||
| t.Run("wraps missing table error", func(t *testing.T) { | ||
| mysqlErr := &mysql.MySQLError{ | ||
| Number: mysqlMissingTableErrorNumber, | ||
| Message: "Table 'spicedb.caveat' doesn't exist", | ||
| } | ||
| wrapped := WrapMissingTableError(mysqlErr) | ||
| require.Error(t, wrapped) | ||
|
|
||
| var schemaErr dscommon.SchemaNotInitializedError | ||
| require.ErrorAs(t, wrapped, &schemaErr) | ||
| require.Contains(t, wrapped.Error(), "spicedb datastore migrate") | ||
| }) | ||
|
|
||
| t.Run("returns nil for non-missing-table errors", func(t *testing.T) { | ||
| mysqlErr := &mysql.MySQLError{ | ||
| Number: 1062, // Duplicate entry error | ||
| Message: "Duplicate entry '1' for key 'PRIMARY'", | ||
| } | ||
| require.NoError(t, WrapMissingTableError(mysqlErr)) | ||
| }) | ||
|
|
||
| t.Run("returns nil for non-mysql errors", func(t *testing.T) { | ||
| err := fmt.Errorf("some other error") | ||
| require.NoError(t, WrapMissingTableError(err)) | ||
| }) | ||
|
|
||
| t.Run("returns nil for nil error", func(t *testing.T) { | ||
| require.NoError(t, WrapMissingTableError(nil)) | ||
| }) | ||
|
|
||
| t.Run("preserves original error in chain", func(t *testing.T) { | ||
| mysqlErr := &mysql.MySQLError{ | ||
| Number: mysqlMissingTableErrorNumber, | ||
| Message: "Table 'spicedb.caveat' doesn't exist", | ||
| } | ||
| wrapped := WrapMissingTableError(mysqlErr) | ||
| require.Error(t, wrapped) | ||
|
|
||
| // The original mysql error should be accessible via unwrapping | ||
| var foundMySQLErr *mysql.MySQLError | ||
| require.ErrorAs(t, wrapped, &foundMySQLErr) | ||
| require.Equal(t, uint16(mysqlMissingTableErrorNumber), foundMySQLErr.Number) | ||
| }) | ||
|
|
||
| t.Run("does not double-wrap already wrapped errors", func(t *testing.T) { | ||
| mysqlErr := &mysql.MySQLError{ | ||
| Number: mysqlMissingTableErrorNumber, | ||
| Message: "Table 'spicedb.caveat' doesn't exist", | ||
| } | ||
| // First wrap | ||
| wrapped := WrapMissingTableError(mysqlErr) | ||
| require.Error(t, wrapped) | ||
|
|
||
| // Second wrap should return the already-wrapped error (preserving it through call chain) | ||
| doubleWrapped := WrapMissingTableError(wrapped) | ||
| require.Error(t, doubleWrapped) | ||
| require.Equal(t, wrapped, doubleWrapped) | ||
|
|
||
| // Should still be detectable as SchemaNotInitializedError | ||
| var schemaErr dscommon.SchemaNotInitializedError | ||
| require.ErrorAs(t, doubleWrapped, &schemaErr) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: please create the right error code