Skip to content

Commit

Permalink
refactor: adjust agent server to raw error and cli error display (#472)
Browse files Browse the repository at this point in the history
* feat: introduce RawError and middleware

Signed-off-by: mlycore <[email protected]>

* feat: introduce new errors

Signed-off-by: mlycore <[email protected]>

* refactor: using raw error and new error code

Signed-off-by: mlycore <[email protected]>

* refactor: using InvalidHttpRequestBody

Signed-off-by: mlycore <[email protected]>

* refactor: using raw error as cmd error

Signed-off-by: mlycore <[email protected]>

* fix: fix MvTempToPgData error handle

Signed-off-by: mlycore <[email protected]>

* feat: introduce http request raw error

Signed-off-by: mlycore <[email protected]>

* refactor: using unwrapped error

Signed-off-by: mlycore <[email protected]>

* chore: adjust style

Signed-off-by: mlycore <[email protected]>

* chore: adjust style

Signed-off-by: mlycore <[email protected]>

* chore: adjust style

Signed-off-by: mlycore <[email protected]>

* chore: adjust style

Signed-off-by: mlycore <[email protected]>

* chore: adjust style

Signed-off-by: mlycore <[email protected]>

* chore: adjust style

Signed-off-by: mlycore <[email protected]>

---------

Signed-off-by: mlycore <[email protected]>
  • Loading branch information
mlycore authored Nov 23, 2023
1 parent 23554ab commit 8ce3577
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 118 deletions.
76 changes: 45 additions & 31 deletions pitr/agent/internal/cons/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,49 @@ import (
)

var (
Internal = xerror.New(10000, "Internal error.")
InvalidHTTPHeader = xerror.New(10001, "Invalid http header.")
DataNotFound = xerror.New(10002, "Data not found.")
CmdOperateFailed = xerror.New(10003, "Command operate failed.")
BackupPathAlreadyExist = xerror.New(10004, "The backup path already exists.")
NoPermission = xerror.New(10005, "No permission to operate.")
InstanceAlreadyExist = xerror.New(10006, "The instance already exist.")
InstanceNotExist = xerror.New(10007, "The instance not exist.")
StartOpenGaussFailed = xerror.New(10008, "Failed to start opengauss.")
StopOpenGaussFailed = xerror.New(10009, "Failed to stop opengauss.")
RestoreFailed = xerror.New(10010, "Failed to restore opengauss.")
InvalidDBPort = xerror.New(10011, "Invalid db port or missing db port.")
MissingUsername = xerror.New(10012, "Missing username")
MissingPassword = xerror.New(10013, "Missing password.")
MissingDnBackupPath = xerror.New(10014, "Missing dn backup path.")
InvalidDnThreadsNum = xerror.New(10015, "Invalid dn threads num.")
MissingDnBackupMode = xerror.New(10016, "Missing dn backup mode.")
InvalidDnBackupMode = xerror.New(10017, "Invalid dn backup mode.")
MissingInstance = xerror.New(10018, "Missing instance.")
MissingDnBackupID = xerror.New(10019, "Missing dn backup id.")
BodyParseFailed = xerror.New(10020, "Invalid http request body.")
MissingDBName = xerror.New(10021, "Missing db name.")
DBConnectionFailed = xerror.New(10022, "Database connection failed.")
UnmatchBackupID = xerror.New(10023, "Unmatch any backup id.")
InvalidPgDataDir = xerror.New(10024, "Invalid PGDATA dir.")
UnknownOgStatus = xerror.New(10025, "Unknown openGauss status.")
MvPgDataToTempFailed = xerror.New(10026, "Move pgdata dir to temp failed.")
MvTempToPgDataFailed = xerror.New(10027, "Move temp dir to pgdata failed.")
CleanPgDataTempFailed = xerror.New(10028, "Clean pgdata temp dir failed.")
MissingDiskPath = xerror.New(10029, "Missing disk path.")
MissingBackupID = xerror.New(10030, "Missing backup id.")
Internal = xerror.New(10000, "Agent server internal error.")
InvalidHTTPHeader = xerror.New(10001, "Invalid http header.")
DataNotFound = xerror.New(10002, "Data not found.")
CmdOperateFailed = xerror.New(10003, "Command operate failed.")
BackupPathAlreadyExist = xerror.New(10004, "The backup path already exists.")
NoPermission = xerror.New(10005, "No permission to operate.")
InstanceAlreadyExist = xerror.New(10006, "The instance already exist.")
InstanceNotExist = xerror.New(10007, "The instance not exist.")
StartOpenGaussFailed = xerror.New(10008, "Failed to start opengauss.")
StopOpenGaussFailed = xerror.New(10009, "Failed to stop opengauss.")
RestoreFailed = xerror.New(10010, "Failed to restore opengauss.")
InvalidDBPort = xerror.New(10011, "Invalid db port or missing db port.")
MissingUsername = xerror.New(10012, "Missing username")
MissingPassword = xerror.New(10013, "Missing password.")
MissingDnBackupPath = xerror.New(10014, "Missing dn backup path.")
InvalidDnThreadsNum = xerror.New(10015, "Invalid dn threads num.")
MissingDnBackupMode = xerror.New(10016, "Missing dn backup mode.")
InvalidDnBackupMode = xerror.New(10017, "Invalid dn backup mode.")
MissingInstance = xerror.New(10018, "Missing instance.")
MissingDnBackupID = xerror.New(10019, "Missing dn backup id.")
BodyParseFailed = xerror.New(10020, "Invalid http request body.")
MissingDBName = xerror.New(10021, "Missing db name.")
DBConnectionFailed = xerror.New(10022, "Database connection failed.")
UnmatchBackupID = xerror.New(10023, "Unmatch any backup id.")
InvalidPgDataDir = xerror.New(10024, "Invalid PGDATA dir.")
UnknownOgStatus = xerror.New(10025, "Unknown openGauss status.")
MvPgDataToTempFailed = xerror.New(10026, "Move pgdata dir to temp failed.")
MvTempToPgDataFailed = xerror.New(10027, "Move temp dir to pgdata failed.")
CleanPgDataTempFailed = xerror.New(10028, "Clean pgdata temp dir failed.")
MissingDiskPath = xerror.New(10029, "Missing disk path.")
MissingBackupID = xerror.New(10030, "Missing backup id.")
MissingDBInformation = xerror.New(10031, "Missing db information.")
GetBackupIDFailed = xerror.New(10032, "Get backup id failed.")
InvalidHTTPRequestBody = xerror.New(10033, "Invalid http request body")
JSONUnmarshalFailed = xerror.New(10034, "Json unmarshal failed.")
CmdAsyncBackupFailed = xerror.New(10035, "Command `gs_probackup backup` failed.")
CmdShowBackupFailed = xerror.New(10036, "Command `gs_probackup show` failed.")
CmdDeleteBackupFailed = xerror.New(10037, "Command `gs_probackup delete` failed.")
CmdInitBackupFailed = xerror.New(10038, "Command `gs_probackup init` failed.")
CmdAddInstanceFailed = xerror.New(10039, "Command `gs_probackup add-instance` failed.")
CmdDelInstanceFailed = xerror.New(10040, "Command `gs_probackup del-instance` failed.")
CmdStartOpenGaussFailed = xerror.New(10041, "Command `gs_ctl start` failed.")
CmdStopOpenGaussFailed = xerror.New(10042, "Command `gs_ctl stop` failed.")
CmdStatusOpenGaussFailed = xerror.New(10043, "Command `gs_ctl status` failed.")
CmdAsyncRestoreFailed = xerror.New(10044, "Command `gs_ctl restore` failed.")
)
17 changes: 16 additions & 1 deletion pitr/agent/internal/handler/middleware/uniform_err_resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,25 @@ func UniformErrResp(log logging.ILog) fiber.Handler {
log.Fields(map[logging.FieldKey]string{
logging.ErrorKey: err.Error(),
logging.RequestID: ctx.Get(cons.RequestID),
}).Error("UniformErrResp:an error occurred")
}).Error("UniformErrResp: an error occurred")
if e, b := xerror.FromError(err); b {
return responder.Error(ctx, e)
}
return responder.Error(ctx, cons.Internal)
}
}

