Skip to content

Commit

Permalink
Add NamespaceUnavailable serviceerror
Browse files Browse the repository at this point in the history
  • Loading branch information
bergundy committed Nov 21, 2024
1 parent 39e3fba commit 10ebbfd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
7 changes: 6 additions & 1 deletion serviceerror/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ func FromStatus(st *status.Status) error {
case codes.Canceled:
return newCanceled(st)
case codes.Unavailable:
return newUnavailable(st)
switch errDetails := errDetails.(type) {
case *errordetails.NamespaceUnavailableFailure:
return newNamespaceUnavailable(st, errDetails)
default:
return newUnavailable(st)
}
case codes.Unimplemented:
return newUnimplemented(st)
case codes.Unknown:
Expand Down
80 changes: 80 additions & 0 deletions serviceerror/namespace_unavailable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// The MIT License
//
// Copyright (c) 2024 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package serviceerror

import (
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"go.temporal.io/api/errordetails/v1"
)

type (
// NamespaceUnavailable is returned by the service when a request addresses a namespace that is unavailable. For
// example, when a namespace is in the process of failing over between clusters. This is a transient error that
// should be automatically retried by clients.
NamespaceUnavailable struct {
Namespace string
// Note that other service errors have a private status.Status field, there's no compelling reason to do
// copy that pattern here. The status can and should be computed on the fly in case the
// We'll just store the message from the wire here in case the code that generates it changes.
messageFromWire string
}
)

// NewNamespaceUnavailable returns new NamespaceInvalidState error.
func NewNamespaceUnavailable(namespace string) error {
return &NamespaceUnavailable{
Namespace: namespace,
}
}

// Error returns string message.
func (e *NamespaceUnavailable) Error() string {
if e.messageFromWire != "" {
return e.messageFromWire
}
// Continuing the practice of starting errors with upper case and ending with periods even if it's not
// idiomatic.
return fmt.Sprintf("Namespace unavailable: %q.", e.Namespace)
}

func (e *NamespaceUnavailable) Status() *status.Status {
st := status.New(codes.Unavailable, e.Error())
// We seem to be okay ignoring these errors everywhere else, doing this here too.
st, _ = st.WithDetails(
&errordetails.NamespaceUnavailableFailure{
Namespace: e.Namespace,
},
)
return st
}

func newNamespaceUnavailable(st *status.Status, errDetails *errordetails.NamespaceUnavailableFailure) error {
return &NamespaceUnavailable{
messageFromWire: st.Message(),
Namespace: errDetails.GetNamespace(),
}
}

0 comments on commit 10ebbfd

Please sign in to comment.