Kernel-Exceptions.st 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. Smalltalk createPackage: 'Kernel-Exceptions'!
  2. Object subclass: #Error
  3. slots: {#message. #stack. #amberHandled. #context. #smalltalkError}
  4. package: 'Kernel-Exceptions'!
  5. !Error commentStamp!
  6. From the ANSI standard:
  7. This protocol describes the behavior of instances of class `Error`.
  8. These are used to represent error conditions that prevent the normal continuation of processing.
  9. Actual error exceptions used by an application may be subclasses of this class.
  10. As `Error` is explicitly specified to be subclassable, conforming implementations must implement its behavior in a non-fragile manner.!
  11. !Error methodsFor: 'accessing'!
  12. beHandled
  13. amberHandled := true
  14. !
  15. beSmalltalkError
  16. smalltalkError := true
  17. !
  18. beUnhandled
  19. amberHandled := false
  20. !
  21. context
  22. ^ context
  23. !
  24. context: aMethodContext
  25. context := aMethodContext
  26. !
  27. jsStack
  28. ^ stack
  29. !
  30. messageText
  31. ^ message
  32. !
  33. messageText: aString
  34. message := aString
  35. !
  36. signalerContext
  37. ^ self signalerContextFrom: self context
  38. !
  39. signalerContextFrom: aContext
  40. "Find the first sender of signal(:), the first context which is neither
  41. for an instance method nor for a class side method of Exception (or subclass).
  42. This will make sure that the same context is found for both, `Error signal`
  43. and `Error new signal`"
  44. ^ aContext findContextSuchThat: [ :one |
  45. (one receiver == self
  46. or: [ one receiver == self class ]) not ]
  47. ! !
  48. !Error methodsFor: 'initialization'!
  49. initialize
  50. self messageText: 'Errorclass: ', (self class name).
  51. ! !
  52. !Error methodsFor: 'private'!
  53. basicSignal
  54. <inlineJS: 'throw self;'>
  55. ! !
  56. !Error methodsFor: 'signaling'!
  57. outer
  58. "Pharo compatibility. Just sends #pass."
  59. ^ self pass
  60. !
  61. pass
  62. "Let outer handler take care of this."
  63. self beUnhandled; basicSignal
  64. !
  65. resignal
  66. self deprecatedAPI: 'Use #pass.'.
  67. ^ self pass
  68. !
  69. signal
  70. self beUnhandled; context: thisContext; beSmalltalkError; basicSignal
  71. !
  72. signal: aString
  73. self messageText: aString; signal
  74. ! !
  75. !Error methodsFor: 'testing'!
  76. isSmalltalkError
  77. ^ smalltalkError == true
  78. !
  79. wasHandled
  80. ^ amberHandled == true
  81. ! !
  82. !Error class methodsFor: 'accessing'!
  83. classTag
  84. "Returns a tag or general category for this class.
  85. Typically used to help tools do some reflection.
  86. Helios, for example, uses this to decide what icon the class should display."
  87. ^ 'exception'
  88. ! !
  89. !Error class methodsFor: 'instance creation'!
  90. signal
  91. ^ self new signal
  92. !
  93. signal: aString
  94. ^ self new
  95. signal: aString
  96. ! !
  97. Error subclass: #Halt
  98. slots: {}
  99. package: 'Kernel-Exceptions'!
  100. !Halt commentStamp!
  101. I am provided to support `Object>>#halt`.!
  102. !Halt methodsFor: 'accessing'!
  103. messageText
  104. ^ 'Halt encountered'
  105. !
  106. signalerContextFrom: aContext
  107. "specialized version to find the proper context to open the debugger on.
  108. This will find the first context whose method is no longer on `Halt` or
  109. `Halt class` nor is `#halt` method itself."
  110. ^ aContext findContextSuchThat: [ :one |
  111. (one receiver == self
  112. or: [ (one receiver == self class)
  113. or: [ one method selector = #halt ]]) not ]
  114. ! !
  115. Error subclass: #JavaScriptException
  116. slots: {#exception}
  117. package: 'Kernel-Exceptions'!
  118. !JavaScriptException commentStamp!
  119. A JavaScriptException is thrown when a non-Smalltalk exception occurs while in the Smalltalk stack.
  120. See `boot.js` `inContext()` and `BlockClosure >> on:do:`!
  121. !JavaScriptException methodsFor: 'accessing'!
  122. exception
  123. ^ exception
  124. !
  125. exception: anException
  126. exception := anException
  127. !
  128. messageText
  129. <inlineJS: 'return "JavaScript exception: " + $self.exception.toString()'>
  130. ! !
  131. !JavaScriptException methodsFor: 'error handling'!
  132. wrap
  133. [ self signal ] tryCatch:
  134. [ self shouldBeStubbed ifTrue: [ self context stubToAtMost: 100 ] ]
  135. ! !
  136. !JavaScriptException methodsFor: 'testing'!
  137. shouldBeStubbed
  138. <inlineJS: 'return $self.exception instanceof RangeError'>
  139. ! !
  140. !JavaScriptException class methodsFor: 'instance creation'!
  141. on: anException
  142. ^ self new
  143. exception: anException;
  144. yourself
  145. !
  146. on: anException context: aMethodContext
  147. ^ self new
  148. exception: anException;
  149. context: aMethodContext;
  150. yourself
  151. ! !
  152. Error subclass: #MessageNotUnderstood
  153. slots: {#smalltalkMessage. #receiver}
  154. package: 'Kernel-Exceptions'!
  155. !MessageNotUnderstood commentStamp!
  156. This exception is provided to support `Object>>doesNotUnderstand:`.!
  157. !MessageNotUnderstood methodsFor: 'accessing'!
  158. message
  159. ^ smalltalkMessage
  160. !
  161. message: aMessage
  162. smalltalkMessage := aMessage
  163. !
  164. messageText
  165. ^ self receiver asString, ' does not understand #', self message selector
  166. !
  167. receiver
  168. ^ receiver
  169. !
  170. receiver: anObject
  171. receiver := anObject
  172. ! !
  173. Error subclass: #NonBooleanReceiver
  174. slots: {#object}
  175. package: 'Kernel-Exceptions'!
  176. !NonBooleanReceiver commentStamp!
  177. NonBooleanReceiver exceptions may be thrown when executing inlined methods such as `#ifTrue:` with a non boolean receiver.!
  178. !NonBooleanReceiver methodsFor: 'accessing'!
  179. object
  180. ^ object
  181. !
  182. object: anObject
  183. object := anObject
  184. ! !
  185. !NonBooleanReceiver class methodsFor: 'instance creation'!
  186. signalOn: anObject
  187. ^ self new
  188. object: anObject;
  189. signal
  190. ! !