func UniformRawErrResp(log logging.ILog) fiber.Handler {
return func(ctx *fiber.Ctx) error {
err := ctx.Next()
if err == nil {
return nil
}
//nolint:exhaustive
log.Fields(map[logging.FieldKey]string{
logging.ErrorKey: err.Error(),
logging.RequestID: ctx.Get(cons.RequestID),
}).Error("UniformErrResp: an error occurred")
return responder.RawError(ctx, err)
}
}
9 changes: 6 additions & 3 deletions pitr/agent/internal/handler/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ func Restore(ctx *fiber.Ctx) (err error) {
defer func() {
if status != "restore success" {
mvErr := pkg.OG.MvTempToPgData()
err = fmt.Errorf("resotre failre[err=%s], pkg.OG.MvTempToPgData return err wrap: %w", err, mvErr)
if mvErr != nil {
err = fmt.Errorf("restore failure[err=%s], pkg.OG.MvTempToPgData return err: %s", err, mvErr)
return
}
err = fmt.Errorf("restore failure[err=%s]", err)
return
}
}()

// restore data from backup
if err = pkg.OG.Restore(in.DnBackupPath, in.Instance, in.DnBackupID, in.DnThreadsNum); err != nil {
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 Down
2 changes: 1 addition & 1 deletion pitr/agent/internal/handler/view/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type RestoreIn struct {
//nolint:dupl
func (in *RestoreIn) Validate() error {
if in == nil {
return cons.Internal
return cons.InvalidHTTPRequestBody
}

if in.DBPort == 0 {
Expand Down
Loading

0 comments on commit 8ce3577

Please sign in to comment.