github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/errors/errors_test.go (about)

     1  /*
     2   Copyright Digital Asset Holdings, LLC 2016 All Rights Reserved.
     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 errors
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/hyperledger/fabric/common/flogging"
    25  )
    26  
    27  func TestError(t *testing.T) {
    28  	e := Error("Utility", "ErrorWithArg", "An unknown error occurred.")
    29  	s := e.GetStack()
    30  	if s != "" {
    31  		t.Fatalf("No error stack should have been recorded.")
    32  	}
    33  }
    34  
    35  // TestErrorWithArg tests creating an error with a message argument
    36  func TestErrorWithArg(t *testing.T) {
    37  	e := Error("Utility", "ErrorWithArg", "An error occurred: %s", "arg1")
    38  	s := e.GetStack()
    39  	if s != "" {
    40  		t.Fatalf("No error stack should have been recorded.")
    41  	}
    42  }
    43  
    44  func TestErrorWithCallstack(t *testing.T) {
    45  	e := ErrorWithCallstack("Utility", "UnknownError", "An unknown error occurred.")
    46  	s := e.GetStack()
    47  	if s == "" {
    48  		t.Fatalf("No error stack was recorded.")
    49  	}
    50  }
    51  
    52  // TestErrorWithCallstackAndArg tests creating an error with a callstack and
    53  // message argument
    54  func TestErrorWithCallstackAndArg(t *testing.T) {
    55  	e := ErrorWithCallstack("Utility", "ErrorWithArg", "An error occurred: %s", "arg1")
    56  	s := e.GetStack()
    57  	if s == "" {
    58  		t.Fatalf("No error stack was recorded.")
    59  	}
    60  }
    61  
    62  // TestErrorWithCallstackMessage tests the output for a logging error where
    63  // and an invalid log level has been provided and the stack trace should be
    64  // displayed with the error message
    65  func TestErrorWithCallstackMessage(t *testing.T) {
    66  	// when the 'error' module is set to debug, the callstack will be appended
    67  	// to the error message
    68  	flogging.SetModuleLevel("error", "debug")
    69  
    70  	e := ErrorWithCallstack("Utility", "ErrorWithArg", "An unknown error occurred.")
    71  	s := e.GetStack()
    72  	if s == "" {
    73  		t.Fatalf("No error stack was recorded.")
    74  	}
    75  
    76  	// check that the error message contains this part of the stack trace, which
    77  	// is non-platform specific
    78  	if !strings.Contains(e.Error(), "github.com/hyperledger/fabric/core/errors.TestErrorWithCallstackMessage") {
    79  		t.Fatalf("Error message does not have stack trace appended.")
    80  	}
    81  }
    82  
    83  func ExampleError() {
    84  	// when the 'error' module is set to anything but debug, the callstack will
    85  	// not be appended to the error message
    86  	flogging.SetModuleLevel("error", "warning")
    87  
    88  	err := Error("Utility", "UnknownError", "An unknown error occurred.")
    89  
    90  	if err != nil {
    91  		fmt.Printf("%s\n", err.Error())
    92  		fmt.Printf("%s\n", err.GetErrorCode())
    93  		fmt.Printf("%s\n", err.GetComponentCode())
    94  		fmt.Printf("%s\n", err.GetReasonCode())
    95  		fmt.Printf("%s\n", err.Message())
    96  		// Output:
    97  		// UTILITY_UNKNOWNERROR - An unknown error occurred.
    98  		// UTILITY_UNKNOWNERROR
    99  		// UTILITY
   100  		// UNKNOWNERROR
   101  		// UTILITY_UNKNOWNERROR - An unknown error occurred.
   102  	}
   103  }
   104  
   105  func ExampleError_blankParameters() {
   106  	// when the 'error' module is set to anything but debug, the callstack will
   107  	// not be appended to the error message
   108  	flogging.SetModuleLevel("error", "warning")
   109  
   110  	// create error with blank strings for the component code, reason code, and
   111  	// message text. the code should use the default for each value instead of
   112  	// using the blank strings
   113  	err := Error("", "", "")
   114  
   115  	if err != nil {
   116  		fmt.Printf("%s\n", err.Error())
   117  		fmt.Printf("%s\n", err.GetErrorCode())
   118  		fmt.Printf("%s\n", err.GetComponentCode())
   119  		fmt.Printf("%s\n", err.GetReasonCode())
   120  		fmt.Printf("%s\n", err.Message())
   121  		// Output:
   122  		// UTILITY_UNKNOWNERROR - An unknown error occurred.
   123  		// UTILITY_UNKNOWNERROR
   124  		// UTILITY
   125  		// UNKNOWNERROR
   126  		// UTILITY_UNKNOWNERROR - An unknown error occurred.
   127  	}
   128  }
   129  
   130  func ExampleErrorWithCallstack() {
   131  	// when the 'error' module is set to anything but debug, the callstack will
   132  	// not be appended to the error message
   133  	flogging.SetModuleLevel("error", "warning")
   134  
   135  	err := ErrorWithCallstack("Utility", "UnknownError", "An unknown error occurred.")
   136  
   137  	if err != nil {
   138  		fmt.Printf("%s\n", err.Error())
   139  		fmt.Printf("%s\n", err.GetErrorCode())
   140  		fmt.Printf("%s\n", err.GetComponentCode())
   141  		fmt.Printf("%s\n", err.GetReasonCode())
   142  		fmt.Printf("%s\n", err.Message())
   143  		// Output:
   144  		// UTILITY_UNKNOWNERROR - An unknown error occurred.
   145  		// UTILITY_UNKNOWNERROR
   146  		// UTILITY
   147  		// UNKNOWNERROR
   148  		// UTILITY_UNKNOWNERROR - An unknown error occurred.
   149  	}
   150  }
   151  
   152  // ExampleErrorWithArg tests the output for a sample error with a message
   153  // argument
   154  func Example_utilityErrorWithArg() {
   155  	// when the 'error' module is set to anything but debug, the callstack will
   156  	// not be appended to the error message
   157  	flogging.SetModuleLevel("error", "warning")
   158  
   159  	err := ErrorWithCallstack("Utility", "ErrorWithArg", "An error occurred: %s", "arg1")
   160  
   161  	if err != nil {
   162  		fmt.Printf("%s\n", err.Error())
   163  		fmt.Printf("%s\n", err.GetErrorCode())
   164  		fmt.Printf("%s\n", err.GetComponentCode())
   165  		fmt.Printf("%s\n", err.GetReasonCode())
   166  		fmt.Printf("%s\n", err.Message())
   167  		// Output:
   168  		// UTILITY_ERRORWITHARG - An error occurred: arg1
   169  		// UTILITY_ERRORWITHARG
   170  		// UTILITY
   171  		// ERRORWITHARG
   172  		// UTILITY_ERRORWITHARG - An error occurred: arg1
   173  	}
   174  }
   175  
   176  // Example_loggingInvalidLevel tests the output for a logging error where
   177  // and an invalid log level has been provided
   178  func Example_loggingInvalidLevel() {
   179  	// when the 'error' module is set to anything but debug, the callstack will
   180  	// not be appended to the error message
   181  	flogging.SetModuleLevel("error", "warning")
   182  
   183  	err := ErrorWithCallstack("Logging", "InvalidLevel", "Invalid log level provided - %s", "invalid")
   184  
   185  	if err != nil {
   186  		fmt.Printf("%s\n", err.Error())
   187  		fmt.Printf("%s\n", err.GetErrorCode())
   188  		fmt.Printf("%s\n", err.GetComponentCode())
   189  		fmt.Printf("%s\n", err.GetReasonCode())
   190  		fmt.Printf("%s\n", err.Message())
   191  		// Output:
   192  		// LOGGING_INVALIDLEVEL - Invalid log level provided - invalid
   193  		// LOGGING_INVALIDLEVEL
   194  		// LOGGING
   195  		// INVALIDLEVEL
   196  		// LOGGING_INVALIDLEVEL - Invalid log level provided - invalid
   197  	}
   198  }