Skip to content

Commit

Permalink
refactor: optimize error message format
Browse files Browse the repository at this point in the history
Signed-off-by: mlycore <[email protected]>
  • Loading branch information
mlycore committed Sep 24, 2023
1 parent eab3e46 commit 0100137
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 80 deletions.
19 changes: 10 additions & 9 deletions pitr/agent/internal/handler/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,34 @@ import (
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/view"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/responder"

"github.com/gofiber/fiber/v2"
)

func Backup(ctx *fiber.Ctx) error {
in := &view.BackupIn{}

if err := ctx.BodyParser(in); err != nil {
return fmt.Errorf("body parse err=%s,wrap=%w", err, cons.BodyParseFailed)
return fmt.Errorf("body parse err: %s, wrap: %w", err, cons.BodyParseFailed)
}

if err := in.Validate(); err != nil {
return fmt.Errorf("invalid parameter,err=%w", err)
return fmt.Errorf("invalid parameter, err wrap: %w", err)
}

if err := pkg.OG.Auth(in.Username, in.Password, in.DBName, in.DBPort); err != nil {
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w"
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s], err wrap=%w"
return fmt.Errorf(efmt, in.Username, len(in.Password), in.DBName, err)
}

// try to add backup instance
if err := pkg.OG.AddInstance(in.DnBackupPath, in.Instance); err != nil && !errors.Is(err, cons.InstanceAlreadyExist) {
return fmt.Errorf("add instance failed, err=%w", err)
return fmt.Errorf("add instance failed, err wrap: %w", err)
}

backupID, err := pkg.OG.AsyncBackup(in.DnBackupPath, in.Instance, in.DnBackupMode, 1, in.DBPort)
if err != nil {
efmt := "pkg.OG.AsyncBackup[path=%s,instance=%s,mode=%s] failure,err=%w"
efmt := "pkg.OG.AsyncBackup[path=%s,instance=%s,mode=%s] failure, err wrap: %w"
return fmt.Errorf(efmt, in.DnBackupPath, in.Instance, in.DnBackupMode, err)
}

