Serge Bazanski | cc25bdf | 2018-10-25 14:02:58 +0200 | [diff] [blame] | 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 "fmt" |
| 18 | |
| 19 | // ParseError respresents a parsing error |
| 20 | type ParseError struct { |
| 21 | code int32 |
| 22 | Name string |
| 23 | In string |
| 24 | Value string |
| 25 | Reason error |
| 26 | message string |
| 27 | } |
| 28 | |
| 29 | func (e *ParseError) Error() string { |
| 30 | return e.message |
| 31 | } |
| 32 | |
| 33 | // Code returns the http status code for this error |
| 34 | func (e *ParseError) Code() int32 { |
| 35 | return e.code |
| 36 | } |
| 37 | |
| 38 | const ( |
| 39 | parseErrorTemplContent = `parsing %s %s from %q failed, because %s` |
| 40 | parseErrorTemplContentNoIn = `parsing %s from %q failed, because %s` |
| 41 | ) |
| 42 | |
| 43 | // NewParseError creates a new parse error |
| 44 | func NewParseError(name, in, value string, reason error) *ParseError { |
| 45 | var msg string |
| 46 | if in == "" { |
| 47 | msg = fmt.Sprintf(parseErrorTemplContentNoIn, name, value, reason) |
| 48 | } else { |
| 49 | msg = fmt.Sprintf(parseErrorTemplContent, name, in, value, reason) |
| 50 | } |
| 51 | return &ParseError{ |
| 52 | code: 400, |
| 53 | Name: name, |
| 54 | In: in, |
| 55 | Value: value, |
| 56 | Reason: reason, |
| 57 | message: msg, |
| 58 | } |
| 59 | } |