typo in timeout tests + adding tests for standarderror

This commit is contained in:
Julien Neuhart
2019-07-08 17:50:31 +02:00
parent 9af00d6d58
commit 9c6f23d7cc
5 changed files with 77 additions and 4 deletions

View File

@@ -9,6 +9,7 @@ go test github.com/thecodingmachine/gotenberg/internal/pkg/pm2 -run TestChromeSt
go test github.com/thecodingmachine/gotenberg/internal/pkg/pm2 -run TestUnoconvStart
# Running others tests.
go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg/internal/pkg
go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg/internal/app/api
# Finally testing processes shutdown.

View File

@@ -64,6 +64,8 @@ func Code(err error) string {
return Internal
}
const defaultMessage = "an internal error has occurred: please contact technical support"
// Message returns the human-readable message of the error, if available.
// Otherwise returns a generic error message.
func Message(err error) string {
@@ -77,7 +79,7 @@ func Message(err error) string {
if ok && e.Err != nil {
return Message(e.Err)
}
return "An internal error has occurred. Please contact technical support."
return defaultMessage
}
// Op returns the logical operation of the error, if available.

View File

@@ -0,0 +1,70 @@
package standarderror
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func scenario1() error {
rootErr := errors.New("root error")
nestedErr := &Error{
Code: Invalid,
Op: "bar",
Message: "nested error",
Err: rootErr,
}
err := &Error{
Op: "foo",
Err: nestedErr,
}
return err
}
func scenario2() error {
nestedErr := &Error{
Code: Invalid,
Op: "bar",
Message: "nested error",
}
err := &Error{
Code: Internal,
Op: "foo",
Err: nestedErr,
}
return err
}
func TestError(t *testing.T) {
err := scenario1()
assert.Equal(t, "root error", err.Error())
err = scenario2()
assert.Equal(t, "<invalid> nested error", err.Error())
}
func TestCode(t *testing.T) {
assert.Equal(t, "", Code(nil))
err := scenario1()
assert.Equal(t, Invalid, Code(err))
err = scenario2()
assert.Equal(t, Internal, Code(err))
err = errors.New("some error")
assert.Equal(t, Internal, Code(err))
}
func TestMessage(t *testing.T) {
assert.Equal(t, "", Message(nil))
err := scenario1()
assert.Equal(t, "nested error", Message(err))
err = errors.New("some error")
assert.Equal(t, defaultMessage, Message(err))
}
func TestOp(t *testing.T) {
assert.Equal(t, "", Op(nil))
err := scenario1()
assert.Equal(t, "foo: bar", Op(err))
err = errors.New("some error")
assert.Equal(t, "", Op(err))
}

View File

@@ -27,12 +27,12 @@ func TestErr(t *testing.T) {
err := Err(ctx)
assert.NotNil(t, err)
standardized := test.RequireStandardError(t, err)
assert.Equal(t, standardized.Code, standarderror.Timeout)
assert.Equal(t, standarderror.Timeout, standardized.Code)
// should failed.
ctx, cancel = Context(5)
cancel()
err = Err(ctx)
assert.NotNil(t, err)
standardized = test.RequireStandardError(t, err)
assert.Equal(t, standarderror.Code(err), standarderror.Internal)
assert.Equal(t, standarderror.Internal, standarderror.Code(err))
}

View File

@@ -45,7 +45,7 @@ func AssertConcurrent(t *testing.T, fn func() error, amount int) {
// If so, returns the instance of standarderror.Error.
func RequireStandardError(t *testing.T, err error) *standarderror.Error {
standardized, ok := err.(*standarderror.Error)
require.Equal(t, ok, true)
require.Equal(t, true, ok)
return standardized
}