Print panic message when mmctl panics (#27390)

Этот коммит содержится в:
Ben Schumacher
2024-07-02 13:58:37 +02:00
коммит произвёл GitHub
родитель a3c1272cb6
Коммит 213ebc57fb
3 изменённых файлов: 38 добавлений и 7 удалений

Просмотреть файл

@@ -4,6 +4,8 @@
package commands package commands
import ( import (
"fmt"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"runtime/debug" "runtime/debug"
@@ -56,10 +58,9 @@ func Run(args []string) error {
defer func() { defer func() {
if x := recover(); x != nil { if x := recover(); x != nil {
printer.PrintError("Uh oh! Something unexpected happened :( Would you mind reporting it?\n") printPanic(x)
printer.PrintError(`https://github.com/mattermost/mmctl/issues/new?title=%5Bbug%5D%20panic%20on%20mmctl%20v` + Version + "&body=%3C!---%20Please%20provide%20the%20stack%20trace%20--%3E\n")
printer.PrintError(string(debug.Stack()))
_ = printer.Flush()
os.Exit(1) os.Exit(1)
} }
}() }()
@@ -67,6 +68,23 @@ func Run(args []string) error {
return RootCmd.Execute() return RootCmd.Execute()
} }
func printPanic(x any) {
u, err := url.Parse("https://github.com/mattermost/mattermost/issues/new")
if err != nil {
panic(err)
}
q := u.Query()
q.Add("title", "[mmctl] [bug] panic on v"+Version)
q.Add("body", "<!--- Please provide the stack trace -->\n")
u.RawQuery = q.Encode()
printer.PrintError("Uh oh! Something unexpected happened :( Would you mind reporting it?")
printer.PrintError(u.String() + "\n")
printer.PrintError(fmt.Sprintf("%s", x))
printer.PrintError(string(debug.Stack()))
}
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "mmctl", Use: "mmctl",
Short: "Remote client for the Open Source, self-hosted Slack-alternative", Short: "Remote client for the Open Source, self-hosted Slack-alternative",

Просмотреть файл

@@ -7,8 +7,11 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"strings" "strings"
"testing"
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
) )
func executeRawCommand(root *cobra.Command, args string) (c *cobra.Command, output string, err error) { func executeRawCommand(root *cobra.Command, args string) (c *cobra.Command, output string, err error) {
@@ -20,6 +23,16 @@ func executeRawCommand(root *cobra.Command, args string) (c *cobra.Command, outp
return c, actual.String(), err return c, actual.String(), err
} }
func TestRootRecover(t *testing.T) {
printPanic("some panic")
lines := printer.GetErrorLines()
assert.Equal(t, "Uh oh! Something unexpected happened :( Would you mind reporting it?", lines[0])
assert.True(t, strings.HasPrefix(lines[1], "https://github.com/mattermost/mattermost/issues/new?body="))
assert.Equal(t, "some panic", lines[2])
assert.True(t, strings.HasPrefix(lines[3], "goroutine "))
}
func (s *MmctlUnitTestSuite) TestArgumentsHaveWhitespaceTrimmed() { func (s *MmctlUnitTestSuite) TestArgumentsHaveWhitespaceTrimmed() {
arguments := []string{"value_1", "value_2"} arguments := []string{"value_1", "value_2"}
lineEndings := []string{"\n", "\r", "\r\n"} lineEndings := []string{"\n", "\r", "\r\n"}

Просмотреть файл

@@ -33,7 +33,7 @@ type Printer struct { //nolint
pager bool pager bool
Quiet bool Quiet bool
Lines []interface{} Lines []interface{}
ErrorLines []interface{} ErrorLines []string
cmd *cobra.Command cmd *cobra.Command
serverAddr string serverAddr string
@@ -193,7 +193,7 @@ func Flush() error {
defer func() { defer func() {
printer.Lines = []interface{}{} printer.Lines = []interface{}{}
printer.ErrorLines = []interface{}{} printer.ErrorLines = []string{}
}() }()
if cmd == nil || cmd.Name() != "list" || printer.cmd.Parent().Name() == "auth" { if cmd == nil || cmd.Name() != "list" || printer.cmd.Parent().Name() == "auth" {
@@ -232,7 +232,7 @@ func Flush() error {
// Clean resets the printer's accumulated lines // Clean resets the printer's accumulated lines
func Clean() { func Clean() {
printer.Lines = []interface{}{} printer.Lines = []interface{}{}
printer.ErrorLines = []interface{}{} printer.ErrorLines = []string{}
} }
// GetLines returns the printer's accumulated lines // GetLines returns the printer's accumulated lines
@@ -241,7 +241,7 @@ func GetLines() []interface{} {
} }
// GetErrorLines returns the printer's accumulated error lines // GetErrorLines returns the printer's accumulated error lines
func GetErrorLines() []interface{} { func GetErrorLines() []string {
return printer.ErrorLines return printer.ErrorLines
} }