1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package errtype provides a number of concrete types which are used by the 16 // cloudsqlconn package. 17 package errtype 18 19 import "fmt" 20 21 type genericError struct { 22 Message string 23 ConnName string 24 } 25 26 func (e *genericError) Error() string { 27 return fmt.Sprintf("%v (connection name = %q)", e.Message, e.ConnName) 28 } 29 30 // NewConfigError initializes a ConfigError. 31 func NewConfigError(msg, cn string) *ConfigError { 32 return &ConfigError{ 33 genericError: &genericError{Message: "Config error: " + msg, ConnName: cn}, 34 } 35 } 36 37 // ConfigError represents an incorrect request by the user. Config errors 38 // usually indicate a semantic error (e.g., the instance connection name is 39 // malformed, the SQL instance does not support the requested IP type, etc.) 40 // ConfigError's should not be retried. 41 type ConfigError struct{ *genericError } 42 43 // NewRefreshError initializes a RefreshError. 44 func NewRefreshError(msg, cn string, err error) *RefreshError { 45 return &RefreshError{ 46 genericError: &genericError{Message: msg, ConnName: cn}, 47 Err: err, 48 } 49 } 50 51 // RefreshError means that an error occurred during the background 52 // refresh operation. In general, this is an unexpected error caused by 53 // an interaction with the API itself (e.g., missing certificates, 54 // invalid certificate encoding, region mismatch with the requested 55 // instance connection name, etc.). RefreshError's usually can be retried. 56 type RefreshError struct { 57 *genericError 58 // Err is the underlying error and may be nil. 59 Err error 60 } 61 62 func (e *RefreshError) Error() string { 63 if e.Err == nil { 64 return fmt.Sprintf("Refresh error: %v", e.genericError) 65 } 66 return fmt.Sprintf("Refresh error: %v: %v", e.genericError, e.Err) 67 } 68 69 func (e *RefreshError) Unwrap() error { return e.Err } 70 71 // NewDialError initializes a DialError. 72 func NewDialError(msg, cn string, err error) *DialError { 73 return &DialError{ 74 genericError: &genericError{Message: msg, ConnName: cn}, 75 Err: err, 76 } 77 } 78 79 // DialError represents a problem that occurred when trying to dial a SQL 80 // instance (e.g., a failure to set the keep-alive property, a TLS handshake 81 // failure, a missing certificate, etc.). DialError's are often network-related 82 // and can be retried. 83 type DialError struct { 84 *genericError 85 // Err is the underlying error and may be nil. 86 Err error 87 } 88 89 func (e *DialError) Error() string { 90 if e.Err == nil { 91 return fmt.Sprintf("Dial error: %v", e.genericError) 92 } 93 return fmt.Sprintf("Dial error: %v: %v", e.genericError, e.Err) 94 } 95 96 func (e *DialError) Unwrap() error { return e.Err } 97