formatCoderSDKError come from API requests. In verbose mode, add the request debug information.
(from string, err *codersdk.Error, opts *formatOpts)
| 1462 | // formatCoderSDKError come from API requests. In verbose mode, add the |
| 1463 | // request debug information. |
| 1464 | func formatCoderSDKError(from string, err *codersdk.Error, opts *formatOpts) string { |
| 1465 | var str strings.Builder |
| 1466 | if opts.Verbose { |
| 1467 | // If all these fields are empty, then do not print this information. |
| 1468 | // This can occur if the error is being used outside the api. |
| 1469 | if !(err.Method() == "" && err.URL() == "" && err.StatusCode() == 0) { |
| 1470 | _, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("API request error to \"%s:%s\". Status code %d", err.Method(), err.URL(), err.StatusCode()))) |
| 1471 | _, _ = str.WriteString("\n") |
| 1472 | } |
| 1473 | } |
| 1474 | // Always include this trace. Users can ignore this. |
| 1475 | if from != "" { |
| 1476 | _, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("Trace=[%s]", from))) |
| 1477 | _, _ = str.WriteString("\n") |
| 1478 | } |
| 1479 | |
| 1480 | // The main error message |
| 1481 | _, _ = str.WriteString(pretty.Sprint(headLineStyle(), err.Message)) |
| 1482 | |
| 1483 | // Validation errors. |
| 1484 | if len(err.Validations) > 0 { |
| 1485 | _, _ = str.WriteString("\n") |
| 1486 | _, _ = str.WriteString(pretty.Sprint(tailLineStyle(), fmt.Sprintf("%d validation error(s) found", len(err.Validations)))) |
| 1487 | for _, e := range err.Validations { |
| 1488 | _, _ = str.WriteString("\n\t") |
| 1489 | _, _ = str.WriteString(pretty.Sprint(cliui.DefaultStyles.Field, e.Field)) |
| 1490 | _, _ = str.WriteString(pretty.Sprintf(cliui.DefaultStyles.Warn, ": %s", e.Detail)) |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | if err.Helper != "" { |
| 1495 | _, _ = str.WriteString("\n") |
| 1496 | _, _ = str.WriteString(pretty.Sprintf(tailLineStyle(), "Suggestion: %s", err.Helper)) |
| 1497 | } |
| 1498 | // By default we do not show the Detail with the helper. |
| 1499 | if opts.Verbose || (err.Helper == "" && err.Detail != "") { |
| 1500 | _, _ = str.WriteString("\n") |
| 1501 | _, _ = str.WriteString(pretty.Sprint(tailLineStyle(), err.Detail)) |
| 1502 | } |
| 1503 | return str.String() |
| 1504 | } |
| 1505 | |
| 1506 | // traceError is a helper function that aides developers debugging failed cli |
| 1507 | // commands. When we pretty print errors, we lose the context in which they came. |
no test coverage detected