Skip to content
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

[BEAM-12969] [Playground] Define and implement errors of the application #15615

Merged
merged 1 commit into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playground/backend/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package main

import (
"beam.apache.org/playground/backend/pkg/executors"
"beam.apache.org/playground/backend/internal/executors"
)

func main() {
Expand Down
36 changes: 36 additions & 0 deletions playground/backend/internal/errors/grpc_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// 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)
}
102 changes: 102 additions & 0 deletions playground/backend/internal/errors/grpc_errors_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Empty file removed playground/backend/test/.gitkeep
Empty file.