diff --git a/pkg/replicator/pgreplicator/initializer.go b/pkg/replicator/pgreplicator/initializer.go index eb885db..ce33c02 100644 --- a/pkg/replicator/pgreplicator/initializer.go +++ b/pkg/replicator/pgreplicator/initializer.go @@ -30,11 +30,15 @@ type InitializerOpts struct { func NewInitializer(ctx context.Context, opts InitializerOpts) (replicator.SystemInitializer[InitializeResult], error) { conn, err := pgx.ConnectConfig(ctx, &opts.AdminConfig) if err != nil { - return nil, ErrInvalidCredentials + newErr := ErrInvalidCredentials + newErr.Data = map[string]any{"detail": err.Error()} + return nil, newErr } defer conn.Close(ctx) if err := conn.Ping(ctx); err != nil { - return nil, ErrCannotCommunicate + newErr := ErrCannotCommunicate + newErr.Data = map[string]any{"detail": err.Error()} + return nil, newErr } return initializer[pgsetup.TestConnResult]{opts: opts}, nil } diff --git a/pkg/replicator/replicator.go b/pkg/replicator/replicator.go index 3ddacf5..20892fd 100644 --- a/pkg/replicator/replicator.go +++ b/pkg/replicator/replicator.go @@ -2,6 +2,7 @@ package replicator import ( "context" + "encoding/json" "github.com/inngest/dbcap/pkg/changeset" ) @@ -47,11 +48,6 @@ type ConnectionResult interface { Results() map[string]ConnectionStepResult } -type ConnectionStepResult struct { - Error error `json:"error"` - Complete bool `json:"complete"` -} - type SystemInitializer[T ConnectionResult] interface { // PerformInit perform setup for the replicator. PerformInit(ctx context.Context) (T, error) @@ -59,3 +55,28 @@ type SystemInitializer[T ConnectionResult] interface { // CheckInit ensures that the setup for the replicator is complete. CheckInit(ctx context.Context) (T, error) } + +type ConnectionStepResult struct { + Error error `json:"error"` + Complete bool `json:"complete"` +} + +func (c ConnectionStepResult) MarshalJSON() ([]byte, error) { + res := map[string]any{"complete": c.Complete} + + if c.Error == nil { + return json.Marshal(res) + } + + if m, ok := c.Error.(json.Marshaler); ok { + byt, err := m.MarshalJSON() + if err != nil { + return nil, err + } + res["error"] = json.RawMessage(byt) + return json.Marshal(res) + } + + res["error"] = c.Error.Error() + return json.Marshal(res) +}