github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb.chai2010/error.go (about)

     1  // Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package leveldb
     6  
     7  const (
     8  	errNil = iota
     9  	errInvalidArgument
    10  	errNotFound
    11  	errCorruption
    12  	errIOError
    13  	errUnknown
    14  )
    15  
    16  // Error represents an LevelDB error.
    17  type Error struct {
    18  	code int
    19  	err  string
    20  }
    21  
    22  func newError(code int, err string) *Error {
    23  	return &Error{code: code, err: err}
    24  }
    25  
    26  func (e *Error) InvalidArgument() bool {
    27  	return e.code == errInvalidArgument
    28  }
    29  
    30  func (e *Error) IsNotFound() bool {
    31  	return e.code == errNotFound
    32  }
    33  
    34  func (e *Error) IsCorruption() bool {
    35  	return e.code == errCorruption
    36  }
    37  
    38  func (e *Error) IsIOError() bool {
    39  	return e.code == errIOError
    40  }
    41  
    42  func (e *Error) IsUnknown() bool {
    43  	return e.code == errUnknown
    44  }
    45  
    46  func (e *Error) Error() string {
    47  	if e == nil || e.code == errNil {
    48  		return "leveldb: <nil>"
    49  	}
    50  	switch e.code {
    51  	case errInvalidArgument:
    52  		return "leveldb: Invalid Argument"
    53  	case errNotFound:
    54  		return "leveldb: Not Found"
    55  	case errCorruption:
    56  		return "leveldb: Corruption"
    57  	case errIOError:
    58  		return "leveldb: IO Error"
    59  	}
    60  	return "leveldb: " + e.err
    61  }