Skip to content

Commit

Permalink
fix: 馃悰 test infrastructure bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Aug 9, 2023
1 parent 9868b4d commit 99b75a4
Show file tree
Hide file tree
Showing 59 changed files with 2,631 additions and 141 deletions.
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# npx --no -- commitlint --edit ${1}
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# make format && git add -A .
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: '@commitlint/config-conventional']};
2 changes: 1 addition & 1 deletion internal/pkg/bun/postgres/bun_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewBunDB(cfg *bun2.BunConfig) (*bun.DB, error) {
return nil, err
}

//https://bun.uptrace.dev/postgres/#pgdriver
// https://bun.uptrace.dev/postgres/#pgdriver
datasource := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=disable",
cfg.User,
cfg.Password,
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/core/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (m Metadata) Keys() []string {
i := 0
r := make([]string, len(m))

for k, _ := range m {
for k := range m {
r[i] = k
i++
}
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/es/contracts/store/aggregate_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

// AggregateStore is responsible for loading and saving Aggregate.
type AggregateStore[T models.IHaveEventSourcedAggregate] interface {

// StoreWithVersion store the new or update aggregate state with expected version
StoreWithVersion(
aggregate T,
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/es/contracts/store/event_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type EventStore interface {
//StreamExists Check if specific stream exists in the store
// StreamExists Check if specific stream exists in the store
StreamExists(streamName streamName.StreamName, ctx context.Context) (bool, error)

// ReadEventsFromStart Read events for existing stream and start position with specified events count.
Expand All @@ -22,15 +22,15 @@ type EventStore interface {
ctx context.Context,
) ([]*models.StreamEvent, error)

//ReadEvents Read events for a specific stream and position in forward mode with specified events count.
// ReadEvents Read events for a specific stream and position in forward mode with specified events count.
ReadEvents(
streamName streamName.StreamName,
readPosition readPosition.StreamReadPosition,
count uint64,
ctx context.Context,
) ([]*models.StreamEvent, error)

//ReadEventsWithMaxCount Read events for a specific stream and position in forward mode with max count.
// ReadEventsWithMaxCount Read events for a specific stream and position in forward mode with max count.
ReadEventsWithMaxCount(
streamName streamName.StreamName,
readPosition readPosition.StreamReadPosition,
Expand Down
14 changes: 7 additions & 7 deletions internal/pkg/es/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package errors

import (
"fmt"
"fmt"

"emperror.dev/errors"
"emperror.dev/errors"

customErrors "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/http/http_errors/custom_errors"
customErrors "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/http/http_errors/custom_errors"
)

var (
EventAlreadyExistsError = customErrors.NewConflictError(
fmt.Sprintf("domain_events event already exists in event registry"),
)
InvalidEventTypeError = errors.New("invalid event type")
EventAlreadyExistsError = customErrors.NewConflictError(
fmt.Sprintf("domain_events event already exists in event registry"),
)
InvalidEventTypeError = errors.New("invalid event type")
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package expectedStreamVersion

//https://github.com/EventStore/EventStore-Client-Go/blob/1591d047c0c448cacc0468f9af3605572aba7970/esdb/revision.go
//https://github.com/EventStore/EventStore-Client-Dotnet/blob/b8beee7b97ef359316822cb2d00f120bf67bd14d/src/EventStore.Client/StreamRevision.cs
// https://github.com/EventStore/EventStore-Client-Go/blob/1591d047c0c448cacc0468f9af3605572aba7970/esdb/revision.go
// https://github.com/EventStore/EventStore-Client-Dotnet/blob/b8beee7b97ef359316822cb2d00f120bf67bd14d/src/EventStore.Client/StreamRevision.cs

// ExpectedStreamVersion an int64 for accepts negative and positive value
type ExpectedStreamVersion int64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
customErrors "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/http/http_errors/custom_errors"
)

//https://klotzandrew.com/blog/error-handling-in-golang/
//https://banzaicloud.com/blog/error-handling-go/
// https://klotzandrew.com/blog/error-handling-in-golang/
// https://banzaicloud.com/blog/error-handling-go/

type aggregateNotFoundError struct {
customErrors.NotFoundError
Expand Down
15 changes: 7 additions & 8 deletions internal/pkg/grpc/grpcErrors/custom_grpc_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import (
)

func NewValidationGrpcError(detail string, stackTrace string) GrpcErr {
validationError :=
&grpcErr{
Title: constants.ErrBadRequestTitle,
Detail: detail,
Status: codes.InvalidArgument,
Timestamp: time.Now(),
StackTrace: stackTrace,
}
validationError := &grpcErr{
Title: constants.ErrBadRequestTitle,
Detail: detail,
Status: codes.InvalidArgument,
Timestamp: time.Now(),
StackTrace: stackTrace,
}

return validationError
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/grpc/grpcErrors/grpc_error_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
errorUtils "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/utils/error_utils"
)

//https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
//https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
// https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
// https://github.com/grpc/grpc/blob/master/doc/statuscodes.md

func ParseError(err error) GrpcErr {
customErr := customErrors.GetCustomError(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {

resp, err := handler(ctx, req)
if err != nil {
grpcErr := grpcErrors.ParseError(err)

if grpcErr != nil {
return nil, grpcErr.ToGrpcResponseErr()

} else {
prb := grpcErrors.NewInternalServerGrpcError(err.Error(), fmt.Sprintf("%+v\n", err))
return nil, prb.ToGrpcResponseErr()
Expand All @@ -44,13 +42,11 @@ func StreamServerInterceptor() grpc.StreamServerInterceptor {
handler grpc.StreamHandler,
) error {
err := handler(srv, ss)

if err != nil {
grpcErr := grpcErrors.ParseError(err)

if grpcErr != nil {
return grpcErr.ToGrpcResponseErr()

} else {
prb := grpcErrors.NewInternalServerGrpcError(err.Error(), fmt.Sprintf("%+v\n", err))
return prb.ToGrpcResponseErr()
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/http/custom_echo/otel/tracing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TraceHttpErrFromSpanWithCode(span trace.Span, err error, code int) error {
}

func TraceHttpErrFromContext(ctx context.Context, err error) error {
//https://opentelemetry.io/docs/instrumentation/go/manual/#record-errors
// https://opentelemetry.io/docs/instrumentation/go/manual/#record-errors
span := trace.SpanFromContext(ctx)
defer span.End()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (b *badRequestError) IsBadRequestError() bool {

func IsBadRequestError(err error) bool {
var badRequestError BadRequestError
//us, ok := grpc_errors.Cause(err).(BadRequestError)
// us, ok := grpc_errors.Cause(err).(BadRequestError)
if errors.As(err, &badRequestError) {
return badRequestError.IsBadRequestError()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *conflictError) IsConflictError() bool {

func IsConflictError(err error) bool {
var conflictError ConflictError
//us, ok := grpc_errors.Cause(err).(ConflictError)
// us, ok := grpc_errors.Cause(err).(ConflictError)
if errors.As(err, &conflictError) {
return conflictError.IsConflictError()
}
Expand Down
27 changes: 13 additions & 14 deletions internal/pkg/http/http_errors/custom_errors/custom_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ func Test_BadRequest_Err(t *testing.T) {

var stackErr contracts.StackTracer
if ok := errors.As(err, &stackErr); ok {
//https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
// https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
fmt.Println(errorUtils.ErrorsWithoutStack(err, false)) // Just write errorUtils messages for
fmt.Println(errorUtils.ErrorsWithStack(err)) //write errorUtils messages with stacktrace
fmt.Println(errorUtils.ErrorsWithStack(err)) // write errorUtils messages with stacktrace
} else {
fmt.Println(errorUtils.ErrorsWithoutStack(err, false))
}

}

func Test_NotFound_Err(t *testing.T) {
Expand All @@ -66,9 +65,9 @@ func Test_NotFound_Err(t *testing.T) {

var stackErr contracts.StackTracer
if ok := errors.As(err, &stackErr); ok {
//https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
// https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
fmt.Println(errorUtils.ErrorsWithoutStack(err, false)) // Just write errorUtils messages for
fmt.Println(errorUtils.ErrorsWithStack(err)) //write errorUtils messages with stacktrace
fmt.Println(errorUtils.ErrorsWithStack(err)) // write errorUtils messages with stacktrace
} else {
fmt.Println(errorUtils.ErrorsWithoutStack(err, false))
}
Expand Down Expand Up @@ -97,9 +96,9 @@ func Test_Domain_Err(t *testing.T) {

var stackErr contracts.StackTracer
if ok := errors.As(err, &stackErr); ok {
//https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
// https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
fmt.Println(errorUtils.ErrorsWithoutStack(err, false)) // Just write errorUtils messages for
fmt.Println(errorUtils.ErrorsWithStack(err)) //write errorUtils messages with stacktrace
fmt.Println(errorUtils.ErrorsWithStack(err)) // write errorUtils messages with stacktrace
} else {
fmt.Println(errorUtils.ErrorsWithoutStack(err, false))
}
Expand Down Expand Up @@ -132,7 +131,7 @@ func Test_Application_Err(t *testing.T) {

var stackErr contracts.StackTracer
if ok := errors.As(err, &stackErr); ok {
//https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
// https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
fmt.Println(errorUtils.ErrorsWithoutStack(err, false)) // Just write errorUtils messages for
fmt.Println(errorUtils.ErrorsWithStack(err))
} else {
Expand All @@ -159,7 +158,7 @@ func Test_Internal_Server_Error(t *testing.T) {

var stackErr contracts.StackTracer
if ok := errors.As(err, &stackErr); ok {
//https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
// https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
fmt.Println(errorUtils.ErrorsWithoutStack(err, false)) // Just write errorUtils messages for
fmt.Println(errorUtils.ErrorsWithStack(err))
} else {
Expand Down Expand Up @@ -199,7 +198,7 @@ func Test_Marshaling_Error(t *testing.T) {

var stackErr contracts.StackTracer
if ok := errors.As(err, &stackErr); ok {
//https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
// https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
fmt.Println(errorUtils.ErrorsWithoutStack(err, false)) // Just write errorUtils messages for
fmt.Println(errorUtils.ErrorsWithStack(err))
} else {
Expand Down Expand Up @@ -239,7 +238,7 @@ func Test_Validation_Error(t *testing.T) {

var stackErr contracts.StackTracer
if ok := errors.As(err, &stackErr); ok {
//https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
// https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
fmt.Println(errorUtils.ErrorsWithoutStack(err, false)) // Just write errorUtils messages for
fmt.Println(errorUtils.ErrorsWithStack(err))
} else {
Expand Down Expand Up @@ -274,7 +273,7 @@ func Test_Conflict_Error(t *testing.T) {

var stackErr contracts.StackTracer
if ok := errors.As(err, &stackErr); ok {
//https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
// https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package
fmt.Println(errorUtils.ErrorsWithoutStack(err, false)) // Just write errorUtils messages for
fmt.Println(errorUtils.ErrorsWithStack(err))
} else {
Expand All @@ -283,8 +282,8 @@ func Test_Conflict_Error(t *testing.T) {
}

func myfoo(e error) error {
//https://itnext.io/golang-error-handling-best-practice-a36f47b0b94c
//Note: Do not repeat Wrap, it will record redundancy call stacks, we usually care about root stack trace
// https://itnext.io/golang-error-handling-best-practice-a36f47b0b94c
// Note: Do not repeat Wrap, it will record redundancy call stacks, we usually care about root stack trace
return errors.WithMessage(e, "foo failed") // or grpc_errors.WrapIf()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (f *forbiddenError) IsForbiddenError() bool {

func IsForbiddenError(err error) bool {
var forbiddenError ForbiddenError
//us, ok := grpc_errors.Cause(err).(ForbiddenError)
// us, ok := grpc_errors.Cause(err).(ForbiddenError)
if errors.As(err, &forbiddenError) {
return forbiddenError.IsForbiddenError()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (i *internalServerError) IsInternalServerError() bool {

func IsInternalServerError(err error) bool {
var internalErr InternalServerError
//us, ok := grpc_errors.Cause(err).(InternalServerError)
// us, ok := grpc_errors.Cause(err).(InternalServerError)
if errors.As(err, &internalErr) {
return internalErr.IsInternalServerError()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m *marshalingError) IsMarshalingError() bool {
func IsMarshalingError(err error) bool {
var me MarshalingError

//us, ok := grpc_errors.Cause(err).(MarshalingError)
// us, ok := grpc_errors.Cause(err).(MarshalingError)
if errors.As(err, &me) {
return me.IsMarshalingError()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (n *notFoundError) IsNotFoundError() bool {

func IsNotFoundError(err error) bool {
var notFoundError NotFoundError
//us, ok := grpc_errors.Cause(err).(NotFoundError)
// us, ok := grpc_errors.Cause(err).(NotFoundError)
if errors.As(err, &notFoundError) {
return notFoundError.IsNotFoundError()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (u *unauthorizedError) IsUnAuthorizedError() bool {

func IsUnAuthorizedError(err error) bool {
var unauthorizedError UnauthorizedError
//us, ok := grpc_errors.Cause(err).(UnauthorizedError)
// us, ok := grpc_errors.Cause(err).(UnauthorizedError)
if errors.As(err, &unauthorizedError) {
return unauthorizedError.IsUnAuthorizedError()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (u *unMarshalingError) IsUnMarshalingError() bool {

func IsUnMarshalingError(err error) bool {
var unMarshalingError UnMarshalingError
//us, ok := grpc_errors.Cause(err).(UnMarshalingError)
// us, ok := grpc_errors.Cause(err).(UnMarshalingError)
if errors.As(err, &unMarshalingError) {
return unMarshalingError.IsUnMarshalingError()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (v *validationError) IsValidationError() bool {

func IsValidationError(err error) bool {
var validationError ValidationError
//us, ok := grpc_errors.Cause(err).(ValidationError)
// us, ok := grpc_errors.Cause(err).(ValidationError)
if errors.As(err, &validationError) {
return validationError.IsValidationError()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import (
)

func NewValidationProblemDetail(detail string, stackTrace string) ProblemDetailErr {
validationError :=
&problemDetail{
Title: constants.ErrBadRequestTitle,
Detail: detail,
Status: http.StatusBadRequest,
Type: getDefaultType(http.StatusBadRequest),
Timestamp: time.Now(),
StackTrace: stackTrace,
}
validationError := &problemDetail{
Title: constants.ErrBadRequestTitle,
Detail: detail,
Status: http.StatusBadRequest,
Type: getDefaultType(http.StatusBadRequest),
Timestamp: time.Now(),
StackTrace: stackTrace,
}

return validationError
}
Expand Down
Loading

0 comments on commit 99b75a4

Please sign in to comment.