error.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package otto
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/robertkrimen/otto/file"
  6. )
  7. type _exception struct {
  8. value interface{}
  9. }
  10. func newException(value interface{}) *_exception {
  11. return &_exception{
  12. value: value,
  13. }
  14. }
  15. func (self *_exception) eject() interface{} {
  16. value := self.value
  17. self.value = nil // Prevent Go from holding on to the value, whatever it is
  18. return value
  19. }
  20. type _error struct {
  21. name string
  22. message string
  23. trace []_frame
  24. offset int
  25. }
  26. func (err _error) format() string {
  27. if len(err.name) == 0 {
  28. return err.message
  29. }
  30. if len(err.message) == 0 {
  31. return err.name
  32. }
  33. return fmt.Sprintf("%s: %s", err.name, err.message)
  34. }
  35. func (err _error) formatWithStack() string {
  36. str := err.format() + "\n"
  37. for _, frame := range err.trace {
  38. str += " at " + frame.location() + "\n"
  39. }
  40. return str
  41. }
  42. type _frame struct {
  43. native bool
  44. nativeFile string
  45. nativeLine int
  46. file *file.File
  47. offset int
  48. callee string
  49. fn interface{}
  50. }
  51. var (
  52. nativeFrame = _frame{}
  53. )
  54. type _at int
  55. func (fr _frame) location() string {
  56. str := "<unknown>"
  57. switch {
  58. case fr.native:
  59. str = "<native code>"
  60. if fr.nativeFile != "" && fr.nativeLine != 0 {
  61. str = fmt.Sprintf("%s:%d", fr.nativeFile, fr.nativeLine)
  62. }
  63. case fr.file != nil:
  64. if p := fr.file.Position(file.Idx(fr.offset)); p != nil {
  65. path, line, column := p.Filename, p.Line, p.Column
  66. if path == "" {
  67. path = "<anonymous>"
  68. }
  69. str = fmt.Sprintf("%s:%d:%d", path, line, column)
  70. }
  71. }
  72. if fr.callee != "" {
  73. str = fmt.Sprintf("%s (%s)", fr.callee, str)
  74. }
  75. return str
  76. }
  77. // An Error represents a runtime error, e.g. a TypeError, a ReferenceError, etc.
  78. type Error struct {
  79. _error
  80. }
  81. // Error returns a description of the error
  82. //
  83. // TypeError: 'def' is not a function
  84. //
  85. func (err Error) Error() string {
  86. return err.format()
  87. }
  88. // String returns a description of the error and a trace of where the
  89. // error occurred.
  90. //
  91. // TypeError: 'def' is not a function
  92. // at xyz (<anonymous>:3:9)
  93. // at <anonymous>:7:1/
  94. //
  95. func (err Error) String() string {
  96. return err.formatWithStack()
  97. }
  98. func (err _error) describe(format string, in ...interface{}) string {
  99. return fmt.Sprintf(format, in...)
  100. }
  101. func (self _error) messageValue() Value {
  102. if self.message == "" {
  103. return Value{}
  104. }
  105. return toValue_string(self.message)
  106. }
  107. func (rt *_runtime) typeErrorResult(throw bool) bool {
  108. if throw {
  109. panic(rt.panicTypeError())
  110. }
  111. return false
  112. }
  113. func newError(rt *_runtime, name string, stackFramesToPop int, in ...interface{}) _error {
  114. err := _error{
  115. name: name,
  116. offset: -1,
  117. }
  118. description := ""
  119. length := len(in)
  120. if rt != nil && rt.scope != nil {
  121. scope := rt.scope
  122. for i := 0; i < stackFramesToPop; i++ {
  123. if scope.outer != nil {
  124. scope = scope.outer
  125. }
  126. }
  127. frame := scope.frame
  128. if length > 0 {
  129. if at, ok := in[length-1].(_at); ok {
  130. in = in[0 : length-1]
  131. if scope != nil {
  132. frame.offset = int(at)
  133. }
  134. length--
  135. }
  136. if length > 0 {
  137. description, in = in[0].(string), in[1:]
  138. }
  139. }
  140. limit := rt.traceLimit
  141. err.trace = append(err.trace, frame)
  142. if scope != nil {
  143. for scope = scope.outer; scope != nil; scope = scope.outer {
  144. if limit--; limit == 0 {
  145. break
  146. }
  147. if scope.frame.offset >= 0 {
  148. err.trace = append(err.trace, scope.frame)
  149. }
  150. }
  151. }
  152. } else {
  153. if length > 0 {
  154. description, in = in[0].(string), in[1:]
  155. }
  156. }
  157. err.message = err.describe(description, in...)
  158. return err
  159. }
  160. func (rt *_runtime) panicTypeError(argumentList ...interface{}) *_exception {
  161. return &_exception{
  162. value: newError(rt, "TypeError", 0, argumentList...),
  163. }
  164. }
  165. func (rt *_runtime) panicReferenceError(argumentList ...interface{}) *_exception {
  166. return &_exception{
  167. value: newError(rt, "ReferenceError", 0, argumentList...),
  168. }
  169. }
  170. func (rt *_runtime) panicURIError(argumentList ...interface{}) *_exception {
  171. return &_exception{
  172. value: newError(rt, "URIError", 0, argumentList...),
  173. }
  174. }
  175. func (rt *_runtime) panicSyntaxError(argumentList ...interface{}) *_exception {
  176. return &_exception{
  177. value: newError(rt, "SyntaxError", 0, argumentList...),
  178. }
  179. }
  180. func (rt *_runtime) panicRangeError(argumentList ...interface{}) *_exception {
  181. return &_exception{
  182. value: newError(rt, "RangeError", 0, argumentList...),
  183. }
  184. }
  185. func catchPanic(function func()) (err error) {
  186. defer func() {
  187. if caught := recover(); caught != nil {
  188. if exception, ok := caught.(*_exception); ok {
  189. caught = exception.eject()
  190. }
  191. switch caught := caught.(type) {
  192. case *Error:
  193. err = caught
  194. return
  195. case _error:
  196. err = &Error{caught}
  197. return
  198. case Value:
  199. if vl := caught._object(); vl != nil {
  200. switch vl := vl.value.(type) {
  201. case _error:
  202. err = &Error{vl}
  203. return
  204. }
  205. }
  206. err = errors.New(caught.string())
  207. return
  208. }
  209. panic(caught)
  210. }
  211. }()
  212. function()
  213. return nil
  214. }