David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Portions copyright 2019-present Open Networking Foundation |
| 3 | * Original copyright 2019-present Ciena Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package commands |
| 19 | |
| 20 | import ( |
| 21 | "fmt" |
| 22 | "google.golang.org/grpc/status" |
| 23 | "regexp" |
| 24 | "strings" |
| 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | descRE = regexp.MustCompile(`desc = "(.*)"`) |
| 29 | try2RE = regexp.MustCompile(`all SubConns are in TransientFailure, latest connection error: (.*)`) |
| 30 | try3RE = regexp.MustCompile(`all SubConns are in TransientFailure, (.*)`) |
| 31 | ) |
| 32 | |
| 33 | func ErrorToString(err error) string { |
| 34 | if err == nil { |
| 35 | return "" |
| 36 | } |
| 37 | |
| 38 | if st, ok := status.FromError(err); ok { |
| 39 | msg := st.Message() |
| 40 | if match := descRE.FindAllStringSubmatch(msg, 1); match != nil { |
| 41 | msg = match[0][1] |
| 42 | } else if match = try2RE.FindAllStringSubmatch(msg, 1); match != nil { |
| 43 | msg = match[0][1] |
| 44 | } else if match = try3RE.FindAllStringSubmatch(msg, 1); match != nil { |
| 45 | msg = match[0][1] |
| 46 | } |
| 47 | |
| 48 | return fmt.Sprintf("%s: %s", strings.ToUpper(st.Code().String()), msg) |
| 49 | } |
| 50 | return err.Error() |
| 51 | } |