github.com/daixiang0/gci@v0.13.0/pkg/section/errors.go (about)

     1  package section
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/daixiang0/gci/pkg/parse"
     8  	"github.com/daixiang0/gci/pkg/utils"
     9  )
    10  
    11  type SectionParsingError struct {
    12  	error
    13  }
    14  
    15  func (s SectionParsingError) Unwrap() error {
    16  	return s.error
    17  }
    18  
    19  func (s SectionParsingError) Wrap(sectionStr string) error {
    20  	return fmt.Errorf("failed to parse section %q: %w", sectionStr, s)
    21  }
    22  
    23  func (s SectionParsingError) Is(err error) bool {
    24  	_, ok := err.(SectionParsingError)
    25  	return ok
    26  }
    27  
    28  var MissingParameterClosingBracketsError = fmt.Errorf("section parameter is missing closing %q", utils.RightParenthesis)
    29  
    30  var MoreThanOneOpeningQuotesError = fmt.Errorf("found more than one %q parameter start sequences", utils.RightParenthesis)
    31  
    32  var SectionTypeDoesNotAcceptParametersError = errors.New("section type does not accept a parameter")
    33  
    34  var SectionTypeDoesNotAcceptPrefixError = errors.New("section may not contain a Prefix")
    35  
    36  var SectionTypeDoesNotAcceptSuffixError = errors.New("section may not contain a Suffix")
    37  
    38  type EqualSpecificityMatchError struct {
    39  	Imports            *parse.GciImports
    40  	SectionA, SectionB Section
    41  }
    42  
    43  func (e EqualSpecificityMatchError) Error() string {
    44  	return fmt.Sprintf("Import %v matched section %s and %s equally", e.Imports, e.SectionA, e.SectionB)
    45  }
    46  
    47  func (e EqualSpecificityMatchError) Is(err error) bool {
    48  	_, ok := err.(EqualSpecificityMatchError)
    49  	return ok
    50  }
    51  
    52  type NoMatchingSectionForImportError struct {
    53  	Imports *parse.GciImports
    54  }
    55  
    56  func (n NoMatchingSectionForImportError) Error() string {
    57  	return fmt.Sprintf("No section found for Import: %v", n.Imports)
    58  }
    59  
    60  func (n NoMatchingSectionForImportError) Is(err error) bool {
    61  	_, ok := err.(NoMatchingSectionForImportError)
    62  	return ok
    63  }
    64  
    65  type InvalidImportSplitError struct {
    66  	segments []string
    67  }
    68  
    69  func (i InvalidImportSplitError) Error() string {
    70  	return fmt.Sprintf("separating the inline comment from the import yielded an invalid number of segments: %v", i.segments)
    71  }
    72  
    73  func (i InvalidImportSplitError) Is(err error) bool {
    74  	_, ok := err.(InvalidImportSplitError)
    75  	return ok
    76  }
    77  
    78  type InvalidAliasSplitError struct {
    79  	segments []string
    80  }
    81  
    82  func (i InvalidAliasSplitError) Error() string {
    83  	return fmt.Sprintf("separating the alias from the path yielded an invalid number of segments: %v", i.segments)
    84  }
    85  
    86  func (i InvalidAliasSplitError) Is(err error) bool {
    87  	_, ok := err.(InvalidAliasSplitError)
    88  	return ok
    89  }
    90  
    91  var (
    92  	MissingImportStatementError   = FileParsingError{errors.New("no import statement present in File")}
    93  	ImportStatementNotClosedError = FileParsingError{errors.New("import statement not closed")}
    94  )
    95  
    96  type FileParsingError struct {
    97  	error
    98  }
    99  
   100  func (f FileParsingError) Unwrap() error {
   101  	return f.error
   102  }
   103  
   104  func (f FileParsingError) Is(err error) bool {
   105  	_, ok := err.(FileParsingError)
   106  	return ok
   107  }