[#369] status: Make errors return default messages

Use default messages in `Error` methods like in `ToStatusV2`.

Signed-off-by: Pavel Karpy <p.karpy@yadro.com>
This commit is contained in:
Pavel Karpy 2022-12-08 19:34:52 +03:00 committed by Stanislav Bogatyrev
parent b4b07a3c4e
commit 45a6e7a7c2
4 changed files with 108 additions and 24 deletions

View file

@ -130,10 +130,17 @@ type SignatureVerification struct {
v2 status.Status v2 status.Status
} }
const defaultSignatureVerificationMsg = "signature verification failed"
func (x SignatureVerification) Error() string { func (x SignatureVerification) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultSignatureVerificationMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail), globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail),
x.v2.Message(), msg,
) )
} }
@ -153,7 +160,7 @@ func (x SignatureVerification) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail)) x.v2.SetCode(globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail))
if x.v2.Message() == "" { if x.v2.Message() == "" {
x.v2.SetMessage("signature verification failed") x.v2.SetMessage(defaultSignatureVerificationMsg)
} }
return &x.v2 return &x.v2
@ -181,11 +188,13 @@ type NodeUnderMaintenance struct {
v2 status.Status v2 status.Status
} }
const defaultNodeUnderMaintenanceMsg = "node is under maintenance"
// Error implements the error interface. // Error implements the error interface.
func (x NodeUnderMaintenance) Error() string { func (x NodeUnderMaintenance) Error() string {
msg := x.Message() msg := x.Message()
if msg == "" { if msg == "" {
msg = "node is under maintenance" msg = defaultNodeUnderMaintenanceMsg
} }
return errMessageStatusV2( return errMessageStatusV2(
@ -202,10 +211,15 @@ func (x *NodeUnderMaintenance) fromStatusV2(st *status.Status) {
// If the value was returned by FromStatusV2, returns the source message. // If the value was returned by FromStatusV2, returns the source message.
// Otherwise, returns message with // Otherwise, returns message with
// - code: NODE_UNDER_MAINTENANCE; // - code: NODE_UNDER_MAINTENANCE;
// - string message: written message via SetMessage; // - string message: written message via SetMessage or
// "node is under maintenance" as a default message;
// - details: empty. // - details: empty.
func (x NodeUnderMaintenance) ToStatusV2() *status.Status { func (x NodeUnderMaintenance) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(status.NodeUnderMaintenance, status.GlobalizeCommonFail)) x.v2.SetCode(globalizeCodeV2(status.NodeUnderMaintenance, status.GlobalizeCommonFail))
if x.v2.Message() == "" {
x.v2.SetMessage(defaultNodeUnderMaintenanceMsg)
}
return &x.v2 return &x.v2
} }

View file

@ -11,10 +11,17 @@ type ContainerNotFound struct {
v2 status.Status v2 status.Status
} }
const defaultContainerNotFoundMsg = "container not found"
func (x ContainerNotFound) Error() string { func (x ContainerNotFound) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultContainerNotFoundMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(container.StatusNotFound, container.GlobalizeFail), globalizeCodeV2(container.StatusNotFound, container.GlobalizeFail),
x.v2.Message(), msg,
) )
} }
@ -31,7 +38,7 @@ func (x *ContainerNotFound) fromStatusV2(st *status.Status) {
// - details: empty. // - details: empty.
func (x ContainerNotFound) ToStatusV2() *status.Status { func (x ContainerNotFound) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(container.StatusNotFound, container.GlobalizeFail)) x.v2.SetCode(globalizeCodeV2(container.StatusNotFound, container.GlobalizeFail))
x.v2.SetMessage("container not found") x.v2.SetMessage(defaultContainerNotFoundMsg)
return &x.v2 return &x.v2
} }
@ -42,10 +49,17 @@ type EACLNotFound struct {
v2 status.Status v2 status.Status
} }
const defaultEACLNotFoundMsg = "eACL not found"
func (x EACLNotFound) Error() string { func (x EACLNotFound) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultEACLNotFoundMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(container.StatusEACLNotFound, container.GlobalizeFail), globalizeCodeV2(container.StatusEACLNotFound, container.GlobalizeFail),
x.v2.Message(), msg,
) )
} }
@ -62,6 +76,6 @@ func (x *EACLNotFound) fromStatusV2(st *status.Status) {
// - details: empty. // - details: empty.
func (x EACLNotFound) ToStatusV2() *status.Status { func (x EACLNotFound) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(container.StatusEACLNotFound, container.GlobalizeFail)) x.v2.SetCode(globalizeCodeV2(container.StatusEACLNotFound, container.GlobalizeFail))
x.v2.SetMessage("eACL not found") x.v2.SetMessage(defaultEACLNotFoundMsg)
return &x.v2 return &x.v2
} }

