github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/syndtr/goleveldb/leveldb/errors/errors.go (about) 1 // Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com> 2 // All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 // Package errors provides common error types used throughout leveldb. 8 package errors 9 10 import ( 11 "errors" 12 "fmt" 13 14 "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/storage" 15 "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/util" 16 ) 17 18 var ( 19 ErrNotFound = New("leveldb: not found") 20 ErrReleased = util.ErrReleased 21 ErrHasReleaser = util.ErrHasReleaser 22 ) 23 24 // New returns an error that formats as the given text. 25 func New(text string) error { 26 return errors.New(text) 27 } 28 29 // ErrCorrupted is the type that wraps errors that indicate corruption in 30 // the database. 31 type ErrCorrupted struct { 32 Fd storage.FileDesc 33 Err error 34 } 35 36 func (e *ErrCorrupted) Error() string { 37 if !e.Fd.Nil() { 38 return fmt.Sprintf("%v [file=%v]", e.Err, e.Fd) 39 } else { 40 return e.Err.Error() 41 } 42 } 43 44 // NewErrCorrupted creates new ErrCorrupted error. 45 func NewErrCorrupted(fd storage.FileDesc, err error) error { 46 return &ErrCorrupted{fd, err} 47 } 48 49 // IsCorrupted returns a boolean indicating whether the error is indicating 50 // a corruption. 51 func IsCorrupted(err error) bool { 52 switch err.(type) { 53 case *ErrCorrupted: 54 return true 55 case *storage.ErrCorrupted: 56 return true 57 } 58 return false 59 } 60 61 // ErrMissingFiles is the type that indicating a corruption due to missing 62 // files. ErrMissingFiles always wrapped with ErrCorrupted. 63 type ErrMissingFiles struct { 64 Fds []storage.FileDesc 65 } 66 67 func (e *ErrMissingFiles) Error() string { return "file missing" } 68 69 // SetFd sets 'file info' of the given error with the given file. 70 // Currently only ErrCorrupted is supported, otherwise will do nothing. 71 func SetFd(err error, fd storage.FileDesc) error { 72 switch x := err.(type) { 73 case *ErrCorrupted: 74 x.Fd = fd 75 return x 76 } 77 return err 78 }