github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/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  	"strings"
    19  
    20  	"github.com/dolthub/dolt/go/libraries/doltcore/diff"
    21  )
    22  
    23  type tblErrorType string
    24  
    25  const (
    26  	tblErrInvalid        tblErrorType = "invalid"
    27  	tblErrTypeNotExist   tblErrorType = "do not exist"
    28  	tblErrTypeInConflict tblErrorType = "in conflict"
    29  )
    30  
    31  type TblError struct {
    32  	tables     []string
    33  	tblErrType tblErrorType
    34  }
    35  
    36  func NewTblNotExistError(tbls []string) TblError {
    37  	return TblError{tbls, tblErrTypeNotExist}
    38  }
    39  
    40  func NewTblInConflictError(tbls []string) TblError {
    41  	return TblError{tbls, tblErrTypeInConflict}
    42  }
    43  
    44  func (te TblError) Error() string {
    45  	return "error: the table(s) " + strings.Join(te.tables, ", ") + " " + string(te.tblErrType)
    46  }
    47  
    48  func getTblErrType(err error) tblErrorType {
    49  	te, ok := err.(TblError)
    50  
    51  	if ok {
    52  		return te.tblErrType
    53  	}
    54  
    55  	return tblErrInvalid
    56  }
    57  
    58  func IsTblError(err error) bool {
    59  	return getTblErrType(err) != tblErrInvalid
    60  }
    61  
    62  func IsTblNotExist(err error) bool {
    63  	return getTblErrType(err) == tblErrTypeNotExist
    64  }
    65  
    66  func IsTblInConflict(err error) bool {
    67  	return getTblErrType(err) == tblErrTypeInConflict
    68  }
    69  
    70  func GetTablesForError(err error) []string {
    71  	te, ok := err.(TblError)
    72  
    73  	if !ok {
    74  		panic("Must validate with IsTblError or more specific methods before calling GetTablesForError")
    75  	}
    76  
    77  	return te.tables
    78  }
    79  
    80  type CheckoutWouldOverwrite struct {
    81  	tables []string
    82  }
    83  
    84  func (cwo CheckoutWouldOverwrite) Error() string {
    85  	return "local changes would be overwritten by overwrite"
    86  }
    87  
    88  func IsCheckoutWouldOverwrite(err error) bool {
    89  	_, ok := err.(CheckoutWouldOverwrite)
    90  	return ok
    91  }
    92  
    93  func CheckoutWouldOverwriteTables(err error) []string {
    94  	cwo, ok := err.(CheckoutWouldOverwrite)
    95  
    96  	if !ok {
    97  		panic("Must validate with IsCheckoutWouldOverwrite before calling CheckoutWouldOverwriteTables")
    98  	}
    99  
   100  	return cwo.tables
   101  }
   102  
   103  type NothingStaged struct {
   104  	NotStagedTbls []diff.TableDelta
   105  	NotStagedDocs *diff.DocDiffs
   106  }
   107  
   108  func (ns NothingStaged) Error() string {
   109  	return "no changes added to commit"
   110  }
   111  
   112  func IsNothingStaged(err error) bool {
   113  	_, ok := err.(NothingStaged)
   114  	return ok
   115  }
   116  
   117  func NothingStagedTblDiffs(err error) []diff.TableDelta {
   118  	ns, ok := err.(NothingStaged)
   119  
   120  	if !ok {
   121  		panic("Must validate with IsCheckoutWouldOverwrite before calling CheckoutWouldOverwriteTables")
   122  	}
   123  
   124  	return ns.NotStagedTbls
   125  }
   126  
   127  func NothingStagedDocsDiffs(err error) *diff.DocDiffs {
   128  	ns, ok := err.(NothingStaged)
   129  
   130  	if !ok {
   131  		panic("Must validate with IsCheckoutWouldOverwrite before calling CheckoutWouldOverwriteTables")
   132  	}
   133  
   134  	return ns.NotStagedDocs
   135  }