Expand All @@ -63,20 +64,20 @@ func Backup(ctx *fiber.Ctx) error {
func DeleteBackup(ctx *fiber.Ctx) error {
in := &view.DeleteBackupIn{}
if err := ctx.BodyParser(in); err != nil {
return fmt.Errorf("body parse err=%s,wrap=%w", err, cons.BodyParseFailed)
return fmt.Errorf("body parse err: %s, wrap: %w", err, cons.BodyParseFailed)
}

if err := in.Validate(); err != nil {
return fmt.Errorf("invalid parameter,err=%w", err)
return fmt.Errorf("invalid parameter, err: %w", err)
}

if err := pkg.OG.Auth(in.Username, in.Password, in.DBName, in.DBPort); err != nil {
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w"
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s], err: %w"
return fmt.Errorf(efmt, in.Username, len(in.Password), in.DBName, err)
}

if err := pkg.OG.DelBackup(in.DnBackupPath, in.Instance, in.BackupID); err != nil {
return fmt.Errorf("delete backup failure,err=%w", err)
return fmt.Errorf("delete backup failure, err: %w", err)
}

return responder.Success(ctx, "")
Expand Down
7 changes: 4 additions & 3 deletions pitr/agent/internal/handler/diskspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,26 @@ import (
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/view"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/cmds"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/responder"

"github.com/gofiber/fiber/v2"
)

func DiskSpace(ctx *fiber.Ctx) error {
in := &view.DiskSpaceIn{}

if err := ctx.BodyParser(in); err != nil {
return fmt.Errorf("body parse err=%s,wrap=%w", err, cons.BodyParseFailed)
return fmt.Errorf("body parse err: %s, wrap: %w", err, cons.BodyParseFailed)
}

if err := in.Validate(); err != nil {
return fmt.Errorf("invalid parameter,err=%w", err)
return fmt.Errorf("invalid parameter, err wrap: %w", err)
}

// show disk space
cmd := fmt.Sprintf("df -h %s", in.DiskPath)
output, err := cmds.Exec(os.Getenv("SHELL"), cmd)
if err != nil {
return fmt.Errorf("exec cmd [%s] failure,err=%w", cmd, err)
return fmt.Errorf("exec cmd [%s] failure, err wrap: %w", cmd, err)
}

return responder.Success(ctx, output)
Expand Down
7 changes: 4 additions & 3 deletions pitr/agent/internal/handler/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ import (
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/view"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/responder"

"github.com/gofiber/fiber/v2"
)

func HealthCheck(ctx *fiber.Ctx) error {
in := &view.HealthCheckIn{}
if err := ctx.BodyParser(in); err != nil {
return fmt.Errorf("body parse err=%s,wrap=%w", err, cons.BodyParseFailed)
return fmt.Errorf("body parse err: %s, wrap: %w", err, cons.BodyParseFailed)
}

if err := pkg.OG.Auth(in.Username, in.Password, in.DBName, in.DBPort); err != nil {
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w"
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s], err wrap: %w"
return fmt.Errorf(efmt, in.Username, len(in.Password), in.DBName, err)
}

// check schema if needed
if in.Schema != "" {
if err := pkg.OG.CheckSchema(in.Username, in.Password, in.DBName, in.DBPort, in.Schema); err != nil {
return fmt.Errorf("pkg.OG.CheckSchema return err=%s,wrap=%w", err, err)
return fmt.Errorf("pkg.OG.CheckSchema return err: %s, err wrap: %w", err, err)
}
}

Expand Down
31 changes: 15 additions & 16 deletions pitr/agent/internal/handler/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,67 +20,66 @@ package handler
import (
"fmt"

"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/view"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/responder"

"github.com/gofiber/fiber/v2"

"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/view"
)

func Restore(ctx *fiber.Ctx) (err error) {
in := &view.RestoreIn{}

if err = ctx.BodyParser(in); err != nil {
err = fmt.Errorf("body parse err=%s,wrap=%w", err, cons.BodyParseFailed)
err = fmt.Errorf("body parse err: %s, wrap: %w", err, cons.BodyParseFailed)
return
}

if err = in.Validate(); err != nil {
err = fmt.Errorf("invalid parameter,err=%w", err)
err = fmt.Errorf("invalid parameter, err wrap: %w", err)
return
}

if err = pkg.OG.Auth(in.Username, in.Password, in.DBName, in.DBPort); err != nil {
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w"
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s], err wrap: %w"
err = fmt.Errorf(efmt, in.Username, len(in.Password), in.DBName, err)
return
}

// stop openGauss
if err = pkg.OG.Stop(); err != nil {
err = fmt.Errorf("stop openGauss failure,err=%w", err)
err = fmt.Errorf("stop openGauss failure, err wrap: %w", err)
return
}

defer func() {
if err != nil {
err2 := pkg.OG.Start()
if err2 != nil {
err = fmt.Errorf("pkg.OG.Start() return err=%s,wrap=%w", err2, err)
startErr := pkg.OG.Start()
if startErr != nil {
err = fmt.Errorf("pkg.OG.Start() return err: %s, wrap: %w", startErr, err)
return
}
}
}()

// move pgdata to temp
if err = pkg.OG.MvPgDataToTemp(); err != nil {
err = fmt.Errorf("pkg.OG.MvPgDataToTemp return err=%w", err)
err = fmt.Errorf("pkg.OG.MvPgDataToTemp return err wrap: %w", err)
return
}

var status = "restoring"
defer func() {
if status != "restore success" {
err2 := pkg.OG.MvTempToPgData()
err = fmt.Errorf("resotre failre[err=%s],pkg.OG.MvTempToPgData return err=%w", err, err2)
mvErr := pkg.OG.MvTempToPgData()
err = fmt.Errorf("resotre failre[err=%s], pkg.OG.MvTempToPgData return err wrap: %w", err, mvErr)
}
}()

// restore data from backup
if err = pkg.OG.Restore(in.DnBackupPath, in.Instance, in.DnBackupID); err != nil {
efmt := "pkg.OG.Restore failure[path=%s,instance=%s,backupID=%s],err=%w"
efmt := "pkg.OG.Restore failure[path=%s,instance=%s,backupID=%s], err wrap: %w"
err = fmt.Errorf(efmt, in.DnBackupPath, in.Instance, in.DnBackupID, err)
status = "restore failure"
return
Expand All @@ -89,12 +88,12 @@ func Restore(ctx *fiber.Ctx) (err error) {

// clean temp
if err = pkg.OG.CleanPgDataTemp(); err != nil {
err = fmt.Errorf("pkg.OG.CleanPgDataTemp return err=%w", err)
err = fmt.Errorf("pkg.OG.CleanPgDataTemp return err wrap: %w", err)
return
}

if err = pkg.OG.Start(); err != nil {
err = fmt.Errorf("pkg.OG.Start return err=%w", err)
err = fmt.Errorf("pkg.OG.Start return err wrap: %w", err)
return
}

Expand Down
22 changes: 10 additions & 12 deletions pitr/agent/internal/handler/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,33 @@ package handler
import (
"fmt"

"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/view"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/responder"

"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/view"

"github.com/gofiber/fiber/v2"

"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
)

func Show(ctx *fiber.Ctx) error {
in := &view.ShowIn{}

if err := ctx.BodyParser(in); err != nil {
return fmt.Errorf("body parse err=%s,wrap=%w", err, cons.BodyParseFailed)
return fmt.Errorf("body parse err: %s, wrap: %w", err, cons.BodyParseFailed)
}

if err := in.Validate(); err != nil {
return fmt.Errorf("invalid parameter,err=%w", err)
return fmt.Errorf("invalid parameter, err wrap: %w", err)
}

if err := pkg.OG.Auth(in.Username, in.Password, in.DBName, in.DBPort); err != nil {
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w"
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s], err wrap: %w"
return fmt.Errorf(efmt, in.Username, len(in.Password), in.DBName, err)
}

data, err := pkg.OG.ShowBackup(in.DnBackupPath, in.Instance, in.DnBackupID)
if err != nil {
efmt := "pkg.OG.ShowBackupDetail failure[backupPath=%s,instance=%s,backupID=%s],err=%w"
efmt := "pkg.OG.ShowBackupDetail failure[backupPath=%s,instance=%s,backupID=%s], err wrap: %w"
return fmt.Errorf(efmt, in.DnBackupPath, in.Instance, in.DnBackupID, err)
}

Expand All @@ -59,22 +57,22 @@ func ShowList(ctx *fiber.Ctx) error {
in := &view.ShowListIn{}

if err := ctx.BodyParser(in); err != nil {
return fmt.Errorf("body parse err=%s,wrap=%w", err, cons.BodyParseFailed)
return fmt.Errorf("body parse err: %s, wrap: %w", err, cons.BodyParseFailed)
}

if err := in.Validate(); err != nil {
return fmt.Errorf("invalid parameter,err=%w", err)
return fmt.Errorf("invalid parameter, err wrap: %w", err)
}

if err := pkg.OG.Auth(in.Username, in.Password, in.DBName, in.DBPort); err != nil {
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w"
efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s], err wrap: %w"
return fmt.Errorf(efmt, in.Username, len(in.Password), in.DBName, err)
}

//Show list
list, err := pkg.OG.ShowBackupList(in.DnBackupPath, in.Instance)
if err != nil {
efmt := "pkg.OG.ShowBackupList failure[backupPath=%s,instance=%s],err=%w"
efmt := "pkg.OG.ShowBackupList failure[backupPath=%s,instance=%s], err wrap: %w"
return fmt.Errorf(efmt, in.DnBackupPath, in.Instance, err)
}

Expand Down
Loading

0 comments on commit 0100137

Please sign in to comment.