error.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. package y
  17. // This file contains some functions for error handling. Note that we are moving
  18. // towards using x.Trace, i.e., rpc tracing using net/tracer. But for now, these
  19. // functions are useful for simple checks logged on one machine.
  20. // Some common use cases are:
  21. // (1) You receive an error from external lib, and would like to check/log fatal.
  22. // For this, use x.Check, x.Checkf. These will check for err != nil, which is
  23. // more common in Go. If you want to check for boolean being true, use
  24. // x.Assert, x.Assertf.
  25. // (2) You receive an error from external lib, and would like to pass on with some
  26. // stack trace information. In this case, use x.Wrap or x.Wrapf.
  27. // (3) You want to generate a new error with stack trace info. Use x.Errorf.
  28. import (
  29. "fmt"
  30. "log"
  31. "github.com/pkg/errors"
  32. )
  33. var debugMode = false
  34. // Check logs fatal if err != nil.
  35. func Check(err error) {
  36. if err != nil {
  37. log.Fatalf("%+v", Wrap(err, ""))
  38. }
  39. }
  40. // Check2 acts as convenience wrapper around Check, using the 2nd argument as error.
  41. func Check2(_ interface{}, err error) {
  42. Check(err)
  43. }
  44. // AssertTrue asserts that b is true. Otherwise, it would log fatal.
  45. func AssertTrue(b bool) {
  46. if !b {
  47. log.Fatalf("%+v", errors.Errorf("Assert failed"))
  48. }
  49. }
  50. // AssertTruef is AssertTrue with extra info.
  51. func AssertTruef(b bool, format string, args ...interface{}) {
  52. if !b {
  53. log.Fatalf("%+v", errors.Errorf(format, args...))
  54. }
  55. }
  56. // Wrap wraps errors from external lib.
  57. func Wrap(err error, msg string) error {
  58. if !debugMode {
  59. if err == nil {
  60. return nil
  61. }
  62. return fmt.Errorf("%s err: %+v", msg, err)
  63. }
  64. return errors.Wrap(err, msg)
  65. }
  66. // Wrapf is Wrap with extra info.
  67. func Wrapf(err error, format string, args ...interface{}) error {
  68. if !debugMode {
  69. if err == nil {
  70. return nil
  71. }
  72. return fmt.Errorf(format+" error: %+v", append(args, err)...)
  73. }
  74. return errors.Wrapf(err, format, args...)
  75. }
  76. func CombineErrors(one, other error) error {
  77. if one != nil && other != nil {
  78. return fmt.Errorf("%v; %v", one, other)
  79. }
  80. if one != nil && other == nil {
  81. return fmt.Errorf("%v", one)
  82. }
  83. if one == nil && other != nil {
  84. return fmt.Errorf("%v", other)
  85. }
  86. return nil
  87. }