Skip to content

Commit b3602df

Browse files
committed
Better error handling for setup CORS check
1 parent a7c4273 commit b3602df

File tree

2 files changed

+29
-8
lines changed
  • internal

2 files changed

+29
-8
lines changed

internal/configuration/setup/Setup.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ func handleTestAws(w http.ResponseWriter, r *http.Request) {
869869
}
870870
ok, err := aws.IsValidLogin(awsConfig)
871871
if err != nil {
872-
handleAwsError(w, err, "Unable to login. ")
872+
handleAwsError(w, err, awsOperationLogin)
873873
return
874874
}
875875
if !ok {
@@ -880,7 +880,7 @@ func handleTestAws(w http.ResponseWriter, r *http.Request) {
880880
ok, err = aws.IsCorsCorrectlySet(awsConfig.Bucket, t.GokapiUrl)
881881
aws.LogOut()
882882
if err != nil {
883-
handleAwsError(w, err, "Could not get CORS settings. ")
883+
handleAwsError(w, err, awsOperationCors)
884884
return
885885
}
886886
if !ok {
@@ -890,11 +890,24 @@ func handleTestAws(w http.ResponseWriter, r *http.Request) {
890890
_, _ = w.Write([]byte("All tests OK."))
891891
}
892892

893-
func handleAwsError(w http.ResponseWriter, err error, prefix string) {
893+
const (
894+
awsOperationLogin = iota
895+
awsOperationCors
896+
)
897+
898+
func handleAwsError(w http.ResponseWriter, err error, operation int) {
894899
var awsErr awserr.Error
895900
isAwsErr := errors.As(err, &awsErr)
901+
var prefix string
902+
switch operation {
903+
case awsOperationLogin:
904+
prefix = "Unable to login. "
905+
case awsOperationCors:
906+
prefix = "Could not get CORS settings. "
907+
}
896908
if isAwsErr {
897-
switch awsErr.Code() {
909+
code := awsErr.Code()
910+
switch code {
898911
case s3.ErrCodeNoSuchBucket:
899912
_, _ = w.Write([]byte("Invalid bucket or regions provided, bucket does not exist."))
900913
case "Forbidden":
@@ -903,6 +916,13 @@ func handleAwsError(w http.ResponseWriter, err error, prefix string) {
903916
_, _ = w.Write([]byte("Unable to connect to server, check endpoint."))
904917
case "SerializationError":
905918
_, _ = w.Write([]byte("Invalid response received by server, check endpoint."))
919+
case "NotFound":
920+
if operation == awsOperationCors {
921+
_, _ = w.Write([]byte("Login OK, but could not check bucket CORS settings."))
922+
923+
} else {
924+
_, _ = w.Write([]byte("The requested resource could not be found, check endpoint"))
925+
}
906926
default:
907927
_, _ = w.Write([]byte(prefix + "Error " + awsErr.Code() + ": " + err.Error()))
908928
}

internal/storage/filesystem/s3filesystem/aws/Aws.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ func fileExists(bucket, filename string) (bool, int64, error) {
267267
if aerr.Code() == "NotFound" {
268268
return false, 0, nil
269269
}
270-
}
271-
if aerr.Code() == request.CanceledErrorCode {
272-
return false, 0, errors.New("Timeout - could not connect to " + *svc.Config.Endpoint)
270+
if aerr.Code() == request.CanceledErrorCode {
271+
return false, 0, errors.New("Timeout - could not connect to " + *svc.Config.Endpoint)
272+
}
273273
}
274274
return false, 0, err
275275
}
@@ -310,7 +310,8 @@ func IsCorsCorrectlySet(bucket, gokapiUrl string) (bool, error) {
310310
if err != nil {
311311
var aerr awserr.Error
312312
ok := errors.As(err, &aerr)
313-
if ok && aerr.Code() == "NoSuchCorsConfiguration" {
313+
if ok && (aerr.Code() == "NoSuchCORSConfiguration" ||
314+
aerr.Code() == "NoSuchCorsConfiguration") {
314315
return false, nil
315316
}
316317
return false, err

0 commit comments

Comments
 (0)