github.com/coocood/badger@v1.5.1-0.20200528065104-c02ac3616d04/y/error.go (about)

     1  /*
     2   * Copyright 2017 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package y
    18  
    19  // This file contains some functions for error handling. Note that we are moving
    20  // towards using x.Trace, i.e., rpc tracing using net/tracer. But for now, these
    21  // functions are useful for simple checks logged on one machine.
    22  // Some common use cases are:
    23  // (1) You receive an error from external lib, and would like to check/log fatal.
    24  //     For this, use x.Check, x.Checkf. These will check for err != nil, which is
    25  //     more common in Go. If you want to check for boolean being true, use
    26  //		   x.Assert, x.Assertf.
    27  // (2) You receive an error from external lib, and would like to pass on with some
    28  //     stack trace information. In this case, use x.Wrap or x.Wrapf.
    29  // (3) You want to generate a new error with stack trace info. Use x.Errorf.
    30  
    31  import (
    32  	"fmt"
    33  	"log"
    34  
    35  	"github.com/pingcap/errors"
    36  )
    37  
    38  var debugMode = true
    39  
    40  // Check logs fatal if err != nil.
    41  func Check(err error) {
    42  	if err != nil {
    43  		log.Fatalf("%+v", Wrap(err))
    44  	}
    45  }
    46  
    47  // Check2 acts as convenience wrapper around Check, using the 2nd argument as error.
    48  func Check2(_ interface{}, err error) {
    49  	Check(err)
    50  }
    51  
    52  // AssertTrue asserts that b is true. Otherwise, it would log fatal.
    53  func Assert(b bool) {
    54  	if !b {
    55  		log.Fatalf("%+v", errors.Errorf("Assert failed"))
    56  	}
    57  }
    58  
    59  // AssertTruef is AssertTrue with extra info.
    60  func AssertTruef(b bool, format string, args ...interface{}) {
    61  	if !b {
    62  		log.Fatalf("%+v", errors.Errorf(format, args...))
    63  	}
    64  }
    65  
    66  // Wrap wraps errors from external lib.
    67  func Wrap(err error) error {
    68  	if !debugMode {
    69  		return err
    70  	}
    71  	return errors.Wrap(err, "")
    72  }
    73  
    74  // Wrapf is Wrap with extra info.
    75  func Wrapf(err error, format string, args ...interface{}) error {
    76  	if !debugMode {
    77  		if err == nil {
    78  			return nil
    79  		}
    80  		return fmt.Errorf(format+" error: %+v", append(args, err)...)
    81  	}
    82  	return errors.Wrapf(err, format, args...)
    83  }