1
0

Kernel-Exceptions.st 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. isError
  77. ^ true
  78. !
  79. isSmalltalkError
  80. ^ smalltalkError == true
  81. !
  82. wasHandled
  83. ^ amberHandled == true
  84. ! !
  85. !Error class methodsFor: 'accessing'!
  86. classTag
  87. "Returns a tag or general category for this class.
  88. Typically used to help tools do some reflection.
  89. Helios, for example, uses this to decide what icon the class should display."
  90. ^ 'exception'
  91. ! !
  92. !Error class methodsFor: 'instance creation'!
  93. signal
  94. ^ self new signal
  95. !
  96. signal: aString
  97. ^ self new
  98. signal: aString
  99. ! !
  100. Error subclass: #Halt
  101. slots: {}
  102. package: 'Kernel-Exceptions'!
  103. !Halt commentStamp!
  104. I am provided to support `Object>>#halt`.!
  105. !Halt methodsFor: 'accessing'!
  106. messageText
  107. ^ 'Halt encountered'
  108. !
  109. signalerContextFrom: aContext
  110. "specialized version to find the proper context to open the debugger on.
  111. This will find the first context whose method is no longer on `Halt` or
  112. `Halt class` nor is `#halt` method itself."
  113. ^ aContext findContextSuchThat: [ :one |
  114. (one receiver == self
  115. or: [ (one receiver == self class)
  116. or: [ one method selector = #halt ]]) not ]
  117. ! !
  118. Error subclass: #JavaScriptException
  119. slots: {#exception}
  120. package: 'Kernel-Exceptions'!
  121. !JavaScriptException commentStamp!
  122. A JavaScriptException is thrown when a non-Smalltalk exception occurs while in the Smalltalk stack.
  123. See `boot.js` `inContext()` and `BlockClosure >> on:do:`!
  124. !JavaScriptException methodsFor: 'accessing'!
  125. exception
  126. ^ exception
  127. !
  128. exception: anException
  129. exception := anException
  130. !
  131. messageText
  132. <inlineJS: 'return "JavaScript exception: " + $self.exception.toString()'>
  133. ! !
  134. !JavaScriptException methodsFor: 'error handling'!
  135. wrap
  136. [ self signal ] tryCatch:
  137. [ self shouldBeStubbed ifTrue: [ self context stubToAtMost: 100 ] ]
  138. ! !
  139. !JavaScriptException methodsFor: 'testing'!
  140. shouldBeStubbed
  141. <inlineJS: 'return $self.exception instanceof RangeError'>
  142. ! !
  143. !JavaScriptException class methodsFor: 'instance creation'!
  144. on: anException
  145. ^ self new
  146. exception: anException;
  147. yourself
  148. !
  149. on: anException context: aMethodContext
  150. ^ self new
  151. exception: anException;
  152. context: aMethodContext;
  153. yourself
  154. ! !
  155. Error subclass: #MessageNotUnderstood
  156. slots: {#smalltalkMessage. #receiver}
  157. package: 'Kernel-Exceptions'!
  158. !MessageNotUnderstood commentStamp!
  159. This exception is provided to support `Object>>doesNotUnderstand:`.!
  160. !MessageNotUnderstood methodsFor: 'accessing'!
  161. message
  162. ^ smalltalkMessage
  163. !
  164. message: aMessage
  165. smalltalkMessage := aMessage
  166. !
  167. messageText
  168. ^ self receiver asString, ' does not understand #', self message selector
  169. !
  170. receiver
  171. ^ receiver
  172. !
  173. receiver: anObject
  174. receiver := anObject
  175. ! !
  176. Error subclass: #NonBooleanReceiver
  177. slots: {#object}
  178. package: 'Kernel-Exceptions'!
  179. !NonBooleanReceiver commentStamp!
  180. NonBooleanReceiver exceptions may be thrown when executing inlined methods such as `#ifTrue:` with a non boolean receiver.!
  181. !NonBooleanReceiver methodsFor: 'accessing'!
  182. object
  183. ^ object
  184. !
  185. object: anObject
  186. object := anObject
  187. ! !
  188. !NonBooleanReceiver class methodsFor: 'instance creation'!
  189. signalOn: anObject
  190. ^ self new
  191. object: anObject;
  192. signal
  193. ! !