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.
9 // The Error interface identifies a run time error.
10 type Error
interface {
13 // RuntimeError is a no-op function but
14 // serves to distinguish types that are run time
15 // errors from ordinary errors: a type is a
16 // run time error if it has a RuntimeError method.
20 // A TypeAssertionError explains a failed type assertion.
21 type TypeAssertionError
struct {
22 interfaceString
string
25 missingMethod
string // one method needed by Interface, missing from Concrete
28 func (*TypeAssertionError
) RuntimeError() {}
30 func (e
*TypeAssertionError
) Error() string {
31 inter
:= e
.interfaceString
35 if e
.concreteString
== "" {
36 return "interface conversion: " + inter
+ " is nil, not " + e
.assertedString
38 if e
.missingMethod
== "" {
39 return "interface conversion: " + inter
+ " is " + e
.concreteString
+
40 ", not " + e
.assertedString
42 return "interface conversion: " + e
.concreteString
+ " is not " + e
.assertedString
+
43 ": missing method " + e
.missingMethod
46 // For calling from C.
47 func NewTypeAssertionError(ps1
, ps2
, ps3
*string, pmeth
*string, ret
*interface{}) {
48 var s1
, s2
, s3
, meth
string
63 // For gccgo, strip out quoted strings.
68 *ret
= &TypeAssertionError
{s1
, s2
, s3
, meth
}
71 // Remove quoted strings from gccgo reflection strings.
72 func unquote(s
string) string {
75 for i
= 0; i
< ls
; i
++ {
84 r
:= make([]byte, len(s
))
86 for i
= 0; i
< ls
; i
++ {
97 // An errorString represents a runtime error described by a single string.
98 type errorString
string
100 func (e errorString
) RuntimeError() {}
102 func (e errorString
) Error() string {
103 return "runtime error: " + string(e
)
106 // An errorCString represents a runtime error described by a single C string.
107 // Not "type errorCString uintptr" because of http://golang.org/issue/7084.
108 type errorCString
struct{ cstr
uintptr }
110 func (e errorCString
) RuntimeError() {}
112 func (e errorCString
) Error() string {
113 return "runtime error: " + gostringnocopy((*byte)(unsafe
.Pointer(e
.cstr
)))
116 // For calling from C.
117 func NewErrorCString(s
uintptr, ret
*interface{}) {
118 *ret
= errorCString
{s
}
121 // plainError represents a runtime error described a string without
122 // the prefix "runtime error: " after invoking errorString.Error().
124 type plainError
string
126 func (e plainError
) RuntimeError() {}
128 func (e plainError
) Error() string {
132 type stringer
interface {
136 func typestring(x
interface{}) string {
138 return *e
._type
.string
141 // For calling from C.
142 // Prints an argument passed to panic.
143 // There's room for arbitrary complexity here, but we keep it
144 // simple and handle just a few important cases: int, string, and Stringer.
145 func printany(i
interface{}) {
146 switch v
:= i
.(type) {
158 print("(", typestring(i
), ") ", i
)
162 // called from generated code
163 func panicwrap(pkg
, typ
, meth
string) {
164 panic(plainError("value method " + pkg
+ "." + typ
+ "." + meth
+ " called using nil *" + typ
+ " pointer"))