github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/tlfhandle/errors.go (about)

     1  // Copyright 2019 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package tlfhandle
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/keybase/client/go/kbfs/kbfsmd"
    11  	"github.com/keybase/client/go/kbfs/tlf"
    12  	kbname "github.com/keybase/client/go/kbun"
    13  )
    14  
    15  // NoSuchNameError indicates that the user tried to access a TLF that
    16  // doesn't exist.
    17  type NoSuchNameError struct {
    18  	Name string
    19  }
    20  
    21  // Error implements the error interface for NoSuchNameError
    22  func (e NoSuchNameError) Error() string {
    23  	return fmt.Sprintf("%s doesn't exist", e.Name)
    24  }
    25  
    26  // HandleFinalizedError is returned when something attempts to modify
    27  // a finalized TLF handle.
    28  type HandleFinalizedError struct {
    29  }
    30  
    31  // Error implements the error interface for HandleFinalizedError.
    32  func (e HandleFinalizedError) Error() string {
    33  	return "Attempt to modify finalized TLF handle"
    34  }
    35  
    36  // HandleMismatchError indicates an inconsistent or unverifiable MD object
    37  // for the given top-level folder.
    38  type HandleMismatchError struct {
    39  	Revision kbfsmd.Revision
    40  	Dir      string
    41  	TlfID    tlf.ID
    42  	Err      error
    43  }
    44  
    45  // Error implements the error interface for HandleMismatchError
    46  func (e HandleMismatchError) Error() string {
    47  	return fmt.Sprintf("Could not verify metadata (revision=%d) for directory %s (id=%s): %s",
    48  		e.Revision, e.Dir, e.TlfID, e.Err)
    49  }
    50  
    51  // ReadAccessError indicates that the user tried to read from a
    52  // top-level folder without read permission.
    53  type ReadAccessError struct {
    54  	User     kbname.NormalizedUsername
    55  	Filename string
    56  	Tlf      tlf.CanonicalName
    57  	Type     tlf.Type
    58  }
    59  
    60  // Error implements the error interface for ReadAccessError
    61  func (e ReadAccessError) Error() string {
    62  	return fmt.Sprintf("%s does not have read access to directory %s",
    63  		e.User, BuildCanonicalPathForTlfName(e.Type, e.Tlf))
    64  }
    65  
    66  // NewReadAccessError constructs a ReadAccessError for the given
    67  // directory and user.
    68  func NewReadAccessError(h *Handle, username kbname.NormalizedUsername, filename string) error {
    69  	tlfname := h.GetCanonicalName()
    70  	return ReadAccessError{
    71  		User:     username,
    72  		Filename: filename,
    73  		Tlf:      tlfname,
    74  		Type:     h.Type(),
    75  	}
    76  }
    77  
    78  // WriteAccessError indicates an error when trying to write a file
    79  type WriteAccessError struct {
    80  	User     kbname.NormalizedUsername
    81  	Filename string
    82  	Tlf      tlf.CanonicalName
    83  	Type     tlf.Type
    84  }
    85  
    86  // Error implements the error interface for WriteAccessError
    87  func (e WriteAccessError) Error() string {
    88  	if e.Tlf != "" {
    89  		return fmt.Sprintf("%s does not have write access to directory %s",
    90  			e.User, BuildCanonicalPathForTlfName(e.Type, e.Tlf))
    91  	}
    92  	return fmt.Sprintf("%s does not have write access to %s", e.User, e.Filename)
    93  }
    94  
    95  // NewWriteAccessError is an access error trying to write a file
    96  func NewWriteAccessError(h *Handle, username kbname.NormalizedUsername, filename string) error {
    97  	tlfName := tlf.CanonicalName("")
    98  	t := tlf.Private
    99  	if h != nil {
   100  		tlfName = h.GetCanonicalName()
   101  		t = h.Type()
   102  	}
   103  	return WriteAccessError{
   104  		User:     username,
   105  		Filename: filename,
   106  		Tlf:      tlfName,
   107  		Type:     t,
   108  	}
   109  }