Improve error message

This commit is contained in:
sunboyy 2021-02-14 11:48:09 +07:00
parent 18f37a3328
commit fa109a09a8
12 changed files with 269 additions and 66 deletions

View file

@ -1,20 +1,31 @@
package mongo
// GenerationError is an error from generating MongoDB repository
type GenerationError string
import (
"fmt"
)
func (err GenerationError) Error() string {
switch err {
case OperationNotSupportedError:
return "operation not supported"
case BsonTagNotFoundError:
return "bson tag not found"
}
return string(err)
// NewOperationNotSupportedError creates operationNotSupportedError
func NewOperationNotSupportedError(operationName string) error {
return operationNotSupportedError{OperationName: operationName}
}
// generation error constants
const (
OperationNotSupportedError GenerationError = "ERROR_OPERATION_NOT_SUPPORTED"
BsonTagNotFoundError GenerationError = "ERROR_BSON_TAG_NOT_FOUND"
)
type operationNotSupportedError struct {
OperationName string
}
func (err operationNotSupportedError) Error() string {
return fmt.Sprintf("operation '%s' not supported", err.OperationName)
}
// NewBsonTagNotFoundError creates bsonTagNotFoundError
func NewBsonTagNotFoundError(fieldName string) error {
return bsonTagNotFoundError{FieldName: fieldName}
}
type bsonTagNotFoundError struct {
FieldName string
}
func (err bsonTagNotFoundError) Error() string {
return fmt.Sprintf("bson tag of field '%s' not found", err.FieldName)
}

View file

@ -0,0 +1,36 @@
package mongo_test
import (
"testing"
"github.com/sunboyy/repogen/internal/mongo"
)
type ErrorTestCase struct {
Name string
Error error
ExpectedString string
}
func TestError(t *testing.T) {
testTable := []ErrorTestCase{
{
Name: "OperationNotSupportedError",
Error: mongo.NewOperationNotSupportedError("Stub"),
ExpectedString: "operation 'Stub' not supported",
},
{
Name: "BsonTagNotFoundError",
Error: mongo.NewBsonTagNotFoundError("AccessToken"),
ExpectedString: "bson tag of field 'AccessToken' not found",
},
}
for _, testCase := range testTable {
t.Run(testCase.Name, func(t *testing.T) {
if testCase.Error.Error() != testCase.ExpectedString {
t.Errorf("Expected = %v\nReceived = %v", testCase.ExpectedString, testCase.Error.Error())
}
})
}
}

View file

@ -87,9 +87,9 @@ func (g RepositoryGenerator) generateMethodImplementation(methodSpec spec.Method
return g.generateDeleteImplementation(operation)
case spec.CountOperation:
return g.generateCountImplementation(operation)
default:
return "", NewOperationNotSupportedError(operation.Name())
}
return "", OperationNotSupportedError
}
func (g RepositoryGenerator) generateInsertImplementation(operation spec.InsertOperation) (string, error) {
@ -201,7 +201,7 @@ func (g RepositoryGenerator) bsonTagFromFieldName(fieldName string) (string, err
bsonTag, ok := structField.Tags["bson"]
if !ok {
return "", BsonTagNotFoundError
return "", NewBsonTagNotFoundError(fieldName)
}
return bsonTag[0], nil

View file

@ -1453,6 +1453,13 @@ type GenerateMethodInvalidTestCase struct {
ExpectedError error
}
type StubOperation struct {
}
func (o StubOperation) Name() string {
return "Stub"
}
func TestGenerateMethod_Invalid(t *testing.T) {
testTable := []GenerateMethodInvalidTestCase{
{
@ -1467,9 +1474,9 @@ func TestGenerateMethod_Invalid(t *testing.T) {
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
code.SimpleType("error"),
},
Operation: "search",
Operation: StubOperation{},
},
ExpectedError: mongo.OperationNotSupportedError,
ExpectedError: mongo.NewOperationNotSupportedError("Stub"),
},
{
Name: "bson tag not found in query",
@ -1492,7 +1499,7 @@ func TestGenerateMethod_Invalid(t *testing.T) {
},
},
},
ExpectedError: mongo.BsonTagNotFoundError,
ExpectedError: mongo.NewBsonTagNotFoundError("AccessToken"),
},
{
Name: "bson tag not found in update field",
@ -1519,7 +1526,7 @@ func TestGenerateMethod_Invalid(t *testing.T) {
},
},
},
ExpectedError: mongo.BsonTagNotFoundError,
ExpectedError: mongo.NewBsonTagNotFoundError("AccessToken"),
},
}