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