Kernel-Exceptions.st 4.4 KB

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