Kernel-Exceptions.st 4.4 KB

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