Skip to content

Commit f4899b5

Browse files
committed
trim response body for 403 error
1 parent 3c4a88a commit f4899b5

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

errors.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package golangsdk
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"strings"
6+
)
47

58
// BaseError is an error type that all other error types embed.
69
type BaseError struct {
@@ -118,9 +121,21 @@ func (e ErrDefault401) Error() string {
118121
return "Authentication failed"
119122
}
120123
func (e ErrDefault403) Error() string {
124+
var maxLength int = 200
125+
var unAuthorized string = "Request not authorized"
126+
127+
messageBody := string(e.Body)
128+
if len(messageBody) > maxLength {
129+
if strings.Contains(messageBody, unAuthorized) {
130+
messageBody = unAuthorized
131+
} else {
132+
messageBody = messageBody[:maxLength] + "\n..."
133+
}
134+
}
135+
121136
e.DefaultErrString = fmt.Sprintf(
122137
"Action forbidden: [%s %s], error message: %s",
123-
e.Method, e.URL, e.Body,
138+
e.Method, e.URL, messageBody,
124139
)
125140
return e.choseErrString()
126141
}

0 commit comments

Comments
 (0)