| 1 | // Copyright 2015 go-swagger maintainers |
| 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 | // http://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 errors |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | ) |
| 20 | |
| 21 | // Error represents a error interface all swagger framework errors implement |
| 22 | type Error interface { |
| 23 | error |
| 24 | Code() int32 |
| 25 | } |
| 26 | |
| 27 | type apiError struct { |
| 28 | code int32 |
| 29 | message string |
| 30 | } |
| 31 | |
| 32 | func (a *apiError) Error() string { |
| 33 | return a.message |
| 34 | } |
| 35 | |
| 36 | func (a *apiError) Code() int32 { |
| 37 | return a.code |
| 38 | } |
| 39 | |
| 40 | // New creates a new API error with a code and a message |
| 41 | func New(code int32, message string, args ...interface{}) Error { |
| 42 | if len(args) > 0 { |
| 43 | return &apiError{code, fmt.Sprintf(message, args...)} |
| 44 | } |
| 45 | return &apiError{code, message} |
| 46 | } |