You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parsing.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. package errors
  15. import "fmt"
  16. // ParseError respresents a parsing error
  17. type ParseError struct {
  18. code int32
  19. Name string
  20. In string
  21. Value string
  22. Reason error
  23. message string
  24. }
  25. func (e *ParseError) Error() string {
  26. return e.message
  27. }
  28. // Code returns the http status code for this error
  29. func (e *ParseError) Code() int32 {
  30. return e.code
  31. }
  32. const (
  33. parseErrorTemplContent = `parsing %s %s from %q failed, because %s`
  34. parseErrorTemplContentNoIn = `parsing %s from %q failed, because %s`
  35. )
  36. // NewParseError creates a new parse error
  37. func NewParseError(name, in, value string, reason error) *ParseError {
  38. var msg string
  39. if in == "" {
  40. msg = fmt.Sprintf(parseErrorTemplContentNoIn, name, value, reason)
  41. } else {
  42. msg = fmt.Sprintf(parseErrorTemplContent, name, in, value, reason)
  43. }
  44. return &ParseError{
  45. code: 400,
  46. Name: name,
  47. In: in,
  48. Value: value,
  49. Reason: reason,
  50. message: msg,
  51. }
  52. }