Skip to content

Commit

Permalink
backend error formatting; notif-listener name
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Aizman <[email protected]>
  • Loading branch information
alex-aizman committed Jan 1, 2024
1 parent 7a525d7 commit 67361c0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 32 deletions.
26 changes: 16 additions & 10 deletions ais/backend/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,27 +613,33 @@ func getBucketLocation(svc *s3.S3, bckName string) (region string, err error) {
}

func awsErrorToAISError(awsError error, bck *cmn.Bck) (int, error) {
if reqErr, ok := awsError.(awserr.RequestFailure); ok {
if reqErr.Code() == s3.ErrCodeNoSuchBucket {
return reqErr.StatusCode(), cmn.NewErrRemoteBckNotFound(bck)
}
return reqErr.StatusCode(), cleanError(awsError)
reqErr, ok := awsError.(awserr.RequestFailure)
if !ok {
return http.StatusInternalServerError, _awsErr(awsError)
}
awsCode, status := reqErr.Code(), reqErr.StatusCode()
switch awsCode {
case s3.ErrCodeNoSuchBucket:
return status, cmn.NewErrRemoteBckNotFound(bck)
case s3.ErrCodeNoSuchKey:
debug.Assert(status == http.StatusNotFound) // expected
fallthrough
default:
return status, _awsErr(awsError)
}

return http.StatusInternalServerError, cleanError(awsError)
}

// Original AWS error contains extra information that a caller does not need:
// status code: 400, request id: D918CB, host id: RJtDP0q8
// The extra information starts from the new line (`\n`) and tab (`\t`) of the message.
// At the same time we want to preserve original error which starts with `\ncaused by:`.
// See more `aws-sdk-go/aws/awserr/types.go:12` (`SprintError`).
func cleanError(awsError error) error {
func _awsErr(awsError error) error {
var (
msg = awsError.Error()
origErrMsg = awsError.Error()
)
// Strip extra information...
// Strip extra information
if idx := strings.Index(msg, "\n\t"); idx > 0 {
msg = msg[:idx]
}
Expand All @@ -642,5 +648,5 @@ func cleanError(awsError error) error {
// `idx+1` because we want to remove `\n`.
msg += " (" + origErrMsg[idx+1:] + ")"
}
return errors.New("aws-error[" + msg + "]")
return errors.New("aws-error[" + strings.TrimSuffix(msg, ".") + "]")
}
22 changes: 14 additions & 8 deletions ais/backend/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,22 @@ func readCredFile() (projectID string) {
}

func gcpErrorToAISError(gcpError error, bck *cmn.Bck) (int, error) {
if gcpError == storage.ErrBucketNotExist {
switch {
case gcpError == storage.ErrBucketNotExist:
return http.StatusNotFound, cmn.NewErrRemoteBckNotFound(bck)
case gcpError == storage.ErrObjectNotExist:
return http.StatusNotFound, _gcpErr(gcpError)
default:
if apiErr, ok := gcpError.(*googleapi.Error); ok {
return apiErr.Code, _gcpErr(gcpError)
}
return http.StatusInternalServerError, _gcpErr(gcpError)
}
status := http.StatusBadRequest
if apiErr, ok := gcpError.(*googleapi.Error); ok {
status = apiErr.Code
} else if gcpError == storage.ErrObjectNotExist {
status = http.StatusNotFound
}
return status, errors.New("gcp-error[" + gcpError.Error() + "]")
}

// (compare w/ _awsErr)
func _gcpErr(gcpError error) error {
return errors.New("gcp-error[" + gcpError.Error() + "]")
}

func handleObjectError(ctx context.Context, gcpClient *storage.Client, objErr error, bck *cmn.Bck) (int, error) {
Expand Down
2 changes: 1 addition & 1 deletion ais/ic.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (ic *ic) xstatusOne(w http.ResponseWriter, r *http.Request) {
if err := nl.Err(); err != nil {
status.ErrMsg = err.Error()
if !nl.Aborted() {
ic.p.writeErrf(w, r, "%v: %v", nl, err)
ic.p.writeErrf(w, r, "%s: %v", nl.Name(), err)
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions ais/prxnotif.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ func (n *notifs) add(nl nl.Listener) (err error) {
}
nl.SetAddedTime()
if cmn.FastV(5, cos.SmoduleAIS) {
nlog.Infoln("add " + nl.String())
nlog.Infoln("add", nl.Name())
}
return
}

func (n *notifs) del(nl nl.Listener, locked bool) (ok bool) {
ok = n.nls.del(nl, locked /*locked*/)
if ok && cmn.FastV(5, cos.SmoduleAIS) {
nlog.Infoln("del " + nl.String())
nlog.Infoln("del", nl.Name())
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion ais/tgtdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (t *target) downloadHandler(w http.ResponseWriter, r *http.Request) {
return
}
if cmn.FastV(4, cos.SmoduleAIS) {
nlog.Infoln("Downloading: " + dljob.ID())
nlog.Infoln("Downloading:", dljob.ID())
}

dljob.AddNotif(&dload.NotifDownload{
Expand Down
47 changes: 37 additions & 10 deletions nl/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
package nl

import (
"fmt"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -43,6 +44,7 @@ type Listener interface {
SetAddedTime()
AddedTime() int64
Finished() bool
Name() string
String() string
GetOwner() string
SetOwner(string)
Expand Down Expand Up @@ -214,20 +216,37 @@ func (nlb *ListenerBase) Status() *Status {
return &Status{Kind: nlb.Kind(), UUID: nlb.UUID(), EndTimeX: nlb.EndTimeX.Load(), AbortedX: nlb.Aborted()}
}

func (nlb *ListenerBase) _name() *strings.Builder {
var sb strings.Builder
sb.WriteString("nl-")
sb.WriteString(nlb.Kind())
sb.WriteByte('[')
sb.WriteString(nlb.UUID())
sb.WriteByte(']')
return &sb
}

func (nlb *ListenerBase) Name() string {
sb := nlb._name()
return sb.String()
}

func (nlb *ListenerBase) String() string {
var (
tm, res string
hdr = fmt.Sprintf("nl-%s[%s]", nlb.Kind(), nlb.UUID())
sb = nlb._name()
finCount = nlb.FinCount()
)
if nlb.Cause() != "" {
hdr += "-caused-by-" + nlb.Cause()
sb.WriteString("-caused-by-")
sb.WriteString(nlb.Cause())
}
if bcks := nlb.Bcks(); len(bcks) > 0 {
if len(bcks) == 1 {
hdr += "-" + bcks[0].String()
} else {
hdr += "-" + bcks[0].String() + "-" + bcks[1].String()
sb.WriteByte('-')
sb.WriteString(bcks[0].String())
if len(bcks) > 1 {
sb.WriteByte('-')
sb.WriteString(bcks[1].String())
}
}
if tfin := nlb.EndTimeX.Load(); tfin > 0 {
Expand All @@ -237,12 +256,20 @@ func (nlb *ListenerBase) String() string {
res = "-done"
}
tm = cos.FormatNanoTime(tfin, cos.StampMicro)
return fmt.Sprintf("%s-%s%s", hdr, tm, res)
sb.WriteByte('-')
sb.WriteString(tm)
sb.WriteString(res)
return sb.String()
}
if finCount > 0 {
return fmt.Sprintf("%s(cnt=%d/%d)", hdr, finCount, len(nlb.Srcs))
sb.WriteString("(cnt=")
sb.WriteString(strconv.Itoa(finCount))
sb.WriteByte('/')
sb.WriteString(strconv.Itoa(len(nlb.Srcs)))
sb.WriteByte(')')
return sb.String()
}
return hdr
return sb.String()
}

////////////
Expand Down

0 comments on commit 67361c0

Please sign in to comment.