error.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "go/ast"
  17. "go/token"
  18. )
  19. const (
  20. errNotDirectCall = "Opts for STABLE metric was not directly passed to new metric function"
  21. errPositionalArguments = "Positional arguments are not supported"
  22. errStabilityLevel = "StabilityLevel should be passed STABLE, ALPHA or removed"
  23. errStableSummary = "Stable summary metric is not supported"
  24. errInvalidNewMetricCall = "Invalid new metric call, please ensure code compiles"
  25. errNonStringAttribute = "Non string attribute it not supported"
  26. errBadVariableAttribute = "Metric attribute was not correctly set. Please use only global consts in same file"
  27. errFieldNotSupported = "Field %s is not supported"
  28. errBuckets = "Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"
  29. errLabels = "Labels were not set to list of strings"
  30. errImport = `Importing using "." is not supported`
  31. )
  32. type decodeError struct {
  33. msg string
  34. pos token.Pos
  35. }
  36. func newDecodeErrorf(node ast.Node, format string, a ...interface{}) *decodeError {
  37. return &decodeError{
  38. msg: fmt.Sprintf(format, a...),
  39. pos: node.Pos(),
  40. }
  41. }
  42. var _ error = (*decodeError)(nil)
  43. func (e decodeError) Error() string {
  44. return e.msg
  45. }
  46. func (e decodeError) errorWithFileInformation(fileset *token.FileSet) error {
  47. position := fileset.Position(e.pos)
  48. return fmt.Errorf("%s:%d:%d: %s", position.Filename, position.Line, position.Column, e.msg)
  49. }