OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / syndtr / goleveldb / leveldb / errors / errors.go
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/syndtr/goleveldb/leveldb/storage"
15         "github.com/syndtr/goleveldb/leveldb/util"
16 )
17
18 // Common errors.
19 var (
20         ErrNotFound    = New("leveldb: not found")
21         ErrReleased    = util.ErrReleased
22         ErrHasReleaser = util.ErrHasReleaser
23 )
24
25 // New returns an error that formats as the given text.
26 func New(text string) error {
27         return errors.New(text)
28 }
29
30 // ErrCorrupted is the type that wraps errors that indicate corruption in
31 // the database.
32 type ErrCorrupted struct {
33         Fd  storage.FileDesc
34         Err error
35 }
36
37 func (e *ErrCorrupted) Error() string {
38         if !e.Fd.Zero() {
39                 return fmt.Sprintf("%v [file=%v]", e.Err, e.Fd)
40         }
41         return e.Err.Error()
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 }