github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/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 // The Error interface identifies a run time error. 8 type Error interface { 9 error 10 11 // RuntimeError is a no-op function but 12 // serves to distinguish types that are runtime 13 // errors from ordinary errors: a type is a 14 // runtime error if it has a RuntimeError method. 15 RuntimeError() 16 } 17 18 // A TypeAssertionError explains a failed type assertion. 19 type TypeAssertionError struct { 20 interfaceString string 21 concreteString string 22 assertedString string 23 missingMethod string // one method needed by Interface, missing from Concrete 24 } 25 26 func (*TypeAssertionError) RuntimeError() {} 27 28 func (e *TypeAssertionError) Error() string { 29 inter := e.interfaceString 30 if inter == "" { 31 inter = "interface" 32 } 33 if e.concreteString == "" { 34 return "interface conversion: " + inter + " is nil, not " + e.assertedString 35 } 36 if e.missingMethod == "" { 37 return "interface conversion: " + inter + " is " + e.concreteString + 38 ", not " + e.assertedString 39 } 40 return "interface conversion: " + e.concreteString + " is not " + e.assertedString + 41 ": missing method " + e.missingMethod 42 } 43 44 // For calling from C. 45 func newTypeAssertionError(ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) { 46 var s1, s2, s3, meth string 47 48 if ps1 != nil { 49 s1 = *ps1 50 } 51 if ps2 != nil { 52 s2 = *ps2 53 } 54 if ps3 != nil { 55 s3 = *ps3 56 } 57 if pmeth != nil { 58 meth = *pmeth 59 } 60 *ret = &TypeAssertionError{s1, s2, s3, meth} 61 } 62 63 // An errorString represents a runtime error described by a single string. 64 type errorString string 65 66 func (e errorString) RuntimeError() {} 67 68 func (e errorString) Error() string { 69 return "runtime error: " + string(e) 70 } 71 72 // For calling from C. 73 func newErrorString(s string, ret *interface{}) { 74 *ret = errorString(s) 75 } 76 77 // An errorCString represents a runtime error described by a single C string. 78 type errorCString uintptr 79 80 func (e errorCString) RuntimeError() {} 81 82 func cstringToGo(uintptr) string 83 84 func (e errorCString) Error() string { 85 return "runtime error: " + cstringToGo(uintptr(e)) 86 } 87 88 // For calling from C. 89 func newErrorCString(s uintptr, ret *interface{}) { 90 *ret = errorCString(s) 91 } 92 93 type stringer interface { 94 String() string 95 } 96 97 func typestring(interface{}) string 98 99 // For calling from C. 100 // Prints an argument passed to panic. 101 // There's room for arbitrary complexity here, but we keep it 102 // simple and handle just a few important cases: int, string, and Stringer. 103 func printany(i interface{}) { 104 switch v := i.(type) { 105 case nil: 106 print("nil") 107 case stringer: 108 print(v.String()) 109 case error: 110 print(v.Error()) 111 case int: 112 print(v) 113 case string: 114 print(v) 115 default: 116 print("(", typestring(i), ") ", i) 117 } 118 } 119 120 // called from generated code 121 func panicwrap(pkg, typ, meth string) { 122 panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer") 123 }