2021-11-12 11:37:05 +00:00
|
|
|
package response
|
|
|
|
|
2023-03-13 11:55:19 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
sdkstatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
2021-11-12 11:37:05 +00:00
|
|
|
|
|
|
|
func Error(r *fasthttp.RequestCtx, msg string, code int) {
|
|
|
|
r.Error(msg+"\n", code)
|
|
|
|
}
|
2023-03-13 11:55:19 +00:00
|
|
|
|
|
|
|
func FormErrorResponse(message string, err error) (int, string, []zap.Field) {
|
|
|
|
var (
|
|
|
|
msg string
|
|
|
|
statusCode int
|
|
|
|
logFields []zap.Field
|
|
|
|
)
|
|
|
|
|
|
|
|
st := new(sdkstatus.ObjectAccessDenied)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case errors.As(err, &st):
|
|
|
|
statusCode = fasthttp.StatusForbidden
|
|
|
|
reason := st.Reason()
|
|
|
|
msg = fmt.Sprintf("%s: %v: %s", message, err, reason)
|
|
|
|
logFields = append(logFields, zap.String("error_detail", reason))
|
|
|
|
case client.IsErrObjectNotFound(err) || client.IsErrContainerNotFound(err):
|
|
|
|
statusCode = fasthttp.StatusNotFound
|
|
|
|
msg = "NotFound"
|
|
|
|
default:
|
|
|
|
statusCode = fasthttp.StatusBadRequest
|
|
|
|
msg = fmt.Sprintf("%s: %v", message, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return statusCode, msg, logFields
|
|
|
|
}
|