blob: a1ebff578174be997d740846cd661edb7ff24187 [file] [log] [blame]
David Bainbridge0f758d42019-10-26 05:17:48 +00001/*
Joey Armstrong903c69d2024-02-01 19:46:39 -05002 * Portions Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
David Bainbridge0f758d42019-10-26 05:17:48 +00003 * 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
18package commands
19
20import (
David Bainbridge7052fe82020-03-25 10:37:00 -070021 "errors"
David Bainbridge0f758d42019-10-26 05:17:48 +000022 "fmt"
David Bainbridge0f758d42019-10-26 05:17:48 +000023 "regexp"
24 "strings"
David Bainbridge7052fe82020-03-25 10:37:00 -070025
26 "google.golang.org/grpc/status"
David Bainbridge0f758d42019-10-26 05:17:48 +000027)
28
29var (
David Bainbridge7052fe82020-03-25 10:37:00 -070030 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 Bainbridge0f758d42019-10-26 05:17:48 +000034)
35
36func 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}