You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by pa...@apache.org on 2021/10/06 20:43:16 UTC

[beam] branch master updated: [BEAM-12969] [Playground] Add methods to return errors as a result of requests to server

This is an automated email from the ASF dual-hosted git repository.

pabloem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 821293e  [BEAM-12969] [Playground] Add methods to return errors as a result of requests to server
     new 840121b  Merge pull request #15615 from [BEAM-12969] [Playground] Define and implement errors of the application
821293e is described below

commit 821293e7bcc33e6d46350e8ba11e488e045fc8aa
Author: AydarZaynutdinov <ay...@akvelon.com>
AuthorDate: Tue Oct 5 22:02:59 2021 +0300

    [BEAM-12969] [Playground]
    Add methods to return errors as a result of requests to server
---
 playground/backend/cmd/server/server.go            |   2 +-
 playground/backend/{test => internal/api}/.gitkeep |   0
 .../server.go => internal/errors/grpc_errors.go}   |  20 +++-
 .../backend/internal/errors/grpc_errors_test.go    | 102 +++++++++++++++++++++
 .../{pkg => internal}/executors/executor.go        |   0
 .../{pkg => internal}/executors/goexecutor.go      |   0
 .../{pkg => internal}/executors/javaexecutor.go    |   0
 playground/backend/pkg/api/.gitkeep                |   0
 8 files changed, 119 insertions(+), 5 deletions(-)

diff --git a/playground/backend/cmd/server/server.go b/playground/backend/cmd/server/server.go
index e4b0f9a..bbeb155 100644
--- a/playground/backend/cmd/server/server.go
+++ b/playground/backend/cmd/server/server.go
@@ -16,7 +16,7 @@
 package main
 
 import (
-	"beam.apache.org/playground/backend/pkg/executors"
+	"beam.apache.org/playground/backend/internal/executors"
 )
 
 func main() {
diff --git a/playground/backend/test/.gitkeep b/playground/backend/internal/api/.gitkeep
similarity index 100%
rename from playground/backend/test/.gitkeep
rename to playground/backend/internal/api/.gitkeep
diff --git a/playground/backend/cmd/server/server.go b/playground/backend/internal/errors/grpc_errors.go
similarity index 52%
copy from playground/backend/cmd/server/server.go
copy to playground/backend/internal/errors/grpc_errors.go
index e4b0f9a..6919622 100644
--- a/playground/backend/cmd/server/server.go
+++ b/playground/backend/internal/errors/grpc_errors.go
@@ -13,12 +13,24 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package errors
 
 import (
-	"beam.apache.org/playground/backend/pkg/executors"
+	"google.golang.org/grpc/codes"
+	"google.golang.org/grpc/status"
 )
 
-func main() {
-	_ = executors.GoExecutor{}
+// InvalidArgumentError Returns error with InvalidArgument code error and message like "title: message"
+func InvalidArgumentError(title string, message string) error {
+	return status.Errorf(codes.InvalidArgument, "%s: %s", title, message)
+}
+
+// NotFoundError Returns error with NotFound code error and message like "title: message"
+func NotFoundError(title string, message string) error {
+	return status.Errorf(codes.NotFound, "%s: %s", title, message)
+}
+
+// InternalError Returns error with Internal code error and message like "title: message"
+func InternalError(title string, message string) error {
+	return status.Errorf(codes.Internal, "%s: %s", title, message)
 }
diff --git a/playground/backend/internal/errors/grpc_errors_test.go b/playground/backend/internal/errors/grpc_errors_test.go
new file mode 100644
index 0000000..ef9720e
--- /dev/null
+++ b/playground/backend/internal/errors/grpc_errors_test.go
@@ -0,0 +1,102 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 errors
+
+import (
+	"strings"
+	"testing"
+)
+
+func TestInternalError(t *testing.T) {
+	type args struct {
+		title   string
+		message string
+	}
+	tests := []struct {
+		name     string
+		args     args
+		expected string
+		wantErr  bool
+	}{
+		{name: "TestInternalError", args: args{title: "TEST_TITLE", message: "TEST_MESSAGE"},
+			expected: "rpc error: code = Internal desc = TEST_TITLE: TEST_MESSAGE", wantErr: true},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			err := InternalError(tt.args.title, tt.args.message)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("InternalError() error = %v, wantErr %v", err, tt.wantErr)
+			}
+			if !strings.EqualFold(err.Error(), tt.expected) {
+				t.Errorf("InternalError() error = %v, wantErr %v", err.Error(), tt.expected)
+			}
+		})
+	}
+}
+
+func TestInvalidArgumentError(t *testing.T) {
+	type args struct {
+		title   string
+		message string
+	}
+	tests := []struct {
+		name     string
+		args     args
+		expected string
+		wantErr  bool
+	}{
+		{name: "TestInvalidArgumentError", args: args{title: "TEST_TITLE", message: "TEST_MESSAGE"},
+			expected: "rpc error: code = InvalidArgument desc = TEST_TITLE: TEST_MESSAGE", wantErr: true},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			err := InvalidArgumentError(tt.args.title, tt.args.message)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("InvalidArgumentError() error = %v, wantErr %v", err, tt.wantErr)
+			}
+			if !strings.EqualFold(err.Error(), tt.expected) {
+				t.Errorf("InvalidArgumentError() error = %v, wantErr %v", err.Error(), tt.expected)
+			}
+		})
+	}
+}
+
+func TestNotFoundError(t *testing.T) {
+	type args struct {
+		title   string
+		message string
+	}
+	tests := []struct {
+		name     string
+		args     args
+		expected string
+		wantErr  bool
+	}{
+		{name: "TestNotFoundError", args: args{title: "TEST_TITLE", message: "TEST_MESSAGE"},
+			expected: "rpc error: code = NotFound desc = TEST_TITLE: TEST_MESSAGE", wantErr: true},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			err := NotFoundError(tt.args.title, tt.args.message)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("NotFoundError() error = %v, wantErr %v", err, tt.wantErr)
+			}
+			if !strings.EqualFold(err.Error(), tt.expected) {
+				t.Errorf("NotFoundError() error = %v, wantErr %v", err.Error(), tt.expected)
+			}
+		})
+	}
+}
diff --git a/playground/backend/pkg/executors/executor.go b/playground/backend/internal/executors/executor.go
similarity index 100%
rename from playground/backend/pkg/executors/executor.go
rename to playground/backend/internal/executors/executor.go
diff --git a/playground/backend/pkg/executors/goexecutor.go b/playground/backend/internal/executors/goexecutor.go
similarity index 100%
rename from playground/backend/pkg/executors/goexecutor.go
rename to playground/backend/internal/executors/goexecutor.go
diff --git a/playground/backend/pkg/executors/javaexecutor.go b/playground/backend/internal/executors/javaexecutor.go
similarity index 100%
rename from playground/backend/pkg/executors/javaexecutor.go
rename to playground/backend/internal/executors/javaexecutor.go
diff --git a/playground/backend/pkg/api/.gitkeep b/playground/backend/pkg/api/.gitkeep
deleted file mode 100644
index e69de29..0000000