OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package runtime | 5 package runtime |
6 | 6 |
7 // The Error interface identifies a run time error. | 7 // The Error interface identifies a run time error. |
8 type Error interface { | 8 type Error interface { |
9 » String() string | 9 » error |
10 | 10 |
11 // RuntimeError is a no-op function but | 11 // RuntimeError is a no-op function but |
12 // serves to distinguish types that are runtime | 12 // serves to distinguish types that are runtime |
13 // errors from ordinary os.Errors: a type is a | 13 // errors from ordinary os.Errors: a type is a |
14 // runtime error if it has a RuntimeError method. | 14 // runtime error if it has a RuntimeError method. |
15 RuntimeError() | 15 RuntimeError() |
16 } | 16 } |
17 | 17 |
18 // A TypeAssertionError explains a failed type assertion. | 18 // A TypeAssertionError explains a failed type assertion. |
19 type TypeAssertionError struct { | 19 type TypeAssertionError struct { |
20 interfaceType Type // interface had this type | 20 interfaceType Type // interface had this type |
21 concreteType Type // concrete value had this type | 21 concreteType Type // concrete value had this type |
22 assertedType Type // asserted type | 22 assertedType Type // asserted type |
23 interfaceString string | 23 interfaceString string |
24 concreteString string | 24 concreteString string |
25 assertedString string | 25 assertedString string |
26 missingMethod string // one method needed by Interface, missing from C
oncrete | 26 missingMethod string // one method needed by Interface, missing from C
oncrete |
27 } | 27 } |
28 | 28 |
29 func (*TypeAssertionError) RuntimeError() {} | 29 func (*TypeAssertionError) RuntimeError() {} |
30 | 30 |
31 func (e *TypeAssertionError) String() string { | 31 func (e *TypeAssertionError) Error() string { |
32 inter := e.interfaceString | 32 inter := e.interfaceString |
33 if inter == "" { | 33 if inter == "" { |
34 inter = "interface" | 34 inter = "interface" |
35 } | 35 } |
36 if e.concreteType == nil { | 36 if e.concreteType == nil { |
37 return "interface conversion: " + inter + " is nil, not " + e.as
sertedString | 37 return "interface conversion: " + inter + " is nil, not " + e.as
sertedString |
38 } | 38 } |
39 if e.missingMethod == "" { | 39 if e.missingMethod == "" { |
40 return "interface conversion: " + inter + " is " + e.concreteStr
ing + | 40 return "interface conversion: " + inter + " is " + e.concreteStr
ing + |
41 ", not " + e.assertedString | 41 ", not " + e.assertedString |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 meth = *pmeth | 91 meth = *pmeth |
92 } | 92 } |
93 *ret = &TypeAssertionError{t1, t2, t3, s1, s2, s3, meth} | 93 *ret = &TypeAssertionError{t1, t2, t3, s1, s2, s3, meth} |
94 } | 94 } |
95 | 95 |
96 // An errorString represents a runtime error described by a single string. | 96 // An errorString represents a runtime error described by a single string. |
97 type errorString string | 97 type errorString string |
98 | 98 |
99 func (e errorString) RuntimeError() {} | 99 func (e errorString) RuntimeError() {} |
100 | 100 |
101 func (e errorString) String() string { | 101 func (e errorString) Error() string { |
102 return "runtime error: " + string(e) | 102 return "runtime error: " + string(e) |
103 } | 103 } |
104 | 104 |
105 // For calling from C. | 105 // For calling from C. |
106 func newErrorString(s string, ret *interface{}) { | 106 func newErrorString(s string, ret *interface{}) { |
107 *ret = errorString(s) | 107 *ret = errorString(s) |
108 } | 108 } |
109 | 109 |
110 type stringer interface { | 110 type stringer interface { |
111 String() string | 111 String() string |
112 } | 112 } |
113 | 113 |
114 func typestring(interface{}) string | 114 func typestring(interface{}) string |
115 | 115 |
116 // For calling from C. | 116 // For calling from C. |
117 // Prints an argument passed to panic. | 117 // Prints an argument passed to panic. |
118 // There's room for arbitrary complexity here, but we keep it | 118 // There's room for arbitrary complexity here, but we keep it |
119 // simple and handle just a few important cases: int, string, and Stringer. | 119 // simple and handle just a few important cases: int, string, and Stringer. |
120 func printany(i interface{}) { | 120 func printany(i interface{}) { |
121 switch v := i.(type) { | 121 switch v := i.(type) { |
122 case nil: | 122 case nil: |
123 print("nil") | 123 print("nil") |
124 case stringer: | 124 case stringer: |
125 print(v.String()) | 125 print(v.String()) |
| 126 case error: |
| 127 print(v.Error()) |
126 case int: | 128 case int: |
127 print(v) | 129 print(v) |
128 case string: | 130 case string: |
129 print(v) | 131 print(v) |
130 default: | 132 default: |
131 print("(", typestring(i), ") ", i) | 133 print("(", typestring(i), ") ", i) |
132 } | 134 } |
133 } | 135 } |
134 | 136 |
135 // called from generated code | 137 // called from generated code |
136 func panicwrap(pkg, typ, meth string) { | 138 func panicwrap(pkg, typ, meth string) { |
137 panic("value method " + pkg + "." + typ + "." + meth + " called using ni
l *" + typ + " pointer") | 139 panic("value method " + pkg + "." + typ + "." + meth + " called using ni
l *" + typ + " pointer") |
138 } | 140 } |
OLD | NEW |