github.com/nevalang/neva@v0.23.1-0.20240507185603-7696a9bb8dda/internal/compiler/error.go (about)

     1  package compiler
     2  
     3  import (
     4  	"fmt"
     5  
     6  	src "github.com/nevalang/neva/internal/compiler/sourcecode"
     7  	"github.com/nevalang/neva/internal/compiler/sourcecode/core"
     8  )
     9  
    10  type Error struct {
    11  	Err      error
    12  	Location *src.Location
    13  	Meta     *core.Meta
    14  	child    *Error
    15  }
    16  
    17  func (e Error) Wrap(child *Error) *Error {
    18  	e.child = child
    19  	return &e
    20  }
    21  
    22  func (e Error) unwrap() Error {
    23  	for e.child != nil {
    24  		e = *e.child
    25  	}
    26  	return e
    27  }
    28  
    29  func (e Error) Error() string {
    30  	e = e.unwrap()
    31  
    32  	hasErr := e.Err != nil
    33  	hasMeta := e.Meta != nil
    34  	hasLocation := e.Location != nil
    35  
    36  	switch {
    37  	case hasLocation && hasMeta:
    38  		return fmt.Sprintf("%v:%v %v", *e.Location, e.Meta.Start, e.Err)
    39  	case hasLocation:
    40  		return fmt.Sprintf("%v %v", *e.Location, e.Err)
    41  	case hasMeta:
    42  		return fmt.Sprintf("%v %v", e.Meta.Start, e.Err)
    43  	case hasErr:
    44  		return e.Err.Error()
    45  	}
    46  
    47  	panic(e)
    48  }