Skip to content

Commit

Permalink
[Playground] [Backend] added validation for snippet endpoints to avoi…
Browse files Browse the repository at this point in the history
…d error panic (#22686)

* [Playground] [Backend] added validation for snippet endpoints to avoid panic error

* [Playground] [Backend] edited error type

* [Playground] [Backend] updated error message
  • Loading branch information
vchunikhin authored Aug 12, 2022
1 parent 7a9bb76 commit 417692a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions playground/backend/cmd/server/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
package main

import (
"context"

"github.com/google/uuid"

pb "beam.apache.org/playground/backend/internal/api/v1"
"beam.apache.org/playground/backend/internal/cache"
"beam.apache.org/playground/backend/internal/cloud_bucket"
Expand All @@ -26,8 +30,6 @@ import (
"beam.apache.org/playground/backend/internal/logger"
"beam.apache.org/playground/backend/internal/setup_tools/life_cycle"
"beam.apache.org/playground/backend/internal/utils"
"context"
"github.com/google/uuid"
)

const (
Expand Down Expand Up @@ -363,6 +365,10 @@ func (controller *playgroundController) SaveSnippet(ctx context.Context, info *p
logger.Errorf("SaveSnippet(): unimplemented sdk: %s\n", info.Sdk)
return nil, errors.InvalidArgumentError(errorTitleSaveSnippet, "Sdk is not implemented yet: %s", info.Sdk.String())
}
if controller.db == nil {
logger.Error("SaveSnippet(): the runner is trying to save the snippet")
return nil, errors.InvalidArgumentError(errorTitleSaveSnippet, "The runner doesn't support snippets")
}
if info.Files == nil || len(info.Files) == 0 {
logger.Error("SaveSnippet(): files are empty")
return nil, errors.InvalidArgumentError(errorTitleSaveSnippet, "Snippet must have files")
Expand Down Expand Up @@ -399,6 +405,10 @@ func (controller *playgroundController) SaveSnippet(ctx context.Context, info *p

// GetSnippet returns the snippet entity
func (controller *playgroundController) GetSnippet(ctx context.Context, info *pb.GetSnippetRequest) (*pb.GetSnippetResponse, error) {
if controller.db == nil {
logger.Error("GetSnippet(): the runner is trying to read the snippet")
return nil, errors.InvalidArgumentError(errorTitleGetSnippet, "The runner doesn't support snippets")
}
snippet, err := controller.db.GetSnippet(ctx, info.GetId())
if err != nil {
logger.Errorf("GetSnippet(): error during getting the snippet: %s", err.Error())
Expand Down

0 comments on commit 417692a

Please sign in to comment.