Exemplo n.º 1
0
func TestRootCause(t *testing.T) {
	for _, test := range []struct {
		err       error
		rootCause error
	}{
		{
			err:       nil,
			rootCause: nil,
		},
		{
			err:       errors.New("msg"),
			rootCause: errors.New("msg"),
		},
		{
			err:       stacktrace.NewError("msg"),
			rootCause: errors.New("msg"),
		},
		{
			err:       stacktrace.Propagate(stacktrace.NewError("msg1"), "msg2"),
			rootCause: errors.New("msg1"),
		},
		{
			err:       customError("msg"),
			rootCause: customError("msg"),
		},
		{
			err:       stacktrace.Propagate(customError("msg1"), "msg2"),
			rootCause: customError("msg1"),
		},
	} {
		assert.Equal(t, test.rootCause, stacktrace.RootCause(test.err))
	}
}
Exemplo n.º 2
0
func TestGetCode(t *testing.T) {
	for _, test := range []struct {
		originalError error
		originalCode  stacktrace.ErrorCode
	}{
		{
			originalError: errors.New("err"),
			originalCode:  stacktrace.NoCode,
		},
		{
			originalError: stacktrace.NewError("err"),
			originalCode:  stacktrace.NoCode,
		},
		{
			originalError: stacktrace.NewErrorWithCode(EcodeInvalidVillain, "err"),
			originalCode:  EcodeInvalidVillain,
		},
		{
			originalError: stacktrace.NewMessageWithCode(EcodeNoSuchPseudo, "err"),
			originalCode:  EcodeNoSuchPseudo,
		},
	} {
		err := test.originalError
		assert.Equal(t, test.originalCode, stacktrace.GetCode(err))

		err = stacktrace.Propagate(err, "")
		assert.Equal(t, test.originalCode, stacktrace.GetCode(err))

		err = stacktrace.PropagateWithCode(err, EcodeNotFastEnough, "")
		assert.Equal(t, EcodeNotFastEnough, stacktrace.GetCode(err))

		err = stacktrace.PropagateWithCode(err, EcodeTimeIsIllusion, "")
		assert.Equal(t, EcodeTimeIsIllusion, stacktrace.GetCode(err))
	}
}
Exemplo n.º 3
0
func TestPropagateNil(t *testing.T) {
	var err error

	err = stacktrace.Propagate(err, "")
	assert.Nil(t, err)

	err = stacktrace.PropagateWithCode(err, EcodeNotImplemented, "")
	assert.Nil(t, err)

	assert.Equal(t, stacktrace.NoCode, stacktrace.GetCode(err))
}
// Up is executed when this migration is applied
func Up_20151127032902(txn *sql.Tx) {
	gdb := config.NewDB()
	models.New(gdb)
	defer gdb.Close()

	pts := models.PatternParts{
		models.PatternPart{Key: "lecturer", Value: `rav|norav`},
		models.PatternPart{Key: "lang", Value: `[[:lower:]]{3,4}`},
		models.PatternPart{Key: "name", Value: `[a-z\-\d]+`},
		models.PatternPart{Key: "content_type", Value: `[[:lower:]]+`},
		models.PatternPart{Key: "line", Value: `[a-z\-\d]+`},
		models.PatternPart{Key: "ot", Value: `o|t`},
		models.PatternPart{Key: "date", Value: `\d{4}-\d{2}-\d{2}`},
		models.PatternPart{Key: "cam", Value: `cam\d*_\d|xdcam\d*_\d{2,3}|cam\d*|xdcam\d*`},
		models.PatternPart{Key: "archive_type", Value: `kabbalah|arvut|ligdol`},
		models.PatternPart{Key: "index", Value: `n\d`},
	}

	for _, pt := range pts {
		if err := pt.Save(); err != nil {
			log.Panicln(stacktrace.Propagate(err, "Unable to save pattern", pt))
		}
	}
}
Exemplo n.º 5
0
func doClosure(err error) error {
	return func() error {
		return stacktrace.Propagate(err, "so closed")
	}()
}
Exemplo n.º 6
0
func (*ptrObj) doPtr(err error) error {
	return stacktrace.Propagate(err, "pointedly")
}
Exemplo n.º 7
0
func (privateObj) doPrivate(err error) error {
	return stacktrace.Propagate(err, "")
}
Exemplo n.º 8
0
func (privateObj) DoPublic(err error) error {
	return stacktrace.Propagate(err, "")
}
Exemplo n.º 9
0
func TestFormat(t *testing.T) {
	plainErr := errors.New("plain")
	stacktraceErr := stacktrace.Propagate(plainErr, "decorated")
	digits := regexp.MustCompile(`\d`)

	for _, test := range []struct {
		format             stacktrace.Format
		specifier          string
		expectedPlain      string
		expectedStacktrace string
	}{
		{
			format:             stacktrace.FormatFull,
			specifier:          "%v",
			expectedPlain:      "plain",
			expectedStacktrace: "decorated\n --- at github.com/palantir/stacktrace/format_test.go:## (TestFormat) ---\nCaused by: plain",
		},
		{
			format:             stacktrace.FormatFull,
			specifier:          "%q",
			expectedPlain:      "\"plain\"",
			expectedStacktrace: "\"decorated\\n --- at github.com/palantir/stacktrace/format_test.go:## (TestFormat) ---\\nCaused by: plain\"",
		},
		{
			format:             stacktrace.FormatFull,
			specifier:          "%105s",
			expectedPlain:      "                                                                                                    plain",
			expectedStacktrace: "     decorated\n --- at github.com/palantir/stacktrace/format_test.go:## (TestFormat) ---\nCaused by: plain",
		},
		{
			format:             stacktrace.FormatFull,
			specifier:          "%#s",
			expectedPlain:      "plain",
			expectedStacktrace: "decorated: plain",
		},
		{
			format:             stacktrace.FormatBrief,
			specifier:          "%v",
			expectedPlain:      "plain",
			expectedStacktrace: "decorated: plain",
		},
		{
			format:             stacktrace.FormatBrief,
			specifier:          "%q",
			expectedPlain:      "\"plain\"",
			expectedStacktrace: "\"decorated: plain\"",
		},
		{
			format:             stacktrace.FormatBrief,
			specifier:          "%20s",
			expectedPlain:      "               plain",
			expectedStacktrace: "    decorated: plain",
		},
		{
			format:             stacktrace.FormatBrief,
			specifier:          "%+s",
			expectedPlain:      "plain",
			expectedStacktrace: "decorated\n --- at github.com/palantir/stacktrace/format_test.go:## (TestFormat) ---\nCaused by: plain",
		},
	} {
		stacktrace.DefaultFormat = test.format

		actualPlain := fmt.Sprintf(test.specifier, plainErr)
		assert.Equal(t, test.expectedPlain, actualPlain)

		actualStacktrace := fmt.Sprintf(test.specifier, stacktraceErr)
		actualStacktrace = digits.ReplaceAllString(actualStacktrace, "#")
		assert.Equal(t, test.expectedStacktrace, actualStacktrace)
	}
}