/* * Copyright 2017 Dgraph Labs, Inc. and Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package y // This file contains some functions for error handling. Note that we are moving // towards using x.Trace, i.e., rpc tracing using net/tracer. But for now, these // functions are useful for simple checks logged on one machine. // Some common use cases are: // (1) You receive an error from external lib, and would like to check/log fatal. // For this, use x.Check, x.Checkf. These will check for err != nil, which is // more common in Go. If you want to check for boolean being true, use // x.Assert, x.Assertf. // (2) You receive an error from external lib, and would like to pass on with some // stack trace information. In this case, use x.Wrap or x.Wrapf. // (3) You want to generate a new error with stack trace info. Use x.Errorf. import ( "fmt" "log" "github.com/pkg/errors" ) var debugMode = true // Check logs fatal if err != nil. func Check(err error) { if err != nil { log.Fatalf("%+v", Wrap(err)) } } // Check2 acts as convenience wrapper around Check, using the 2nd argument as error. func Check2(_ interface{}, err error) { Check(err) } // AssertTrue asserts that b is true. Otherwise, it would log fatal. func AssertTrue(b bool) { if !b { log.Fatalf("%+v", errors.Errorf("Assert failed")) } } // AssertTruef is AssertTrue with extra info. func AssertTruef(b bool, format string, args ...interface{}) { if !b { log.Fatalf("%+v", errors.Errorf(format, args...)) } } // Wrap wraps errors from external lib. func Wrap(err error) error { if !debugMode { return err } return errors.Wrap(err, "") } // Wrapf is Wrap with extra info. func Wrapf(err error, format string, args ...interface{}) error { if !debugMode { if err == nil { return nil } return fmt.Errorf(format+" error: %+v", append(args, err)...) } return errors.Wrapf(err, format, args...) }