diff --git a/engine/framework/fake/errors.go b/engine/framework/fake/errors.go index 71ee23307f3..2719448f42f 100644 --- a/engine/framework/fake/errors.go +++ b/engine/framework/fake/errors.go @@ -14,6 +14,7 @@ package fake import ( + libErrors "errors" "fmt" "regexp" @@ -32,14 +33,13 @@ func NewJobUnRetryableError(errIn error) *JobUnRetryableError { } } -// Message returns raw error message of JobUnRetryableError -func (e *JobUnRetryableError) Message() string { - return "fake job unretryable error" -} - // Error implements error interface func (e *JobUnRetryableError) Error() string { - return fmt.Sprintf("%s: %s", e.Message(), e.errIn) + return fmt.Sprintf("%s: %s", e.message(), e.errIn) +} + +func (e *JobUnRetryableError) message() string { + return "fake job unretryable error" } const fakeJobErrorFormat = "fake job unretryable error: (.*)" @@ -48,9 +48,15 @@ var fakeJobErrorRegexp = regexp.MustCompile(fakeJobErrorFormat) // ToFakeJobError tries best to construct a fake job error from an error object func ToFakeJobError(err error) error { + var errOut *JobUnRetryableError + if libErrors.As(err, &errOut) { + return err + } + subMatch := fakeJobErrorRegexp.FindStringSubmatch(err.Error()) if len(subMatch) > 1 { return NewJobUnRetryableError(errors.New(subMatch[1])) } + return err } diff --git a/engine/framework/fake/errors_test.go b/engine/framework/fake/errors_test.go new file mode 100644 index 00000000000..ed70a4bcbc6 --- /dev/null +++ b/engine/framework/fake/errors_test.go @@ -0,0 +1,33 @@ +// Copyright 2022 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package fake + +import ( + "testing" + + "github.com/pingcap/errors" + "github.com/stretchr/testify/require" +) + +func TestToFakeJobError(t *testing.T) { + t.Parallel() + + normalErr := errors.New("normal error") + fakeJobErr := NewJobUnRetryableError(normalErr) + errFromPlainText := errors.New(fakeJobErr.Error()) + + require.Equal(t, normalErr, ToFakeJobError(normalErr)) + require.Equal(t, fakeJobErr, ToFakeJobError(fakeJobErr)) + require.EqualError(t, ToFakeJobError(errFromPlainText), fakeJobErr.Error()) +}