Skip to content

Commit

Permalink
Update error detail logic
Browse files Browse the repository at this point in the history
  • Loading branch information
krapie committed Dec 11, 2023
1 parent 0381041 commit c97c1bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 9 additions & 5 deletions server/rpc/connecthelper/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"connectrpc.com/connect"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/protobuf/runtime/protoiface"

"github.com/yorkie-team/yorkie/api/converter"
"github.com/yorkie-team/yorkie/api/types"
Expand Down Expand Up @@ -87,7 +86,7 @@ var errorToCode = map[error]connect.Code{
context.Canceled: connect.CodeCanceled,
}

func detailsFromError(err error) (protoiface.MessageV1, bool) {
func detailsFromError(err error) (*errdetails.BadRequest, bool) {
invalidFieldsError, ok := err.(*validation.StructError)
if !ok {
return nil, false
Expand All @@ -102,6 +101,7 @@ func detailsFromError(err error) (protoiface.MessageV1, bool) {
}
br.FieldViolations = append(br.FieldViolations, v)
}

return br, true
}

Expand All @@ -122,9 +122,13 @@ func ToStatusError(err error) error {
var invalidFieldsError *validation.StructError
if errors.As(err, &invalidFieldsError) {
st := connect.NewError(connect.CodeInvalidArgument, err)
//if details, ok := detailsFromError(err); ok {
// st, _ = st.AddDetail(details)
//}
details, ok := detailsFromError(err)
if !ok {
return st
}
if detail, err := connect.NewErrorDetail(details); err == nil {
st.AddDetail(detail)
}
return st
}

Expand Down
2 changes: 0 additions & 2 deletions server/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ func (s *Server) listenAndServe() error {
}

func newCORS() *cors.Cors {
// To let web developers play with the demo service from browsers, we need a
// very permissive CORS setup.
return cors.New(cors.Options{
AllowedMethods: []string{
http.MethodHead,
Expand Down

0 comments on commit c97c1bf

Please sign in to comment.