github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/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  	"errors"
    19  	"fmt"
    20  )
    21  
    22  var ErrInvBranchName = errors.New("not a valid user branch name")
    23  var ErrInvWorkspaceName = errors.New("not a valid user workspace name")
    24  var ErrInvTagName = errors.New("not a valid user tag name")
    25  var ErrInvTableName = errors.New("not a valid table name")
    26  var ErrInvHash = errors.New("not a valid hash")
    27  var ErrInvalidAncestorSpec = errors.New("invalid ancestor spec")
    28  var ErrInvalidBranchOrHash = errors.New("string is not a valid branch or hash")
    29  var ErrInvalidHash = errors.New("string is not a valid hash")
    30  
    31  var ErrFoundHashNotACommit = errors.New("the value retrieved for this hash is not a commit")
    32  
    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 ErrNoConflicts = errors.New("no conflicts")
    46  var ErrUpToDate = errors.New("up to date")
    47  var ErrIsAhead = errors.New("current fast forward from a to b. a is ahead of b already")
    48  var ErrIsBehind = errors.New("cannot reverse from b to a. b is a is behind a already")
    49  
    50  var ErrUnresolvedConflicts = errors.New("merge has unresolved conflicts. please use the dolt_conflicts table to resolve")
    51  var ErrMergeActive = errors.New("merging is not possible because you have not committed an active merge")
    52  
    53  type ErrClientOutOfDate struct {
    54  	RepoVer   FeatureVersion
    55  	ClientVer FeatureVersion
    56  }
    57  
    58  func (e ErrClientOutOfDate) Error() string {
    59  	return fmt.Sprintf(`client (version: %d) is out of date and must upgrade to read this repo (version: %d).
    60  	visit https://github.com/dolthub/dolt/releases/latest/`, e.ClientVer, e.RepoVer)
    61  }
    62  
    63  func IsInvalidFormatErr(err error) bool {
    64  	switch err {
    65  	case ErrInvBranchName, ErrInvTableName, ErrInvHash, ErrInvalidAncestorSpec, ErrInvalidBranchOrHash:
    66  		return true
    67  	default:
    68  		return false
    69  	}
    70  }
    71  
    72  func IsNotFoundErr(err error) bool {
    73  	switch err {
    74  	case ErrHashNotFound, ErrBranchNotFound, ErrTableNotFound:
    75  		return true
    76  	default:
    77  		return false
    78  	}
    79  }
    80  
    81  func IsNotACommit(err error) bool {
    82  	switch err {
    83  	case ErrHashNotFound, ErrBranchNotFound, ErrFoundHashNotACommit:
    84  		return true
    85  	default:
    86  		return false
    87  	}
    88  }
    89  
    90  type RootType int
    91  
    92  const (
    93  	WorkingRoot RootType = iota
    94  	StagedRoot
    95  	CommitRoot
    96  	HeadRoot
    97  	InvalidRoot
    98  )
    99  
   100  func (rt RootType) String() string {
   101  	switch rt {
   102  	case WorkingRoot:
   103  		return "working root"
   104  	case StagedRoot:
   105  		return "staged root"
   106  	case CommitRoot:
   107  		return "root value for commit"
   108  	case HeadRoot:
   109  		return "HEAD commit root value"
   110  	}
   111  
   112  	return "unknown"
   113  }
   114  
   115  type RootTypeSet map[RootType]struct{}
   116  
   117  func NewRootTypeSet(rts ...RootType) RootTypeSet {
   118  	mp := make(map[RootType]struct{})
   119  
   120  	for _, rt := range rts {
   121  		mp[rt] = struct{}{}
   122  	}
   123  
   124  	return RootTypeSet(mp)
   125  }
   126  
   127  func (rts RootTypeSet) Contains(rt RootType) bool {
   128  	_, ok := rts[rt]
   129  	return ok
   130  }
   131  
   132  func (rts RootTypeSet) First(rtList []RootType) RootType {
   133  	for _, rt := range rtList {
   134  		if _, ok := rts[rt]; ok {
   135  			return rt
   136  		}
   137  	}
   138  
   139  	return InvalidRoot
   140  }
   141  
   142  func (rts RootTypeSet) IsEmpty() bool {
   143  	return len(rts) == 0
   144  }
   145  
   146  type RootValueUnreadable struct {
   147  	RootType RootType
   148  	Cause    error
   149  }
   150  
   151  func (rvu RootValueUnreadable) Error() string {
   152  	rs, es := rvu.RootType.String(), rvu.Cause.Error()
   153  	return fmt.Sprintf("unable to read %s: %s", rs, es)
   154  }
   155  
   156  func IsRootValUnreachable(err error) bool {
   157  	_, ok := err.(RootValueUnreadable)
   158  	return ok
   159  }
   160  
   161  func GetUnreachableRootType(err error) RootType {
   162  	rvu, ok := err.(RootValueUnreadable)
   163  
   164  	if !ok {
   165  		panic("Must validate with IsRootValUnreachable before calling GetUnreachableRootType")
   166  	}
   167  
   168  	return rvu.RootType
   169  }
   170  
   171  func GetUnreachableRootCause(err error) error {
   172  	rvu, ok := err.(RootValueUnreadable)
   173  
   174  	if !ok {
   175  		panic("Must validate with IsRootValUnreachable before calling GetUnreachableRootCause")
   176  	}
   177  
   178  	return rvu.Cause
   179  }