|
| 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