github.com/shijuvar/go@v0.0.0-20141209052335-e8f13700b70c/src/runtime/error.go (about) 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 5 package runtime 6 7 import "unsafe" 8 9 // The Error interface identifies a run time error. 10 type Error interface { 11 error 12 13 // RuntimeError is a no-op function but 14 // serves to distinguish types that are runtime 15 // errors from ordinary errors: a type is a 16 // runtime error if it has a RuntimeError method. 17 RuntimeError() 18 } 19 20 // A TypeAssertionError explains a failed type assertion. 21 type TypeAssertionError struct { 22 interfaceString string 23 concreteString string 24 assertedString string 25 missingMethod string // one method needed by Interface, missing from Concrete 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 // For calling from C. 47 func newTypeAssertionError(ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) { 48 var s1, s2, s3, meth string 49 50 if ps1 != nil { 51 s1 = *ps1 52 } 53 if ps2 != nil { 54 s2 = *ps2 55 } 56 if ps3 != nil { 57 s3 = *ps3 58 } 59 if pmeth != nil { 60 meth = *pmeth 61 } 62 *ret = &TypeAssertionError{s1, s2, s3, meth} 63 } 64 65 // An errorString represents a runtime error described by a single string. 66 type errorString string 67 68 func (e errorString) RuntimeError() {} 69 70 func (e errorString) Error() string { 71 return "runtime error: " + string(e) 72 } 73 74 type stringer interface { 75 String() string 76 } 77 78 func typestring(x interface{}) string { 79 e := (*eface)(unsafe.Pointer(&x)) 80 return *e._type._string 81 } 82 83 // For calling from C. 84 // Prints an argument passed to panic. 85 // There's room for arbitrary complexity here, but we keep it 86 // simple and handle just a few important cases: int, string, and Stringer. 87 func printany(i interface{}) { 88 switch v := i.(type) { 89 case nil: 90 print("nil") 91 case stringer: 92 print(v.String()) 93 case error: 94 print(v.Error()) 95 case int: 96 print(v) 97 case string: 98 print(v) 99 default: 100 print("(", typestring(i), ") ", i) 101 } 102 } 103 104 // called from generated code 105 func panicwrap(pkg, typ, meth string) { 106 panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer") 107 }