2
0

Kernel-Exceptions.st 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. Smalltalk current 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. context
  13. <return self.context>
  14. !
  15. jsStack
  16. <return self.stack>
  17. !
  18. messageText
  19. ^messageText
  20. !
  21. messageText: aString
  22. messageText := aString
  23. ! !
  24. !Error methodsFor: 'initialization'!
  25. initialize
  26. self messageText: 'Errorclass: ', (self class name).
  27. ! !
  28. !Error methodsFor: 'signaling'!
  29. resignal
  30. "Resignal the receiver without changing its exception context"
  31. <throw(self)>
  32. !
  33. signal
  34. <self.context = smalltalk.getThisContext(); self.smalltalkError = true; throw(self)>
  35. !
  36. signal: aString
  37. self messageText: aString.
  38. self signal
  39. ! !
  40. !Error methodsFor: 'testing'!
  41. isSmalltalkError
  42. <return self.smalltalkError === true>
  43. ! !
  44. !Error class methodsFor: 'helios'!
  45. heliosClass
  46. ^ 'exception'
  47. ! !
  48. !Error class methodsFor: 'instance creation'!
  49. signal
  50. ^self new signal
  51. !
  52. signal: aString
  53. ^self new
  54. signal: aString
  55. ! !
  56. Error subclass: #JavaScriptException
  57. instanceVariableNames: 'exception'
  58. package: 'Kernel-Exceptions'!
  59. !JavaScriptException commentStamp!
  60. A JavaScriptException is thrown when a non-Smalltalk exception occurs while in the Smalltalk stack.
  61. See `boot.js` `inContext()` and `BlockClosure >> on:do:`!
  62. !JavaScriptException methodsFor: 'accessing'!
  63. context: aMethodContext
  64. "Set the context from the outside.
  65. See boot.js `inContext()` exception handling"
  66. <self.context = aMethodContext>
  67. !
  68. exception
  69. ^ exception
  70. !
  71. exception: anException
  72. exception := anException
  73. !
  74. messageText
  75. <return 'JavaScript exception: ' + self["@exception"].toString()>
  76. ! !
  77. !JavaScriptException class methodsFor: 'instance creation'!
  78. on: anException
  79. ^ self new
  80. exception: anException;
  81. yourself
  82. !
  83. on: anException context: aMethodContext
  84. ^ self new
  85. exception: anException;
  86. context: aMethodContext;
  87. yourself
  88. ! !
  89. Error subclass: #MessageNotUnderstood
  90. instanceVariableNames: 'message receiver'
  91. package: 'Kernel-Exceptions'!
  92. !MessageNotUnderstood commentStamp!
  93. This exception is provided to support `Object>>doesNotUnderstand:`.!
  94. !MessageNotUnderstood methodsFor: 'accessing'!
  95. message
  96. ^message
  97. !
  98. message: aMessage
  99. message := aMessage
  100. !
  101. messageText
  102. ^self receiver asString, ' does not understand #', self message selector
  103. !
  104. receiver
  105. ^receiver
  106. !
  107. receiver: anObject
  108. receiver := anObject
  109. ! !
  110. Error subclass: #NonBooleanReceiver
  111. instanceVariableNames: 'object'
  112. package: 'Kernel-Exceptions'!
  113. !NonBooleanReceiver commentStamp!
  114. NonBooleanReceiver exceptions may be thrown when executing inlined methods such as `#ifTrue:` with a non boolean receiver.!
  115. !NonBooleanReceiver methodsFor: 'accessing'!
  116. object
  117. ^ object
  118. !
  119. object: anObject
  120. object := anObject
  121. ! !
  122. Object subclass: #ErrorHandler
  123. instanceVariableNames: ''
  124. package: 'Kernel-Exceptions'!
  125. !ErrorHandler commentStamp!
  126. I am used to manage Smalltalk errors.
  127. See `boot.js` `handleError()` function.
  128. Subclasses can register themselves as the current handler with
  129. `ErrorHandler class >> register`.
  130. Subclasses may override `#handleError:` to perform an action on the thrown exception.
  131. The default behavior is to log the error and the context stack to the JavaScript console.!
  132. !ErrorHandler methodsFor: 'error handling'!
  133. handleError: anError
  134. anError context ifNotNil: [self logErrorContext: anError context].
  135. self logError: anError
  136. ! !
  137. !ErrorHandler methodsFor: 'private'!
  138. log: aString
  139. console log: aString
  140. !
  141. logContext: aContext
  142. aContext home ifNotNil: [
  143. self logContext: aContext home].
  144. self log: aContext asString
  145. !
  146. logError: anError
  147. self log: anError messageText
  148. !
  149. logErrorContext: aContext
  150. aContext ifNotNil: [
  151. aContext home ifNotNil: [
  152. self logContext: aContext home]]
  153. ! !
  154. ErrorHandler class instanceVariableNames: 'current'!
  155. !ErrorHandler class methodsFor: 'accessing'!
  156. current
  157. ^current ifNil: [current := self new]
  158. !
  159. setCurrent: anHandler
  160. current := anHandler
  161. ! !
  162. !ErrorHandler class methodsFor: 'initialization'!
  163. initialize
  164. self register
  165. !
  166. register
  167. ErrorHandler setCurrent: self new
  168. ! !