Kernel-Exceptions.st 4.4 KB

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