error.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package runtime
  5. // The Error interface identifies a run time error.
  6. type Error interface {
  7. error
  8. // RuntimeError is a no-op function but
  9. // serves to distinguish types that are runtime
  10. // errors from ordinary errors: a type is a
  11. // runtime error if it has a RuntimeError method.
  12. RuntimeError()
  13. }
  14. // A TypeAssertionError explains a failed type assertion.
  15. type TypeAssertionError struct {
  16. interfaceString string
  17. concreteString string
  18. assertedString string
  19. missingMethod string // one method needed by Interface, missing from Concrete
  20. }
  21. func (*TypeAssertionError) RuntimeError() {}
  22. func (e *TypeAssertionError) Error() string {
  23. inter := e.interfaceString
  24. if inter == "" {
  25. inter = "interface"
  26. }
  27. if e.concreteString == "" {
  28. return "interface conversion: " + inter + " is nil, not " + e.assertedString
  29. }
  30. if e.missingMethod == "" {
  31. return "interface conversion: " + inter + " is " + e.concreteString +
  32. ", not " + e.assertedString
  33. }
  34. return "interface conversion: " + e.concreteString + " is not " + e.assertedString +
  35. ": missing method " + e.missingMethod
  36. }
  37. // For calling from C.
  38. func NewTypeAssertionError(ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) {
  39. var s1, s2, s3, meth string
  40. if ps1 != nil {
  41. s1 = *ps1
  42. }
  43. if ps2 != nil {
  44. s2 = *ps2
  45. }
  46. if ps3 != nil {
  47. s3 = *ps3
  48. }
  49. if pmeth != nil {
  50. meth = *pmeth
  51. }
  52. // For gccgo, strip out quoted strings.
  53. s1 = unquote(s1)
  54. s2 = unquote(s2)
  55. s3 = unquote(s3)
  56. *ret = &TypeAssertionError{s1, s2, s3, meth}
  57. }
  58. // Remove quoted strings from gccgo reflection strings.
  59. func unquote(s string) string {
  60. ls := len(s)
  61. var i int
  62. for i = 0; i < ls; i++ {
  63. if s[i] == '\t' {
  64. break
  65. }
  66. }
  67. if i == ls {
  68. return s
  69. }
  70. var q bool
  71. r := make([]byte, len(s))
  72. j := 0
  73. for i = 0; i < ls; i++ {
  74. if s[i] == '\t' {
  75. q = !q
  76. } else if !q {
  77. r[j] = s[i]
  78. j++
  79. }
  80. }
  81. return string(r[:j])
  82. }
  83. // An errorString represents a runtime error described by a single string.
  84. type errorString string
  85. func (e errorString) RuntimeError() {}
  86. func (e errorString) Error() string {
  87. return "runtime error: " + string(e)
  88. }
  89. // For calling from C.
  90. func NewErrorString(s string, ret *interface{}) {
  91. *ret = errorString(s)
  92. }
  93. // An errorCString represents a runtime error described by a single C string.
  94. // Not "type errorCString uintptr" because of http://golang.org/issue/7084.
  95. type errorCString struct{ cstr uintptr }
  96. func (e errorCString) RuntimeError() {}
  97. func cstringToGo(uintptr) string
  98. func (e errorCString) Error() string {
  99. return "runtime error: " + cstringToGo(e.cstr)
  100. }
  101. // For calling from C.
  102. func NewErrorCString(s uintptr, ret *interface{}) {
  103. *ret = errorCString{s}
  104. }
  105. type stringer interface {
  106. String() string
  107. }
  108. func typestring(interface{}) string
  109. // For calling from C.
  110. // Prints an argument passed to panic.
  111. // There's room for arbitrary complexity here, but we keep it
  112. // simple and handle just a few important cases: int, string, and Stringer.
  113. func Printany(i interface{}) {
  114. switch v := i.(type) {
  115. case nil:
  116. print("nil")
  117. case stringer:
  118. print(v.String())
  119. case error:
  120. print(v.Error())
  121. case int:
  122. print(v)
  123. case string:
  124. print(v)
  125. default:
  126. print("(", typestring(i), ") ", i)
  127. }
  128. }
  129. // called from generated code
  130. func panicwrap(pkg, typ, meth string) {
  131. panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer")
  132. }