View file

@ -11,10 +11,17 @@ type ObjectLocked struct {
v2 status.Status v2 status.Status
} }
const defaultObjectLockedMsg = "object is locked"
func (x ObjectLocked) Error() string { func (x ObjectLocked) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultObjectLockedMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(object.StatusLocked, object.GlobalizeFail), globalizeCodeV2(object.StatusLocked, object.GlobalizeFail),
x.v2.Message(), msg,
) )
} }
@ -31,7 +38,7 @@ func (x *ObjectLocked) fromStatusV2(st *status.Status) {
// - details: empty. // - details: empty.
func (x ObjectLocked) ToStatusV2() *status.Status { func (x ObjectLocked) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(object.StatusLocked, object.GlobalizeFail)) x.v2.SetCode(globalizeCodeV2(object.StatusLocked, object.GlobalizeFail))
x.v2.SetMessage("object is locked") x.v2.SetMessage(defaultObjectLockedMsg)
return &x.v2 return &x.v2
} }
@ -41,10 +48,17 @@ type LockNonRegularObject struct {
v2 status.Status v2 status.Status
} }
const defaultLockNonRegularObjectMsg = "locking non-regular object is forbidden"
func (x LockNonRegularObject) Error() string { func (x LockNonRegularObject) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultLockNonRegularObjectMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(object.StatusLockNonRegularObject, object.GlobalizeFail), globalizeCodeV2(object.StatusLockNonRegularObject, object.GlobalizeFail),
x.v2.Message(), msg,
) )
} }
@ -61,7 +75,7 @@ func (x *LockNonRegularObject) fromStatusV2(st *status.Status) {
// - details: empty. // - details: empty.
func (x LockNonRegularObject) ToStatusV2() *status.Status { func (x LockNonRegularObject) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(object.StatusLockNonRegularObject, object.GlobalizeFail)) x.v2.SetCode(globalizeCodeV2(object.StatusLockNonRegularObject, object.GlobalizeFail))
x.v2.SetMessage("locking non-regular object is forbidden") x.v2.SetMessage(defaultLockNonRegularObjectMsg)
return &x.v2 return &x.v2
} }
@ -71,10 +85,17 @@ type ObjectAccessDenied struct {
v2 status.Status v2 status.Status
} }
const defaultObjectAccessDeniedMsg = "access to object operation denied"
func (x ObjectAccessDenied) Error() string { func (x ObjectAccessDenied) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultObjectAccessDeniedMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(object.StatusAccessDenied, object.GlobalizeFail), globalizeCodeV2(object.StatusAccessDenied, object.GlobalizeFail),
x.v2.Message(), msg,
) )
} }
@ -91,7 +112,7 @@ func (x *ObjectAccessDenied) fromStatusV2(st *status.Status) {
// - details: empty. // - details: empty.
func (x ObjectAccessDenied) ToStatusV2() *status.Status { func (x ObjectAccessDenied) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(object.StatusAccessDenied, object.GlobalizeFail)) x.v2.SetCode(globalizeCodeV2(object.StatusAccessDenied, object.GlobalizeFail))
x.v2.SetMessage("access to object operation denied") x.v2.SetMessage(defaultObjectAccessDeniedMsg)
return &x.v2 return &x.v2
} }
@ -112,10 +133,17 @@ type ObjectNotFound struct {
v2 status.Status v2 status.Status
} }
const defaultObjectNotFoundMsg = "object not found"
func (x ObjectNotFound) Error() string { func (x ObjectNotFound) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultObjectNotFoundMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(object.StatusNotFound, object.GlobalizeFail), globalizeCodeV2(object.StatusNotFound, object.GlobalizeFail),
x.v2.Message(), msg,
) )
} }
@ -132,7 +160,7 @@ func (x *ObjectNotFound) fromStatusV2(st *status.Status) {
// - details: empty. // - details: empty.
func (x ObjectNotFound) ToStatusV2() *status.Status { func (x ObjectNotFound) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(object.StatusNotFound, object.GlobalizeFail)) x.v2.SetCode(globalizeCodeV2(object.StatusNotFound, object.GlobalizeFail))
x.v2.SetMessage("object not found") x.v2.SetMessage(defaultObjectNotFoundMsg)
return &x.v2 return &x.v2
} }
@ -142,10 +170,17 @@ type ObjectAlreadyRemoved struct {
v2 status.Status v2 status.Status
} }
const defaultObjectAlreadyRemovedMsg = "object already removed"
func (x ObjectAlreadyRemoved) Error() string { func (x ObjectAlreadyRemoved) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultObjectAlreadyRemovedMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(object.StatusAlreadyRemoved, object.GlobalizeFail), globalizeCodeV2(object.StatusAlreadyRemoved, object.GlobalizeFail),
x.v2.Message(), msg,
) )
} }
@ -162,7 +197,7 @@ func (x *ObjectAlreadyRemoved) fromStatusV2(st *status.Status) {
// - details: empty. // - details: empty.
func (x ObjectAlreadyRemoved) ToStatusV2() *status.Status { func (x ObjectAlreadyRemoved) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(object.StatusAlreadyRemoved, object.GlobalizeFail)) x.v2.SetCode(globalizeCodeV2(object.StatusAlreadyRemoved, object.GlobalizeFail))
x.v2.SetMessage("object already removed") x.v2.SetMessage(defaultObjectAlreadyRemovedMsg)
return &x.v2 return &x.v2
} }
@ -173,10 +208,17 @@ type ObjectOutOfRange struct {
v2 status.Status v2 status.Status
} }
const defaultObjectOutOfRangeMsg = "out of range"
func (x ObjectOutOfRange) Error() string { func (x ObjectOutOfRange) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultObjectOutOfRangeMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(object.StatusOutOfRange, object.GlobalizeFail), globalizeCodeV2(object.StatusOutOfRange, object.GlobalizeFail),
x.v2.Message(), msg,
) )
} }
@ -193,6 +235,6 @@ func (x *ObjectOutOfRange) fromStatusV2(st *status.Status) {
// - details: empty. // - details: empty.
func (x ObjectOutOfRange) ToStatusV2() *status.Status { func (x ObjectOutOfRange) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(object.StatusOutOfRange, object.GlobalizeFail)) x.v2.SetCode(globalizeCodeV2(object.StatusOutOfRange, object.GlobalizeFail))
x.v2.SetMessage("out of range") x.v2.SetMessage(defaultObjectOutOfRangeMsg)
return &x.v2 return &x.v2
} }

