github.com/primecitizens/pcz/std@v0.2.1/builtin/type/errors.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2010 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package stdtype 9 10 import ( 11 "github.com/primecitizens/pcz/std/core/abi" 12 "github.com/primecitizens/pcz/std/core/cerr" 13 ) 14 15 type error = cerr.E 16 17 // A TypeAssertionError explains a failed type assertion. 18 type TypeAssertionError struct { 19 Interface *abi.Type 20 Concrete *abi.Type 21 Asserted *abi.Type 22 MissingMethod string // one method needed by Interface, missing from Concrete 23 } 24 25 // WriteErr implements cerr.E 26 func (e *TypeAssertionError) WriteErr(w cerr.Writer) int { 27 inter := "interface" 28 if e.Interface != nil { 29 inter = e.Interface.String() 30 } 31 32 n := cerr.WriteJoinS(w, " ", "interface", "conversion") + w.Write(": ") 33 as := e.Asserted.String() 34 if e.Concrete == nil { 35 return n + cerr.WriteJoinS(w, " ", inter, "is", "nil") + w.Write(", ") + 36 cerr.WriteJoinS(w, " ", "not", as) 37 } 38 39 cs := e.Concrete.String() 40 if len(e.MissingMethod) == 0 { 41 n += cerr.WriteJoinS(w, " ", inter, "is", cs) + w.Write(", ") + 42 cerr.WriteJoinS(w, " ", "not", as) 43 44 if cs == as { 45 // provide slightly clearer error message 46 n += w.Write(" (") 47 48 if e.Concrete.PkgPath() != e.Asserted.String() { 49 n += cerr.WriteJoinS(w, " ", "types", "from", "different", "packages") 50 } else { 51 n += cerr.WriteJoinS(w, " ", "types", "from", "different", "scopes") 52 } 53 54 n += w.Write(")") 55 } 56 57 return n 58 } 59 60 return n + cerr.WriteJoinS(w, " ", cs, "is", "not", as) + w.Write(": ") + 61 cerr.WriteJoinS(w, " ", "missing", "method", e.MissingMethod) 62 }