From dfddf0e874033d7ac9f5fdde0ce1c163364ed756 Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Fri, 15 Dec 2023 15:47:54 +0100 Subject: [PATCH] for webapi requests, make canceled contexts a user instead of server error no need to trigger alerts for user-initiated errors --- webaccount/account.go | 6 +++++- webadmin/admin.go | 6 +++++- webmail/webmail.go | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/webaccount/account.go b/webaccount/account.go index 6782747..11132c5 100644 --- a/webaccount/account.go +++ b/webaccount/account.go @@ -78,7 +78,11 @@ func xcheckf(ctx context.Context, err error, format string, args ...any) { msg := fmt.Sprintf(format, args...) errmsg := fmt.Sprintf("%s: %s", msg, err) pkglog.WithContext(ctx).Errorx(msg, err) - panic(&sherpa.Error{Code: "server:error", Message: errmsg}) + code := "server:error" + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + code = "user:error" + } + panic(&sherpa.Error{Code: code, Message: errmsg}) } func xcheckuserf(ctx context.Context, err error, format string, args ...any) { diff --git a/webadmin/admin.go b/webadmin/admin.go index 5cd21ae..103e5fe 100644 --- a/webadmin/admin.go +++ b/webadmin/admin.go @@ -230,7 +230,11 @@ func xcheckf(ctx context.Context, err error, format string, args ...any) { msg := fmt.Sprintf(format, args...) errmsg := fmt.Sprintf("%s: %s", msg, err) pkglog.WithContext(ctx).Errorx(msg, err) - panic(&sherpa.Error{Code: "server:error", Message: errmsg}) + code := "server:error" + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + code = "user:error" + } + panic(&sherpa.Error{Code: code, Message: errmsg}) } func xcheckuserf(ctx context.Context, err error, format string, args ...any) { diff --git a/webmail/webmail.go b/webmail/webmail.go index b331444..dbcb3be 100644 --- a/webmail/webmail.go +++ b/webmail/webmail.go @@ -117,7 +117,11 @@ func xcheckf(ctx context.Context, err error, format string, args ...any) { msg := fmt.Sprintf(format, args...) errmsg := fmt.Sprintf("%s: %s", msg, err) pkglog.WithContext(ctx).Errorx(msg, err) - panic(&sherpa.Error{Code: "server:error", Message: errmsg}) + code := "server:error" + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + code = "user:error" + } + panic(&sherpa.Error{Code: code, Message: errmsg}) } func xcheckuserf(ctx context.Context, err error, format string, args ...any) {