-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy patherrors.go
155 lines (131 loc) · 4.94 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright 2020 Cortex Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package aws
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/cortexlabs/cortex/pkg/lib/errors"
s "github.com/cortexlabs/cortex/pkg/lib/strings"
)
const (
ErrInvalidAWSCredentials = "aws.invalid_aws_credentials"
ErrInvalidS3aPath = "aws.invalid_s3a_path"
ErrInvalidS3Path = "aws.invalid_s3_path"
ErrUnexpectedMissingCredentials = "aws.unexpected_missing_credentials"
ErrAuth = "aws.auth"
ErrBucketInaccessible = "aws.bucket_inaccessible"
ErrBucketNotFound = "aws.bucket_not_found"
ErrInstanceTypeLimitIsZero = "aws.instance_type_limit_is_zero"
ErrNoValidSpotPrices = "aws.no_valid_spot_prices"
ErrReadCredentials = "aws.read_credentials"
ErrECRExtractingCredentials = "aws.ecr_failed_credentials"
)
func IsNotFoundErr(err error) bool {
return IsErrCode(err, "NotFound")
}
func IsNoSuchKeyErr(err error) bool {
return IsErrCode(err, s3.ErrCodeNoSuchKey)
}
func IsNoSuchBucketErr(err error) bool {
return IsErrCode(err, s3.ErrCodeNoSuchBucket)
}
func IsGenericNotFoundErr(err error) bool {
return IsNotFoundErr(err) || IsNoSuchKeyErr(err) || IsNoSuchBucketErr(err)
}
func IsErrCode(err error, errorCode string) bool {
awsErr, ok := errors.CauseOrSelf(err).(awserr.Error)
if !ok {
return false
}
if awsErr.Code() == errorCode {
return true
}
return false
}
func ErrorInvalidAWSCredentials(awsErr error) error {
awsErrMsg := errors.MessageFirstLine(awsErr)
return errors.WithStack(&errors.Error{
Kind: ErrInvalidAWSCredentials,
Message: "invalid AWS credentials\n" + awsErrMsg,
Cause: awsErr,
})
}
func ErrorInvalidS3aPath(provided string) error {
return errors.WithStack(&errors.Error{
Kind: ErrInvalidS3aPath,
Message: fmt.Sprintf("%s is not a valid s3a path (e.g. s3a://cortex-examples/pytorch/iris-classifier/weights.pth is a valid s3a path)", s.UserStr(provided)),
})
}
func ErrorInvalidS3Path(provided string) error {
return errors.WithStack(&errors.Error{
Kind: ErrInvalidS3Path,
Message: fmt.Sprintf("%s is not a valid s3 path (e.g. s3://cortex-examples/pytorch/iris-classifier/weights.pth is a valid s3 path)", s.UserStr(provided)),
})
}
func ErrorUnexpectedMissingCredentials(awsAccessKeyID *string, awsSecretAccessKey *string) error {
var msg string
if awsAccessKeyID == nil && awsSecretAccessKey == nil {
msg = "aws access key id and aws secret access key are missing"
} else if awsAccessKeyID == nil {
msg = "aws access key id is missing"
} else if awsSecretAccessKey == nil {
msg = "aws secret access key is missing"
}
return errors.WithStack(&errors.Error{
Kind: ErrUnexpectedMissingCredentials,
Message: msg,
})
}
func ErrorAuth() error {
return errors.WithStack(&errors.Error{
Kind: ErrAuth,
Message: "unable to authenticate with AWS",
})
}
func ErrorBucketInaccessible(bucket string) error {
return errors.WithStack(&errors.Error{
Kind: ErrBucketInaccessible,
Message: fmt.Sprintf("bucket \"%s\" is not accessible with the specified AWS credentials", bucket),
})
}
func ErrorBucketNotFound(bucket string) error {
return errors.WithStack(&errors.Error{
Kind: ErrBucketNotFound,
Message: fmt.Sprintf("bucket \"%s\" not found", bucket),
})
}
func ErrorInstanceTypeLimitIsZero(instanceType string, region string) error {
return errors.WithStack(&errors.Error{
Kind: ErrInstanceTypeLimitIsZero,
Message: fmt.Sprintf(`you don't have access to %s instances in %s; please request access in the appropriate region (https://console.aws.amazon.com/support/cases#/create?issueType=service-limit-increase&limitType=ec2-instances). If you submitted a request and it was recently approved, please allow ~30 minutes for AWS to reflect this change."`, instanceType, region),
})
}
func ErrorNoValidSpotPrices(instanceType string, region string) error {
return errors.WithStack(&errors.Error{
Kind: ErrNoValidSpotPrices,
Message: fmt.Sprintf("no spot prices were found for %s instances in %s", instanceType, region),
})
}
func ErrorReadCredentials() error {
return errors.WithStack(&errors.Error{
Kind: ErrReadCredentials,
Message: "unable to read AWS credentials from credentials file",
})
}
func ErrorECRExtractingCredentials() error {
return errors.WithStack(&errors.Error{
Kind: ErrECRExtractingCredentials,
Message: "unable to extract ECR credentials",
})
}