github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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  type stringer interface {
    78  	String() string
    79  }
    80  
    81  func typestring(interface{}) string
    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  }