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 ( |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 21 | "errors" |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 22 | "fmt" |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 23 | "regexp" |
| 24 | "strings" |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 25 | |
| 26 | "google.golang.org/grpc/status" |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var ( |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 30 | descRE = regexp.MustCompile(`desc = "(.*)"`) |
| 31 | try2RE = regexp.MustCompile(`all SubConns are in TransientFailure, latest connection error: (.*)`) |
| 32 | try3RE = regexp.MustCompile(`all SubConns are in TransientFailure, (.*)`) |
| 33 | NoReportErr = errors.New("no-report-please") |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | func ErrorToString(err error) string { |
| 37 | if err == nil { |
| 38 | return "" |
| 39 | } |
| 40 | |
| 41 | if st, ok := status.FromError(err); ok { |
| 42 | msg := st.Message() |
| 43 | if match := descRE.FindAllStringSubmatch(msg, 1); match != nil { |
| 44 | msg = match[0][1] |
| 45 | } else if match = try2RE.FindAllStringSubmatch(msg, 1); match != nil { |
| 46 | msg = match[0][1] |
| 47 | } else if match = try3RE.FindAllStringSubmatch(msg, 1); match != nil { |
| 48 | msg = match[0][1] |
| 49 | } |
| 50 | |
| 51 | return fmt.Sprintf("%s: %s", strings.ToUpper(st.Code().String()), msg) |
| 52 | } |
| 53 | return err.Error() |
| 54 | } |