Line data Source code
1 : // Copyright 2011 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 : // of this source code is governed by a BSD-style license that can be found in 3 : // the LICENSE file. 4 : 5 : package base 6 : 7 : import ( 8 : "github.com/cockroachdb/errors" 9 : "github.com/cockroachdb/pebble/internal/invariants" 10 : ) 11 : 12 : // ErrNotFound means that a get or delete call did not find the requested key. 13 : var ErrNotFound = errors.New("pebble: not found") 14 : 15 : // ErrCorruption is a marker to indicate that data in a file (WAL, MANIFEST, 16 : // sstable) isn't in the expected format. 17 : var ErrCorruption = errors.New("pebble: corruption") 18 : 19 : // MarkCorruptionError marks given error as a corruption error. 20 2 : func MarkCorruptionError(err error) error { 21 2 : if errors.Is(err, ErrCorruption) { 22 1 : return err 23 1 : } 24 2 : return errors.Mark(err, ErrCorruption) 25 : } 26 : 27 : // CorruptionErrorf formats according to a format specifier and returns 28 : // the string as an error value that is marked as a corruption error. 29 2 : func CorruptionErrorf(format string, args ...interface{}) error { 30 2 : return errors.Mark(errors.Newf(format, args...), ErrCorruption) 31 2 : } 32 : 33 : // AssertionFailedf creates an assertion error and panics in invariants.Enabled 34 : // builds. It should only be used when it indicates a bug. 35 1 : func AssertionFailedf(format string, args ...interface{}) error { 36 1 : err := errors.AssertionFailedf(format, args...) 37 1 : if invariants.Enabled { 38 1 : panic(err) 39 : } 40 0 : return err 41 : } 42 : 43 : // CatchErrorPanic runs a function and catches any panic that contains an 44 : // error, returning that error. Used in tests, in particular to catch panics 45 : // threw by AssertionFailedf. 46 1 : func CatchErrorPanic(f func() error) (err error) { 47 1 : defer func() { 48 1 : if r := recover(); r != nil { 49 1 : if e, ok := r.(error); ok { 50 1 : err = e 51 1 : } else { 52 0 : panic(r) 53 : } 54 : } 55 : }() 56 1 : return f() 57 : }