...
Source file
src/runtime/error.go
Documentation: runtime
1
2
3
4
5 package runtime
6
7 import _ "unsafe"
8
9
10 type Error interface {
11 error
12
13
14
15
16
17 RuntimeError()
18 }
19
20
21 type TypeAssertionError struct {
22 interfaceString string
23 concreteString string
24 assertedString string
25 missingMethod string
26 }
27
28 func (*TypeAssertionError) RuntimeError() {}
29
30 func (e *TypeAssertionError) Error() string {
31 inter := e.interfaceString
32 if inter == "" {
33 inter = "interface"
34 }
35 if e.concreteString == "" {
36 return "interface conversion: " + inter + " is nil, not " + e.assertedString
37 }
38 if e.missingMethod == "" {
39 return "interface conversion: " + inter + " is " + e.concreteString +
40 ", not " + e.assertedString
41 }
42 return "interface conversion: " + e.concreteString + " is not " + e.assertedString +
43 ": missing method " + e.missingMethod
44 }
45
46
47 type errorString string
48
49 func (e errorString) RuntimeError() {}
50
51 func (e errorString) Error() string {
52 return "runtime error: " + string(e)
53 }
54
55
56
57
58 type plainError string
59
60 func (e plainError) RuntimeError() {}
61
62 func (e plainError) Error() string {
63 return string(e)
64 }
65
66 type stringer interface {
67 String() string
68 }
69
70 func typestring(x interface{}) string {
71 e := efaceOf(&x)
72 return e._type.string()
73 }
74
75
76 func printany(i interface{}) {
77 switch v := i.(type) {
78 case nil:
79 print("nil")
80 case stringer:
81 print(v.String())
82 case error:
83 print(v.Error())
84 case bool:
85 print(v)
86 case int:
87 print(v)
88 case int8:
89 print(v)
90 case int16:
91 print(v)
92 case int32:
93 print(v)
94 case int64:
95 print(v)
96 case uint:
97 print(v)
98 case uint8:
99 print(v)
100 case uint16:
101 print(v)
102 case uint32:
103 print(v)
104 case uint64:
105 print(v)
106 case uintptr:
107 print(v)
108 case float32:
109 print(v)
110 case float64:
111 print(v)
112 case complex64:
113 print(v)
114 case complex128:
115 print(v)
116 case string:
117 print(v)
118 default:
119 print("(", typestring(i), ") ", i)
120 }
121 }
122
123
124
125
126 func stringsIndexByte(s string, c byte) int
127
128
129
130
131
132 func panicwrap() {
133 pc := getcallerpc()
134 name := funcname(findfunc(pc))
135
136
137
138 i := stringsIndexByte(name, '(')
139 if i < 0 {
140 throw("panicwrap: no ( in " + name)
141 }
142 pkg := name[:i-1]
143 if i+2 >= len(name) || name[i-1:i+2] != ".(*" {
144 throw("panicwrap: unexpected string after package name: " + name)
145 }
146 name = name[i+2:]
147 i = stringsIndexByte(name, ')')
148 if i < 0 {
149 throw("panicwrap: no ) in " + name)
150 }
151 if i+2 >= len(name) || name[i:i+2] != ")." {
152 throw("panicwrap: unexpected string after type name: " + name)
153 }
154 typ := name[:i]
155 meth := name[i+2:]
156 panic(plainError("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer"))
157 }
158
View as plain text