github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/x/error.go (about)

     1  /*
     2   * Copyright 2016-2018 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 x
    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 errors.Wrapf.
    29  // (3) You want to generate a new error with stack trace info. Use errors.Errorf.
    30  
    31  import (
    32  	"fmt"
    33  	"log"
    34  	"os"
    35  
    36  	"github.com/pkg/errors"
    37  )
    38  
    39  // Check logs fatal if err != nil.
    40  func Check(err error) {
    41  	if err != nil {
    42  		log.Fatalf("%+v", errors.Wrap(err, ""))
    43  	}
    44  }
    45  
    46  // Checkf is Check with extra info.
    47  func Checkf(err error, format string, args ...interface{}) {
    48  	if err != nil {
    49  		log.Fatalf("%+v", errors.Wrapf(err, format, args...))
    50  	}
    51  }
    52  
    53  // CheckfNoTrace is Checkf without a stack trace.
    54  func CheckfNoTrace(err error) {
    55  	if err != nil {
    56  		log.Fatalf(err.Error())
    57  	}
    58  }
    59  
    60  // CheckfNoLog exits on error without any message (to avoid duplicate error messages).
    61  func CheckfNoLog(err error) {
    62  	if err != nil {
    63  		os.Exit(1)
    64  	}
    65  }
    66  
    67  // Check2 acts as convenience wrapper around Check, using the 2nd argument as error.
    68  func Check2(_ interface{}, err error) {
    69  	Check(err)
    70  }
    71  
    72  // Ignore function is used to ignore errors deliberately, while keeping the
    73  // linter happy.
    74  func Ignore(_ error) {
    75  	// Do nothing.
    76  }
    77  
    78  // AssertTrue asserts that b is true. Otherwise, it would log fatal.
    79  func AssertTrue(b bool) {
    80  	if !b {
    81  		log.Fatalf("%+v", errors.Errorf("Assert failed"))
    82  	}
    83  }
    84  
    85  // AssertTruef is AssertTrue with extra info.
    86  func AssertTruef(b bool, format string, args ...interface{}) {
    87  	if !b {
    88  		log.Fatalf("%+v", errors.Errorf(format, args...))
    89  	}
    90  }
    91  
    92  // AssertTruefNoTrace is AssertTruef without a stack trace.
    93  func AssertTruefNoTrace(b bool, format string, args ...interface{}) {
    94  	if !b {
    95  		log.Fatalf("%+v", fmt.Errorf(format, args...))
    96  	}
    97  }
    98  
    99  // Fatalf logs fatal.
   100  func Fatalf(format string, args ...interface{}) {
   101  	log.Fatalf("%+v", errors.Errorf(format, args...))
   102  }