PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/topaz/types/error.topaz

https://github.com/KoreanFoodComics/Project-SnowBall
Unknown | 174 lines | 168 code | 6 blank | 0 comment | 0 complexity | 1cfa2aa6efc2bb192051170377c7f33c MD5 | raw file
  1. Topaz [
  2. Title: "Topaz types: ERROR!"
  3. Author: "Gabriele Santilli"
  4. Copyright: 2011
  5. Type: Fake-Topaz
  6. ; License: {
  7. ; Permission is hereby granted, free of charge, to any person obtaining
  8. ; a copy of this software and associated documentation files
  9. ; (the "Software"), to deal in the Software without restriction, including
  10. ; without limitation the rights to use, copy, modify, merge, publish,
  11. ; distribute, sublicense, and/or sell copies of the Software, and to
  12. ; permit persons to whom the Software is furnished to do so, subject
  13. ; to the following conditions:
  14. ; The above copyright notice and this permission notice shall be included
  15. ; in all copies or substantial portions of the Software.
  16. ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. ; OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. ; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. ; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. ; ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. ; OTHER DEALINGS IN THE SOFTWARE.
  23. ; }
  24. ]
  25. ; ===== TYPES: ERROR! =========================================================
  26. make-type 'error! [
  27. make: function [args] [res] [
  28. if not args [args: make-struct []]
  29. res: make-struct [
  30. type: error!
  31. category: any [args/category "Internal"]
  32. id: any [args/id "unspecified"]
  33. message: any [args/message "Unspecified error"]
  34. args: args/args
  35. stack: make block! none
  36. ]
  37. if args/stack [insert/only res/stack args/stack]
  38. res
  39. ]
  40. topaz-make: function [value] [blk] [
  41. switch-default value/type/name [
  42. "block!" [
  43. blk: make block! none
  44. append blk make none! none
  45. insert/only tail blk value
  46. topaz-make-error apply object!/topaz-make [blk]
  47. ]
  48. "object!" [
  49. topaz-make-error value
  50. ]
  51. "error!" [value]
  52. ] [
  53. apply error!/make [make-struct [
  54. category: "Script"
  55. id: "invalid-argument"
  56. message: "Invalid argument for MAKE ERROR!"
  57. args: value
  58. ]]
  59. ]
  60. ]
  61. mold: function [
  62. "Return a LOAD-able text representation of a value"
  63. err
  64. options:
  65. only: no [logic!] "Don't generate outer [ ] for block! values"
  66. flat: no [logic!] "Produce a single text line"
  67. limit [number! none!] "Don't return a string longer than LIMIT characters"
  68. indent: "" [string!] "Add this string after each new line (ignored if flat)"
  69. ] [result] [
  70. limit-string either flat [
  71. rejoin [
  72. "make error! [category: '" err/category " id: '" err/id " "
  73. "message: ^"" escape err/message "^" "
  74. either err/args [
  75. rejoin [
  76. "args: " mold/options [
  77. value: either word? err/args [make lit-word! err/args/word] [err/args]
  78. flat
  79. limit: limit
  80. ] " "
  81. ]
  82. ] [""]
  83. "stack: " mold/options [value: err/stack flat limit: limit] "]"
  84. ]
  85. ] [
  86. rejoin [
  87. "make error! [^/"
  88. indent " category: '" err/category "^/"
  89. indent " id: '" err/id "^/"
  90. indent " message: ^"" escape err/message "^"^/"
  91. either err/args [
  92. rejoin [
  93. indent " args: " mold/options [
  94. value: either word? err/args [make lit-word! err/args/word] [err/args]
  95. indent: indent + " "
  96. limit: limit
  97. ] "^/"
  98. ]
  99. ] [""]
  100. indent " stack: " mold/options [value: err/stack limit: limit indent: indent + " "] "^/"
  101. indent "]"
  102. ]
  103. ] limit
  104. ]
  105. do: function [err block] [] [
  106. reduce [err skip block 1]
  107. ]
  108. bind: :default-bind
  109. compile: function [err block] [] [
  110. reduce [ast-value err skip block 1]
  111. ]
  112. equal?: :default-equal?
  113. copy: function [value] [] [value]
  114. ]
  115. error!/("error!"): make-struct [
  116. equal?: function [err1 err2] [] [
  117. all [
  118. err1/category = err2/category
  119. err1/id = err2/id
  120. ]
  121. ]
  122. ]
  123. topaz-make-error: function [obj] [args err] [
  124. obj: obj/map
  125. args: make-struct []
  126. if obj/category [
  127. args/category: either obj/category/type/name = "word!" [obj/category/word] [none]
  128. ]
  129. if obj/id [
  130. args/id: either obj/id/type/name = "word!" [obj/id/word] [none]
  131. ]
  132. if obj/message [
  133. args/message: either obj/message/type/name = "string!" [obj/message/string] [none]
  134. ]
  135. if all [obj/args not none? obj/args] [
  136. args/args: obj/args
  137. ]
  138. err: apply error!/make [args]
  139. if all [obj/stack block? obj/stack] [
  140. err/stack: obj/stack
  141. ]
  142. err
  143. ]
  144. error: function [args] [] [
  145. throw apply error!/make [args]
  146. ]
  147. form-error: function [err] [res] [
  148. res: rejoin [
  149. "*** " err/category " error: " err/message
  150. either err/args [
  151. either all [err/category = "Internal" string? err/args] [
  152. ; JS error
  153. rejoin [": " err/args/string]
  154. ] [
  155. rejoin [": " mold/options [value: err/args flat limit: 80]]
  156. ]
  157. ] [""]
  158. ]
  159. foreach 'item err/stack/values [
  160. either all [err/category = "Internal" string? item] [
  161. ; JS error
  162. res: rejoin [res "^/*** JS Stack:^/" item/string "^/"]
  163. ] [
  164. res: rejoin [res "^/*** Stack: " mold/options [value: item only limit: 160 indent: " : "]]
  165. ]
  166. ]
  167. res
  168. ]