Kernel-Exceptions.st 5.2 KB

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