errors.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "errors"
  9. "fmt"
  10. "strings"
  11. "github.com/urfave/cli"
  12. )
  13. var errorsCommand = cli.Command{
  14. Name: "errors",
  15. HideHelp: true,
  16. Usage: "Error command group",
  17. Subcommands: []cli.Command{
  18. {
  19. Name: "show",
  20. Usage: "Show pending errors",
  21. Action: expects(0, dumpOutput("system/error")),
  22. },
  23. {
  24. Name: "push",
  25. Usage: "Push an error to active clients",
  26. ArgsUsage: "[error message]",
  27. Action: expects(1, errorsPush),
  28. },
  29. {
  30. Name: "clear",
  31. Usage: "Clear pending errors",
  32. Action: expects(0, emptyPost("system/error/clear")),
  33. },
  34. },
  35. }
  36. func errorsPush(c *cli.Context) error {
  37. client := c.App.Metadata["client"].(*APIClient)
  38. errStr := strings.Join(c.Args(), " ")
  39. response, err := client.Post("system/error", strings.TrimSpace(errStr))
  40. if err != nil {
  41. return err
  42. }
  43. if response.StatusCode != 200 {
  44. errStr = fmt.Sprint("Failed to push error\nStatus code: ", response.StatusCode)
  45. bytes, err := responseToBArray(response)
  46. if err != nil {
  47. return err
  48. }
  49. body := string(bytes)
  50. if body != "" {
  51. errStr += "\nBody: " + body
  52. }
  53. return errors.New(errStr)
  54. }
  55. return nil
  56. }