Skip to content

Commit

Permalink
Throw a better error when no creds are found (#286)
Browse files Browse the repository at this point in the history
* Throw a better error when no creds are found

* Updated comments for throwing err

* Optionally default to throwing NoCredentials error

* Update src/AWSCredentials.jl

Co-authored-by: Curtis Vogt <[email protected]>

Co-authored-by: Curtis Vogt <[email protected]>
  • Loading branch information
mattBrzezinski and omus authored Feb 22, 2021
1 parent 9cd64f4 commit 29a8331
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/AWSCredentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ Checks credential locations in the order:
# Throws
- `error("Can't find AWS Credentials")`: AWSCredentials could not be found
"""
function AWSCredentials(; profile=nothing)
function AWSCredentials(; profile=nothing, throw_cred_error=true)
creds = nothing
credential_function = () -> nothing

if profile === nothing
profile = get(ENV, "AWS_PROFILE", get(ENV, "AWS_DEFAULT_PROFILE", nothing))
end

# Define our search options, expected to be callable with no arguments. Should return
# `nothing` when credentials are not able to be located
# Define our search options, expected to be callable with no arguments.
# Throw NoCredentials if none are found
functions = [
env_var_credentials,
() -> dot_aws_credentials(profile),
Expand All @@ -164,6 +164,15 @@ function AWSCredentials(; profile=nothing)
creds === nothing || break
end

# If credentials are nothing, default to throwing an error, otherwise return nothing
if creds === nothing
if throw_cred_error
throw(NoCredentials("Can't find AWS credentials!"))
else
return nothing
end
end

creds.renew = credential_function

return creds
Expand Down
2 changes: 1 addition & 1 deletion test/AWSCredentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ end
delete!(ENV, "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI")

apply([_http_request_patch, Patches._cred_file_patch, Patches._config_file_patch]) do
@test_throws ErrorException AWSConfig()
@test_throws NoCredentials AWSConfig()
end
finally
if !isempty(old_aws_access_key_id)
Expand Down

0 comments on commit 29a8331

Please sign in to comment.