View file

@ -11,10 +11,17 @@ type SessionTokenNotFound struct {
v2 status.Status v2 status.Status
} }
const defaultSessionTokenNotFoundMsg = "session token not found"
func (x SessionTokenNotFound) Error() string { func (x SessionTokenNotFound) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultSessionTokenNotFoundMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(session.StatusTokenNotFound, session.GlobalizeFail), globalizeCodeV2(session.StatusTokenNotFound, session.GlobalizeFail),
x.v2.Message(), msg,
) )
} }
@ -31,7 +38,7 @@ func (x *SessionTokenNotFound) fromStatusV2(st *status.Status) {
// - details: empty. // - details: empty.
func (x SessionTokenNotFound) ToStatusV2() *status.Status { func (x SessionTokenNotFound) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(session.StatusTokenNotFound, session.GlobalizeFail)) x.v2.SetCode(globalizeCodeV2(session.StatusTokenNotFound, session.GlobalizeFail))
x.v2.SetMessage("session token not found") x.v2.SetMessage(defaultSessionTokenNotFoundMsg)
return &x.v2 return &x.v2
} }
@ -41,10 +48,17 @@ type SessionTokenExpired struct {
v2 status.Status v2 status.Status
} }
const defaultSessionTokenExpiredMsg = "expired session token"
func (x SessionTokenExpired) Error() string { func (x SessionTokenExpired) Error() string {
msg := x.v2.Message()
if msg == "" {
msg = defaultSessionTokenExpiredMsg
}
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(session.StatusTokenExpired, session.GlobalizeFail), globalizeCodeV2(session.StatusTokenExpired, session.GlobalizeFail),
x.v2.Message(), msg,
) )
} }
@ -61,6 +75,6 @@ func (x *SessionTokenExpired) fromStatusV2(st *status.Status) {
// - details: empty. // - details: empty.
func (x SessionTokenExpired) ToStatusV2() *status.Status { func (x SessionTokenExpired) ToStatusV2() *status.Status {
x.v2.SetCode(globalizeCodeV2(session.StatusTokenExpired, session.GlobalizeFail)) x.v2.SetCode(globalizeCodeV2(session.StatusTokenExpired, session.GlobalizeFail))
x.v2.SetMessage("expired session token") x.v2.SetMessage(defaultSessionTokenExpiredMsg)
return &x.v2 return &x.v2
} }