github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/env/actions/errors.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package actions
    16  
    17  import (
    18  	"bytes"
    19  	"strings"
    20  
    21  	"github.com/dolthub/dolt/go/libraries/doltcore/diff"
    22  )
    23  
    24  type tblErrorType string
    25  
    26  const (
    27  	tblErrInvalid         tblErrorType = "invalid"
    28  	tblErrTypeNotExist    tblErrorType = "do not exist"
    29  	tblErrTypeInConflict  tblErrorType = "are in conflict"
    30  	tblErrTypeSchConflict tblErrorType = "have schema conflicts"
    31  	tblErrTypeConstViols  tblErrorType = "have constraint violations"
    32  )
    33  
    34  type TblError struct {
    35  	tables     []string
    36  	tblErrType tblErrorType
    37  }
    38  
    39  func NewTblNotExistError(tbls []string) TblError {
    40  	return TblError{tbls, tblErrTypeNotExist}
    41  }
    42  
    43  func NewTblInConflictError(tbls []string) TblError {
    44  	return TblError{tbls, tblErrTypeInConflict}
    45  }
    46  
    47  func NewTblSchemaConflictError(tbls []string) TblError {
    48  	return TblError{tbls, tblErrTypeSchConflict}
    49  }
    50  
    51  func NewTblHasConstraintViolations(tbls []string) TblError {
    52  	return TblError{tbls, tblErrTypeConstViols}
    53  }
    54  
    55  func (te TblError) Error() string {
    56  	return "error: the table(s) " + strings.Join(te.tables, ", ") + " " + string(te.tblErrType)
    57  }
    58  
    59  func getTblErrType(err error) tblErrorType {
    60  	te, ok := err.(TblError)
    61  
    62  	if ok {
    63  		return te.tblErrType
    64  	}
    65  
    66  	return tblErrInvalid
    67  }
    68  
    69  func IsTblError(err error) bool {
    70  	return getTblErrType(err) != tblErrInvalid
    71  }
    72  
    73  func IsTblNotExist(err error) bool {
    74  	return getTblErrType(err) == tblErrTypeNotExist
    75  }
    76  
    77  func IsTblInConflict(err error) bool {
    78  	return getTblErrType(err) == tblErrTypeInConflict
    79  }
    80  
    81  func IsTblViolatesConstraints(err error) bool {
    82  	return getTblErrType(err) == tblErrTypeConstViols
    83  }
    84  
    85  func GetTablesForError(err error) []string {
    86  	te, ok := err.(TblError)
    87  
    88  	if !ok {
    89  		panic("Must validate with IsTblError or more specific methods before calling GetTablesForError")
    90  	}
    91  
    92  	return te.tables
    93  }
    94  
    95  type ErrCheckoutWouldOverwrite struct {
    96  	tables []string
    97  }
    98  
    99  func (cwo ErrCheckoutWouldOverwrite) Error() string {
   100  	var buffer bytes.Buffer
   101  	buffer.WriteString("Your local changes to the following tables would be overwritten by checkout:\n")
   102  	for _, tbl := range cwo.tables {
   103  		buffer.WriteString("\t" + tbl + "\n")
   104  	}
   105  
   106  	buffer.WriteString("Please commit your changes or stash them before you switch branches.\n")
   107  	buffer.WriteString("Aborting")
   108  	return buffer.String()
   109  }
   110  
   111  func IsCheckoutWouldOverwrite(err error) bool {
   112  	_, ok := err.(ErrCheckoutWouldOverwrite)
   113  	return ok
   114  }
   115  
   116  func CheckoutWouldOverwriteTables(err error) []string {
   117  	cwo, ok := err.(ErrCheckoutWouldOverwrite)
   118  
   119  	if !ok {
   120  		panic("Must validate with IsCheckoutWouldOverwrite before calling CheckoutWouldOverwriteTables")
   121  	}
   122  
   123  	return cwo.tables
   124  }
   125  
   126  type NothingStaged struct {
   127  	NotStagedTbls []diff.TableDelta
   128  }
   129  
   130  func (ns NothingStaged) Error() string {
   131  	return "no changes added to commit"
   132  }
   133  
   134  func IsNothingStaged(err error) bool {
   135  	_, ok := err.(NothingStaged)
   136  	return ok
   137  }
   138  
   139  func NothingStagedTblDiffs(err error) []diff.TableDelta {
   140  	ns, ok := err.(NothingStaged)
   141  
   142  	if !ok {
   143  		panic("Must validate with IsCheckoutWouldOverwrite before calling CheckoutWouldOverwriteTables")
   144  	}
   145  
   146  	return ns.NotStagedTbls
   147  }