/library/error/error_handler.e

http://github.com/jocelyn/EiffelWebReloaded · Specman e · 161 lines · 123 code · 28 blank · 10 comment · 5 complexity · a3491dffee795ad1459a85d82051fcb3 MD5 · raw file

  1. note
  2. description : "Objects that handle error..."
  3. legal: "See notice at end of class."
  4. status: "See notice at end of class."
  5. date: "$Date$"
  6. revision: "$Revision$"
  7. class
  8. ERROR_HANDLER
  9. inherit
  10. ANY
  11. DEBUG_OUTPUT
  12. create
  13. make
  14. feature {NONE} -- Initialization
  15. make
  16. -- Initialize `Current'.
  17. do
  18. create {ARRAYED_LIST [ERROR]} errors.make (3)
  19. create error_added_actions
  20. end
  21. feature -- Status
  22. has_error: BOOLEAN
  23. -- Has error?
  24. do
  25. Result := count > 0
  26. end
  27. count: INTEGER
  28. do
  29. Result := errors.count
  30. end
  31. feature {ERROR_HANDLER, ERROR_VISITOR} -- Restricted access
  32. errors: LIST [ERROR]
  33. -- Errors container
  34. feature -- Status report
  35. debug_output: STRING
  36. -- String that should be displayed in debugger to represent `Current'.
  37. do
  38. if has_error then
  39. Result := count.out + " errors"
  40. else
  41. Result := "no error"
  42. end
  43. end
  44. feature -- Events
  45. error_added_actions: ACTION_SEQUENCE [TUPLE [ERROR]]
  46. -- Actions triggered when a new error is added
  47. feature {NONE} -- Event: implementation
  48. on_error_added (e: ERROR)
  49. -- Error `e' was just added
  50. do
  51. error_added_actions.call ([e])
  52. end
  53. feature -- Basic operation
  54. add_error (a_error: ERROR)
  55. -- Add `a_error' to the stack of error
  56. do
  57. errors.force (a_error)
  58. on_error_added (a_error)
  59. end
  60. add_error_details, add_custom_error (a_code: INTEGER; a_name: STRING; a_message: detachable STRING_32)
  61. -- Add custom error to the stack of error
  62. local
  63. e: ERROR_CUSTOM
  64. do
  65. create e.make (a_code, a_name, a_message)
  66. add_error (e)
  67. end
  68. append (other: ERROR_HANDLER)
  69. -- Append errors from `a_err_handler'
  70. local
  71. other_errs: LIST [ERROR]
  72. do
  73. other_errs := other.errors
  74. if other_errs.count > 0 then
  75. from
  76. other_errs.start
  77. until
  78. other_errs.after
  79. loop
  80. add_error (other_errs.item)
  81. other_errs.forth
  82. end
  83. end
  84. ensure
  85. other_error_appended: other.has_error implies has_error
  86. new_count: count = old count + other.count
  87. end
  88. feature -- Access
  89. as_single_error: detachable ERROR
  90. do
  91. if count > 1 then
  92. create {ERROR_GROUP} Result.make (errors)
  93. elseif count > 0 then
  94. Result := errors.first
  95. end
  96. ensure
  97. has_error_implies_result_attached: has_error implies Result /= Void
  98. end
  99. as_string_representation: STRING
  100. require
  101. has_error
  102. do
  103. if attached as_single_error as e then
  104. Result := e.string_representation
  105. else
  106. check has_error: False end
  107. Result := "Error occured"
  108. end
  109. end
  110. feature -- Element changes
  111. concatenate
  112. -- Concatenate into a single error if any
  113. do
  114. if count > 1 and then attached as_single_error as e then
  115. wipe_out
  116. errors.force (e)
  117. end
  118. end
  119. reset, wipe_out
  120. do
  121. errors.wipe_out
  122. end
  123. note
  124. copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
  125. license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
  126. source: "[
  127. Eiffel Software
  128. 5949 Hollister Ave., Goleta, CA 93117 USA
  129. Telephone 805-685-1006, Fax 805-685-6869
  130. Website http://www.eiffel.com
  131. Customer support http://support.eiffel.com
  132. ]"
  133. end