-
Notifications
You must be signed in to change notification settings - Fork 153
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
Display MySQL user defined error in API Key UI #4590
Merged
kentakozuka
merged 4 commits into
pipe-cd:master
from
kentakozuka:display-user-defined-error-mysql
Sep 7, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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,92 @@ | ||
// Copyright 2023 The PipeCD Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package grpcapi | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/pipe-cd/pipecd/pkg/datastore" | ||
"github.com/pipe-cd/pipecd/pkg/filestore" | ||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func TestGRPCStoreError(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
inputErr error | ||
inputMsg string | ||
expected error | ||
}{ | ||
{ | ||
name: "datastore not found error", | ||
inputErr: datastore.ErrNotFound, | ||
inputMsg: "datastore", | ||
expected: status.Error(codes.NotFound, "Entity was not found to datastore"), | ||
}, | ||
{ | ||
name: "filestore not found error", | ||
inputErr: filestore.ErrNotFound, | ||
inputMsg: "filestore", | ||
expected: status.Error(codes.NotFound, "Entity was not found to filestore"), | ||
}, | ||
{ | ||
name: "stagelogstore not found error", | ||
inputErr: filestore.ErrNotFound, | ||
inputMsg: "stagelogstore", | ||
expected: status.Error(codes.NotFound, "Entity was not found to stagelogstore"), | ||
}, | ||
{ | ||
name: "datastore invalid argument error", | ||
inputErr: datastore.ErrInvalidArgument, | ||
inputMsg: "datastore", | ||
expected: status.Error(codes.InvalidArgument, "Invalid argument to datastore"), | ||
}, | ||
{ | ||
name: "datastore already exists error", | ||
inputErr: datastore.ErrAlreadyExists, | ||
inputMsg: "datastore", | ||
expected: status.Error(codes.AlreadyExists, "Entity already exists to datastore"), | ||
}, | ||
{ | ||
name: "user defined error", | ||
inputErr: datastore.ErrUserDefined, | ||
expected: status.Error(codes.FailedPrecondition, "user defined error"), | ||
}, | ||
{ | ||
name: "user defined error with message", | ||
inputErr: fmt.Errorf("%w: %s", datastore.ErrUserDefined, "test"), | ||
expected: status.Error(codes.FailedPrecondition, "user defined error: test"), | ||
}, | ||
{ | ||
name: "internal error", | ||
inputErr: errors.New("internal error"), | ||
inputMsg: "test", | ||
expected: status.Error(codes.Internal, "Failed to test"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
err := gRPCStoreError(tt.inputErr, tt.inputMsg) | ||
assert.Equal(t, tt.expected, err) | ||
}) | ||
} | ||
} |
This file contains 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 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 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 |
---|---|---|
|
@@ -26,7 +26,7 @@ import { | |
DISABLE_API_KEY_SUCCESS, | ||
GENERATE_API_KEY_SUCCESS, | ||
} from "~/constants/toast-text"; | ||
import { useAppDispatch, useAppSelector } from "~/hooks/redux"; | ||
import { unwrapResult, useAppDispatch, useAppSelector } from "~/hooks/redux"; | ||
import { | ||
APIKey, | ||
disableAPIKey, | ||
|
@@ -96,10 +96,16 @@ export const APIKeyPage: FC = memo(function APIKeyPage() { | |
|
||
const handleSubmit = useCallback( | ||
(values: { name: string; role: APIKey.Role }) => { | ||
dispatch(generateAPIKey(values)).then(() => { | ||
dispatch(fetchAPIKeys({ enabled: true })); | ||
dispatch(addToast({ message: GENERATE_API_KEY_SUCCESS })); | ||
}); | ||
dispatch(generateAPIKey(values)) | ||
.then(unwrapResult) | ||
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. Fixes an existing bug that success toast is displayed even if a request fails. |
||
.then(() => { | ||
console.log("handleSubmit.then"); | ||
dispatch(fetchAPIKeys({ enabled: true })); | ||
dispatch( | ||
addToast({ message: GENERATE_API_KEY_SUCCESS, severity: "success" }) | ||
); | ||
}) | ||
.catch(() => undefined); | ||
}, | ||
[dispatch] | ||
); | ||
|
This file contains 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.
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.
Why do we have to change from switch case to multiple if 🤔
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.
If we generate an error with a message from MySQL, handling errors with a switch cannot work properly. That's why I had to change it to multi-if blocks.
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.
Which means the switch block can not check the type of datastore.ErrUserDefined error 🤔 It's a bit weird to me. Btw, how about adding test for this function. Looks like this function is testable without mocking anything.
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.
When an error message from MySQL is added to datastore.ErrUserDefined, checking with the switch block does not work because the new error is no longer the same as datastore.ErrUserDefined.
I agree with you to add tests for it 👍
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.
Added tests.