error.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 (e *exception) eject() interface{} {
  16. value := e.value
  17. e.value = nil // Prevent Go from holding on to the value, whatever it is
  18. return value
  19. }
  20. type ottoError struct {
  21. name string
  22. message string
  23. trace []frame
  24. offset int
  25. }
  26. func (e ottoError) format() string {
  27. if len(e.name) == 0 {
  28. return e.message
  29. }
  30. if len(e.message) == 0 {
  31. return e.name
  32. }
  33. return fmt.Sprintf("%s: %s", e.name, e.message)
  34. }
  35. func (e ottoError) formatWithStack() string {
  36. str := e.format() + "\n"
  37. for _, frm := range e.trace {
  38. str += " at " + frm.location() + "\n"
  39. }
  40. return str
  41. }
  42. type frame struct {
  43. fn interface{}
  44. file *file.File
  45. nativeFile string
  46. callee string
  47. nativeLine int
  48. offset int
  49. native bool
  50. }
  51. var nativeFrame = frame{}
  52. type at int
  53. func (fr frame) location() string {
  54. str := "<unknown>"
  55. switch {
  56. case fr.native:
  57. str = "<native code>"
  58. if fr.nativeFile != "" && fr.nativeLine != 0 {
  59. str = fmt.Sprintf("%s:%d", fr.nativeFile, fr.nativeLine)
  60. }
  61. case fr.file != nil:
  62. if p := fr.file.Position(file.Idx(fr.offset)); p != nil {
  63. path, line, column := p.Filename, p.Line, p.Column
  64. if path == "" {
  65. path = "<anonymous>"
  66. }
  67. str = fmt.Sprintf("%s:%d:%d", path, line, column)
  68. }
  69. }
  70. if fr.callee != "" {
  71. str = fmt.Sprintf("%s (%s)", fr.callee, str)
  72. }
  73. return str
  74. }
  75. // An Error represents a runtime error, e.g. a TypeError, a ReferenceError, etc.
  76. type Error struct {
  77. ottoError
  78. }
  79. // Error returns a description of the error
  80. //
  81. // TypeError: 'def' is not a function
  82. func (e Error) Error() string {
  83. return e.format()
  84. }
  85. // String returns a description of the error and a trace of where the
  86. // error occurred.
  87. //
  88. // TypeError: 'def' is not a function
  89. // at xyz (<anonymous>:3:9)
  90. // at <anonymous>:7:1/
  91. func (e Error) String() string {
  92. return e.formatWithStack()
  93. }
  94. // GoString returns a description of the error and a trace of where the
  95. // error occurred. Printing with %#v will trigger this behaviour.
  96. func (e Error) GoString() string {
  97. return e.formatWithStack()
  98. }
  99. func (e ottoError) describe(format string, in ...interface{}) string {
  100. return fmt.Sprintf(format, in...)
  101. }
  102. func (e ottoError) messageValue() Value {
  103. if e.message == "" {
  104. return Value{}
  105. }
  106. return stringValue(e.message)
  107. }
  108. func (rt *runtime) typeErrorResult(throw bool) bool {
  109. if throw {
  110. panic(rt.panicTypeError())
  111. }
  112. return false
  113. }
  114. func newError(rt *runtime, name string, stackFramesToPop int, in ...interface{}) ottoError {
  115. err := ottoError{
  116. name: name,
  117. offset: -1,
  118. }
  119. description := ""
  120. length := len(in)
  121. if rt != nil && rt.scope != nil {
  122. curScope := rt.scope
  123. for range stackFramesToPop {
  124. if curScope.outer != nil {
  125. curScope = curScope.outer
  126. }
  127. }
  128. frm := curScope.frame
  129. if length > 0 {
  130. if atv, ok := in[length-1].(at); ok {
  131. in = in[0 : length-1]
  132. if curScope != nil {
  133. frm.offset = int(atv)
  134. }
  135. length--
  136. }
  137. if length > 0 {
  138. description, in = in[0].(string), in[1:]
  139. }
  140. }
  141. limit := rt.traceLimit
  142. err.trace = append(err.trace, frm)
  143. if curScope != nil {
  144. for curScope = curScope.outer; curScope != nil; curScope = curScope.outer {
  145. if limit--; limit == 0 {
  146. break
  147. }
  148. if curScope.frame.offset >= 0 {
  149. err.trace = append(err.trace, curScope.frame)
  150. }
  151. }
  152. }
  153. } else if length > 0 {
  154. description, in = in[0].(string), in[1:]
  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 excep, ok := caught.(*exception); ok {
  188. caught = excep.eject()
  189. }
  190. switch caught := caught.(type) {
  191. case *Error:
  192. err = caught
  193. return
  194. case ottoError:
  195. err = &Error{caught}
  196. return
  197. case Value:
  198. if vl := caught.object(); vl != nil {
  199. if vl, ok := vl.value.(ottoError); ok {
  200. err = &Error{vl}
  201. return
  202. }
  203. }
  204. err = errors.New(caught.string())
  205. return
  206. }
  207. panic(caught)
  208. }
  209. }()
  210. function()
  211. return nil
  212. }