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