github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/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 run time
    15  	// errors from ordinary errors: a type is a
    16  	// run time 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  // An errorString represents a runtime error described by a single string.
    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  type stringer interface {
    56  	String() string
    57  }
    58  
    59  func typestring(x interface{}) string {
    60  	e := (*eface)(unsafe.Pointer(&x))
    61  	return *e._type._string
    62  }
    63  
    64  // For calling from C.
    65  // Prints an argument passed to panic.
    66  // There's room for arbitrary complexity here, but we keep it
    67  // simple and handle just a few important cases: int, string, and Stringer.
    68  func printany(i interface{}) {
    69  	switch v := i.(type) {
    70  	case nil:
    71  		print("nil")
    72  	case stringer:
    73  		print(v.String())
    74  	case error:
    75  		print(v.Error())
    76  	case int:
    77  		print(v)
    78  	case string:
    79  		print(v)
    80  	default:
    81  		print("(", typestring(i), ") ", i)
    82  	}
    83  }
    84  
    85  // called from generated code
    86  func panicwrap(pkg, typ, meth string) {
    87  	panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer")
    88  }