github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/doltdb/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 doltdb
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"fmt"
    21  )
    22  
    23  var ErrInvBranchName = errors.New("not a valid user branch name")
    24  var ErrInvWorkspaceName = errors.New("not a valid user workspace name")
    25  var ErrInvTagName = errors.New("not a valid user tag name")
    26  var ErrInvTableName = errors.New("not a valid table name")
    27  var ErrInvHash = errors.New("not a valid hash")
    28  var ErrInvalidAncestorSpec = errors.New("invalid ancestor spec")
    29  var ErrInvalidBranchOrHash = errors.New("string is not a valid branch or hash")
    30  var ErrInvalidHash = errors.New("string is not a valid hash")
    31  
    32  var ErrFoundHashNotACommit = errors.New("the value retrieved for this hash is not a commit")
    33  var ErrHashNotFound = errors.New("could not find a value for this hash")
    34  var ErrBranchNotFound = errors.New("branch not found")
    35  var ErrTagNotFound = errors.New("tag not found")
    36  var ErrWorkingSetNotFound = errors.New("working set not found")
    37  var ErrWorkspaceNotFound = errors.New("workspace not found")
    38  var ErrTableNotFound = errors.New("table not found")
    39  var ErrTableExists = errors.New("table already exists")
    40  var ErrAlreadyOnBranch = errors.New("Already on branch")
    41  var ErrAlreadyOnWorkspace = errors.New("Already on workspace")
    42  
    43  var ErrNomsIO = errors.New("error reading from or writing to noms")
    44  
    45  var ErrUpToDate = errors.New("Everything up-to-date")
    46  var ErrIsAhead = errors.New("cannot fast forward from a to b. a is ahead of b already")
    47  var ErrIsBehind = errors.New("cannot reverse from b to a. b is a is behind a already")
    48  
    49  var ErrUnresolvedConflictsOrViolations = errors.New("merge has unresolved conflicts or constraint violations")
    50  var ErrMergeActive = errors.New("merging is not possible because you have not committed an active merge")
    51  
    52  var ErrOperationNotSupportedInDetachedHead = errors.New("this operation is not supported while in a detached head state")
    53  
    54  type ErrClientOutOfDate struct {
    55  	RepoVer   FeatureVersion
    56  	ClientVer FeatureVersion
    57  }
    58  
    59  func (e ErrClientOutOfDate) Error() string {
    60  	return fmt.Sprintf(`client (version: %d) is out of date and must upgrade to read this repo (version: %d).
    61  	visit https://github.com/dolthub/dolt/releases/latest/`, e.ClientVer, e.RepoVer)
    62  }
    63  
    64  func IsInvalidFormatErr(err error) bool {
    65  	switch err {
    66  	case ErrInvBranchName, ErrInvTableName, ErrInvHash, ErrInvalidAncestorSpec, ErrInvalidBranchOrHash:
    67  		return true
    68  	default:
    69  		return false
    70  	}
    71  }
    72  
    73  func IsNotFoundErr(err error) bool {
    74  	return errors.Is(err, ErrHashNotFound) ||
    75  		errors.Is(err, ErrBranchNotFound) ||
    76  		errors.Is(err, ErrTableNotFound)
    77  }
    78  
    79  func IsNotACommit(err error) bool {
    80  	return errors.Is(err, ErrHashNotFound) ||
    81  		errors.Is(err, ErrBranchNotFound) ||
    82  		errors.Is(err, ErrFoundHashNotACommit)
    83  }
    84  
    85  type RootType int
    86  
    87  const (
    88  	WorkingRoot RootType = iota
    89  	StagedRoot
    90  	CommitRoot
    91  	HeadRoot
    92  	InvalidRoot
    93  )
    94  
    95  func (rt RootType) String() string {
    96  	switch rt {
    97  	case WorkingRoot:
    98  		return "working root"
    99  	case StagedRoot:
   100  		return "staged root"
   101  	case CommitRoot:
   102  		return "root value for commit"
   103  	case HeadRoot:
   104  		return "HEAD commit root value"
   105  	}
   106  
   107  	return "unknown"
   108  }
   109  
   110  type RootTypeSet map[RootType]struct{}
   111  
   112  func NewRootTypeSet(rts ...RootType) RootTypeSet {
   113  	mp := make(map[RootType]struct{})
   114  
   115  	for _, rt := range rts {
   116  		mp[rt] = struct{}{}
   117  	}
   118  
   119  	return RootTypeSet(mp)
   120  }
   121  
   122  func (rts RootTypeSet) Contains(rt RootType) bool {
   123  	_, ok := rts[rt]
   124  	return ok
   125  }
   126  
   127  func (rts RootTypeSet) First(rtList []RootType) RootType {
   128  	for _, rt := range rtList {
   129  		if _, ok := rts[rt]; ok {
   130  			return rt
   131  		}
   132  	}
   133  
   134  	return InvalidRoot
   135  }
   136  
   137  func (rts RootTypeSet) IsEmpty() bool {
   138  	return len(rts) == 0
   139  }
   140  
   141  type RootValueUnreadable struct {
   142  	RootType RootType
   143  	Cause    error
   144  }
   145  
   146  func (rvu RootValueUnreadable) Error() string {
   147  	rs, es := rvu.RootType.String(), rvu.Cause.Error()
   148  	return fmt.Sprintf("unable to read %s: %s", rs, es)
   149  }
   150  
   151  func IsRootValUnreachable(err error) bool {
   152  	_, ok := err.(RootValueUnreadable)
   153  	return ok
   154  }
   155  
   156  func GetUnreachableRootType(err error) RootType {
   157  	rvu, ok := err.(RootValueUnreadable)
   158  
   159  	if !ok {
   160  		panic("Must validate with IsRootValUnreachable before calling GetUnreachableRootType")
   161  	}
   162  
   163  	return rvu.RootType
   164  }
   165  
   166  func GetUnreachableRootCause(err error) error {
   167  	rvu, ok := err.(RootValueUnreadable)
   168  
   169  	if !ok {
   170  		panic("Must validate with IsRootValUnreachable before calling GetUnreachableRootCause")
   171  	}
   172  
   173  	return rvu.Cause
   174  }
   175  
   176  // DoltIgnoreConflictError is an error that is returned when the user attempts to stage a table that matches conflicting dolt_ignore patterns
   177  type DoltIgnoreConflictError struct {
   178  	Table         string
   179  	TruePatterns  []string
   180  	FalsePatterns []string
   181  }
   182  
   183  func (dc DoltIgnoreConflictError) Error() string {
   184  	var buffer bytes.Buffer
   185  	buffer.WriteString("the table ")
   186  	buffer.WriteString(dc.Table)
   187  	buffer.WriteString(" matches conflicting patterns in dolt_ignore:")
   188  
   189  	for _, pattern := range dc.TruePatterns {
   190  		buffer.WriteString("\nignored:     ")
   191  		buffer.WriteString(pattern)
   192  	}
   193  
   194  	for _, pattern := range dc.FalsePatterns {
   195  		buffer.WriteString("\nnot ignored: ")
   196  		buffer.WriteString(pattern)
   197  	}
   198  
   199  	return buffer.String()
   200  }
   201  
   202  func AsDoltIgnoreInConflict(err error) *DoltIgnoreConflictError {
   203  	di, ok := err.(DoltIgnoreConflictError)
   204  	if ok {
   205  		return &di
   206  	}
   207  	return nil
   208  }