error.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // ErrReturn is not an error. It is used to return when executing a function
  45. ErrReturn = errors.New("*** return ***")
  46. // Error codes for loop operations
  47. ErrIsIterator = errors.New("Function is an iterator")
  48. ErrEndOfIteration = errors.New("End of iteration was reached")
  49. ErrContinueIteration = errors.New("End of iteration step - Continue iteration")
  50. )
  51. /*
  52. NewRuntimeError creates a new RuntimeError object.
  53. */
  54. func NewRuntimeError(source string, t error, d string, node *parser.ASTNode) error {
  55. if node.Token != nil {
  56. return &RuntimeError{source, t, d, node, node.Token.Lline, node.Token.Lpos}
  57. }
  58. return &RuntimeError{source, t, d, node, 0, 0}
  59. }
  60. /*
  61. Error returns a human-readable string representation of this error.
  62. */
  63. func (re *RuntimeError) Error() string {
  64. ret := fmt.Sprintf("ECAL error in %s: %v (%v)", re.Source, re.Type, re.Detail)
  65. if re.Line != 0 {
  66. // Add line if available
  67. ret = fmt.Sprintf("%s (Line:%d Pos:%d)", ret, re.Line, re.Pos)
  68. }
  69. return ret
  70. }