-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement server errors and add validation errors (#112)
- Loading branch information
1 parent
d3db15b
commit 6b7c627
Showing
12 changed files
with
286 additions
and
29 deletions.
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,47 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
pberr "github.com/multycloud/multy/api/proto/errors" | ||
"github.com/multycloud/multy/util" | ||
"github.com/multycloud/multy/validate" | ||
"google.golang.org/genproto/googleapis/rpc/code" | ||
spb "google.golang.org/genproto/googleapis/rpc/status" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/types/known/anypb" | ||
) | ||
|
||
func PermissionDenied(msg string) error { | ||
return status.ErrorProto(&spb.Status{ | ||
Code: int32(code.Code_PERMISSION_DENIED), | ||
Message: msg, | ||
Details: nil, | ||
}) | ||
} | ||
|
||
func InternalServerError(err error) error { | ||
if _, ok := status.FromError(err); ok { | ||
return err | ||
} | ||
|
||
return status.ErrorProto(&spb.Status{ | ||
Code: int32(code.Code_INTERNAL), | ||
Message: err.Error(), | ||
Details: nil, | ||
}) | ||
} | ||
|
||
func ValidationErrors(errs []validate.ValidationError) error { | ||
return status.ErrorProto(&spb.Status{ | ||
Code: int32(code.Code_INVALID_ARGUMENT), | ||
Message: fmt.Sprintf("%d validation errors found", len(errs)), | ||
Details: util.MapSliceValues(errs, func(e validate.ValidationError) *anypb.Any { | ||
a, _ := anypb.New(&pberr.ResourceValidationError{ | ||
ResourceId: e.ResourceId, | ||
ErrorMessage: e.ErrorMessage, | ||
FieldName: e.FieldName, | ||
}) | ||
return a | ||
}), | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
syntax = "proto3"; | ||
|
||
option go_package = "github.com/multycloud/multy/api/proto/errors"; | ||
option java_multiple_files = true; | ||
option java_package = "dev.multy.api.errors"; | ||
option java_outer_classname = "MultyProto"; | ||
|
||
package dev.multy.config; | ||
|
||
message ResourceValidationError { | ||
string resource_id = 1; | ||
string error_message = 2; | ||
// this is tentative, it might not be populated or might have a different name from the proto request | ||
string field_name = 3; | ||
} |
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
Oops, something went wrong.