1
0

Kernel-Exceptions.st 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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: #MessageNotUnderstood
  49. instanceVariableNames: 'message receiver'
  50. package: 'Kernel-Exceptions'!
  51. !MessageNotUnderstood commentStamp!
  52. This exception is provided to support `Object>>doesNotUnderstand:`.!
  53. !MessageNotUnderstood methodsFor: 'accessing'!
  54. message
  55. ^message
  56. !
  57. message: aMessage
  58. message := aMessage
  59. !
  60. messageText
  61. ^self receiver asString, ' does not understand #', self message selector
  62. !
  63. receiver
  64. ^receiver
  65. !
  66. receiver: anObject
  67. receiver := anObject
  68. ! !
  69. Error subclass: #NonBooleanReceiver
  70. instanceVariableNames: 'object'
  71. package: 'Kernel-Exceptions'!
  72. !NonBooleanReceiver commentStamp!
  73. NonBooleanReceiver exceptions may be thrown when executing inlined methods such as `#ifTrue:` with a non boolean receiver.!
  74. !NonBooleanReceiver methodsFor: 'accessing'!
  75. object
  76. ^ object
  77. !
  78. object: anObject
  79. object := anObject
  80. ! !
  81. Object subclass: #ErrorHandler
  82. instanceVariableNames: ''
  83. package: 'Kernel-Exceptions'!
  84. !ErrorHandler commentStamp!
  85. ErrorHandler is used to manage Smalltalk errors.
  86. See `boot.js` `handleError()` function.
  87. Subclasses of `ErrorHandler` can register themselves as the current handler with
  88. `ErrorHandler class >> register`.
  89. Subclasses may override `#handleError:` to perform an action on the thrown exception.
  90. The default behavior is to log the error and the context stack to the JavaScript console.!
  91. !ErrorHandler methodsFor: 'error handling'!
  92. handleError: anError
  93. anError context ifNotNil: [self logErrorContext: anError context].
  94. self logError: anError
  95. ! !
  96. !ErrorHandler methodsFor: 'private'!
  97. log: aString
  98. console log: aString
  99. !
  100. logContext: aContext
  101. aContext home ifNotNil: [
  102. self logContext: aContext home].
  103. self log: aContext receiver asString, '>>', aContext selector asString
  104. !
  105. logError: anError
  106. self log: anError messageText
  107. !
  108. logErrorContext: aContext
  109. aContext ifNotNil: [
  110. aContext home ifNotNil: [
  111. self logContext: aContext home]]
  112. ! !
  113. ErrorHandler class instanceVariableNames: 'current'!
  114. !ErrorHandler class methodsFor: 'accessing'!
  115. current
  116. ^current ifNil: [current := self new]
  117. !
  118. setCurrent: anHandler
  119. current := anHandler
  120. ! !
  121. !ErrorHandler class methodsFor: 'initialization'!
  122. initialize
  123. self register
  124. !
  125. register
  126. ErrorHandler setCurrent: self new
  127. ! !