Home | History | Annotate | Download | only in runtime
      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 // plainError represents a runtime error described a string without
     54 // the prefix "runtime error: " after invoking errorString.Error().
     55 // See Issue #14965.
     56 type plainError string
     57 
     58 func (e plainError) RuntimeError() {}
     59 
     60 func (e plainError) Error() string {
     61 	return string(e)
     62 }
     63 
     64 type stringer interface {
     65 	String() string
     66 }
     67 
     68 func typestring(x interface{}) string {
     69 	e := efaceOf(&x)
     70 	return e._type.string()
     71 }
     72 
     73 // For calling from C.
     74 // Prints an argument passed to panic.
     75 // There's room for arbitrary complexity here, but we keep it
     76 // simple and handle just a few important cases: int, string, and Stringer.
     77 func printany(i interface{}) {
     78 	switch v := i.(type) {
     79 	case nil:
     80 		print("nil")
     81 	case stringer:
     82 		print(v.String())
     83 	case error:
     84 		print(v.Error())
     85 	case int:
     86 		print(v)
     87 	case string:
     88 		print(v)
     89 	default:
     90 		print("(", typestring(i), ") ", i)
     91 	}
     92 }
     93 
     94 // called from generated code
     95 func panicwrap(pkg, typ, meth string) {
     96 	panic(plainError("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer"))
     97 }
     98