Skip to content

Commit c617e5a

Browse files
committed
Add NamespaceUnavailable serviceerror
1 parent 39e3fba commit c617e5a

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

serviceerror/convert.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,15 @@ func FromStatus(st *status.Status) error {
100100
case codes.Canceled:
101101
return newCanceled(st)
102102
case codes.Unavailable:
103+
103104
return newUnavailable(st)
104105
case codes.Unimplemented:
105-
return newUnimplemented(st)
106+
switch errDetails := errDetails.(type) {
107+
case *errordetails.NamespaceUnavailableFailure:
108+
return newNamespaceUnavailable(st, errDetails)
109+
default:
110+
return newUnimplemented(st)
111+
}
106112
case codes.Unknown:
107113
// Unwrap error message from unknown error.
108114
return errors.New(st.Message())
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// The MIT License
2+
//
3+
// Copyright (c) 2024 Temporal Technologies Inc. All rights reserved.
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in
13+
// all copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
// THE SOFTWARE.
22+
23+
package serviceerror
24+
25+
import (
26+
"fmt"
27+
28+
"google.golang.org/grpc/codes"
29+
"google.golang.org/grpc/status"
30+
31+
"go.temporal.io/api/errordetails/v1"
32+
)
33+
34+
type (
35+
// NamespaceUnavailable is returned by the service when a request addresses a namespace that is unavailable. For
36+
// example, when a namespace is in the process of failing over between clusters. This is a transient error that
37+
// should be automatically retried by clients.
38+
NamespaceUnavailable struct {
39+
Namespace string
40+
// Note that other service errors have a private status.Status field, there's no compelling reason to do
41+
// copy that pattern here. The status can and should be computed on the fly in case the
42+
// We'll just store the message from the wire here in case the code that generates it changes.
43+
messageFromWire string
44+
}
45+
)
46+
47+
// NewNamespaceUnavailable returns new NamespaceInvalidState error.
48+
func NewNamespaceUnavailable(namespace string) error {
49+
return &NamespaceUnavailable{
50+
Namespace: namespace,
51+
}
52+
}
53+
54+
// Error returns string message.
55+
func (e *NamespaceUnavailable) Error() string {
56+
if e.messageFromWire != "" {
57+
return e.messageFromWire
58+
}
59+
// Continuing the practice of starting errors with upper case and ending with periods even if it's not
60+
// idiomatic.
61+
return fmt.Sprintf("Namespace unavailable: %q.", e.Namespace)
62+
}
63+
64+
func (e *NamespaceUnavailable) Status() *status.Status {
65+
st := status.New(codes.Unavailable, e.Error())
66+
// We seem to be okay ignoring these errors everywhere else, doing this here too.
67+
st, _ = st.WithDetails(
68+
&errordetails.NamespaceUnavailableFailure{
69+
Namespace: e.Namespace,
70+
},
71+
)
72+
return st
73+
}
74+
75+
func newNamespaceUnavailable(st *status.Status, errDetails *errordetails.NamespaceUnavailableFailure) error {
76+
return &NamespaceUnavailable{
77+
messageFromWire: st.Message(),
78+
Namespace: errDetails.GetNamespace(),
79+
}
80+
}

0 commit comments

Comments
 (0)