Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update error handling #45

Merged
merged 9 commits into from
May 21, 2023
Prev Previous commit
Next Next commit
update error handling in index.go (#43)
  • Loading branch information
JunNishimura committed May 21, 2023
commit 04ba016389aa6f22f2b85ce6b56008aae0d894db
18 changes: 9 additions & 9 deletions store/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewIndex(rootGoitPath string) (*Index, error) {
indexPath = filepath.Join(rootGoitPath, "index")
if _, err := os.Stat(indexPath); !os.IsNotExist(err) {
if err := index.read(); err != nil {
return nil, fmt.Errorf("fail to read index: %v", err)
return nil, fmt.Errorf("fail to read index: %w", err)
}
}
return index, nil
Expand Down Expand Up @@ -142,7 +142,7 @@ func (idx *Index) read() error {
// read index
b, err := ioutil.ReadFile(indexPath)
if err != nil {
return fmt.Errorf("fail to read index: %v", err)
return fmt.Errorf("fail to read index: %w", err)
}

// make bytes reader
Expand All @@ -151,7 +151,7 @@ func (idx *Index) read() error {
// fixed length decoding
err = binary.Read(buf, binary.BigEndian, &idx.Header)
if err != nil {
return fmt.Errorf("fail to read index header: %v", err)
return fmt.Errorf("fail to read index header: %w", err)
}

// variable length decoding
Expand All @@ -160,21 +160,21 @@ func (idx *Index) read() error {
hash := make(sha.SHA1, 20)
err = binary.Read(buf, binary.BigEndian, &hash)
if err != nil {
return fmt.Errorf("fail to read hash from index: %v", err)
return fmt.Errorf("fail to read hash from index: %w", err)
}

// read file name length
var nameLength uint16
err = binary.Read(buf, binary.BigEndian, &nameLength)
if err != nil {
return fmt.Errorf("fail to read file name length from index: %v", err)
return fmt.Errorf("fail to read file name length from index: %w", err)
}

// read file path
path := make([]byte, nameLength)
err = binary.Read(buf, binary.BigEndian, &path)
if err != nil {
return fmt.Errorf("fail to read path from index: %v", err)
return fmt.Errorf("fail to read path from index: %w", err)
}

entry := NewEntry(hash, path)
Expand All @@ -187,13 +187,13 @@ func (idx *Index) read() error {
func (idx *Index) write() error {
f, err := os.Create(indexPath)
if err != nil {
return fmt.Errorf("fail to create .goit/index: %v", err)
return fmt.Errorf("fail to create .goit/index: %w", err)
}
defer f.Close()

// fixed length encoding
if err := binary.Write(f, binary.BigEndian, &idx.Header); err != nil {
return fmt.Errorf("fail to write fixed-length encoding: %v", err)
return fmt.Errorf("fail to write fixed-length encoding: %w", err)
}

// variable length encoding
Expand All @@ -206,7 +206,7 @@ func (idx *Index) write() error {
data = append(data, entry.Path...)
}
if _, err := f.Write(data); err != nil {
return fmt.Errorf("fail to write variable-length encoding: %v", err)
return fmt.Errorf("fail to write variable-length encoding: %w", err)
}

return nil
Expand Down