| 1 | package validation |
| 2 | |
| 3 | // Copyright 2017 Microsoft Corporation |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | ) |
| 20 | |
| 21 | // Error is the type that's returned when the validation of an APIs arguments constraints fails. |
| 22 | type Error struct { |
| 23 | // PackageType is the package type of the object emitting the error. For types, the value |
| 24 | // matches that produced the the '%T' format specifier of the fmt package. For other elements, |
| 25 | // such as functions, it is just the package name (e.g., "autorest"). |
| 26 | PackageType string |
| 27 | |
| 28 | // Method is the name of the method raising the error. |
| 29 | Method string |
| 30 | |
| 31 | // Message is the error message. |
| 32 | Message string |
| 33 | } |
| 34 | |
| 35 | // Error returns a string containing the details of the validation failure. |
| 36 | func (e Error) Error() string { |
| 37 | return fmt.Sprintf("%s#%s: Invalid input: %s", e.PackageType, e.Method, e.Message) |
| 38 | } |
| 39 | |
| 40 | // NewError creates a new Error object with the specified parameters. |
| 41 | // message is treated as a format string to which the optional args apply. |
| 42 | func NewError(packageType string, method string, message string, args ...interface{}) Error { |
| 43 | return Error{ |
| 44 | PackageType: packageType, |
| 45 | Method: method, |
| 46 | Message: fmt.Sprintf(message, args...), |
| 47 | } |
| 48 | } |