error.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. /*
  11. Package util contains utility definitions and functions for the event condition language ECAL.
  12. */
  13. package util
  14. import (
  15. "errors"
  16. "fmt"
  17. "devt.de/krotik/ecal/parser"
  18. )
  19. /*
  20. RuntimeError is a runtime related error.
  21. */
  22. type RuntimeError struct {
  23. Source string // Name of the source which was given to the parser
  24. Type error // Error type (to be used for equal checks)
  25. Detail string // Details of this error
  26. Node *parser.ASTNode // AST Node where the error occurred
  27. Line int // Line of the error
  28. Pos int // Position of the error
  29. }
  30. /*
  31. Runtime related error types.
  32. */
  33. var (
  34. ErrRuntimeError = errors.New("Runtime error")
  35. ErrUnknownConstruct = errors.New("Unknown construct")
  36. ErrInvalidConstruct = errors.New("Invalid construct")
  37. ErrInvalidState = errors.New("Invalid state")
  38. ErrVarAccess = errors.New("Cannot access variable")
  39. ErrNotANumber = errors.New("Operand is not a number")
  40. ErrNotABoolean = errors.New("Operand is not a boolean")
  41. ErrNotAList = errors.New("Operand is not a list")
  42. ErrNotAMap = errors.New("Operand is not a map")
  43. ErrNotAListOrMap = errors.New("Operand is not a list nor a map")
  44. ErrSink = errors.New("Error in sink")
  45. // ErrReturn is not an error. It is used to return when executing a function
  46. ErrReturn = errors.New("*** return ***")
  47. // Error codes for loop operations
  48. ErrIsIterator = errors.New("Function is an iterator")
  49. ErrEndOfIteration = errors.New("End of iteration was reached")
  50. ErrContinueIteration = errors.New("End of iteration step - Continue iteration")
  51. )
  52. /*
  53. NewRuntimeError creates a new RuntimeError object.
  54. */
  55. func NewRuntimeError(source string, t error, d string, node *parser.ASTNode) error {
  56. if node.Token != nil {
  57. return &RuntimeError{source, t, d, node, node.Token.Lline, node.Token.Lpos}
  58. }
  59. return &RuntimeError{source, t, d, node, 0, 0}
  60. }
  61. /*
  62. Error returns a human-readable string representation of this error.
  63. */
  64. func (re *RuntimeError) Error() string {
  65. ret := fmt.Sprintf("ECAL error in %s: %v (%v)", re.Source, re.Type, re.Detail)
  66. if re.Line != 0 {
  67. // Add line if available
  68. ret = fmt.Sprintf("%s (Line:%d Pos:%d)", ret, re.Line, re.Pos)
  69. }
  70. return ret
  71. }