Skip to content
Snippets Groups Projects
Select Git revision
  • 8760ec5098c13d4f6e22a3b5f34104a8bd27ff63
  • main default protected
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
22 results

errors_test.go

Blame
  • errors_test.go 3.96 KiB
    package errors_test
    
    import (
    	"encoding/json"
    	"fmt"
    	"testing"
    
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
    )
    
    func TestErrorFormatting(t *testing.T) {
    	// pretend this is your error stack
    	// the original error is created by 3rd party library e.g. pg or mysql and may not have stack info
    	// but from that point on, we use our errors package which includes stack info...
    	e1 := errors.Errorf("you have problem in your SQL near xxx")
    	e2 := errors.Wrapf(e1, "query failed")
    	e3 := errors.Wrapf(e2, "failed to find account")
    	e4 := errors.Wrapf(e3, "login failed").(*errors.CustomError)
    
    	// we can log the error in different ways:
    
    	// use %s for a simple message
    	// -> "login failed"
    	t.Logf("%%s: %s", e4)
    
    	// use either Error() or "%v" for concatenated message:
    	// -> "login failed, because failed to find account, because query failed, because you have problem in your SQL near xxx"
    	t.Logf("Error(): %s", e4.Error())
    	t.Logf("%%v: %v", e4)
    
    	// use %+v for complete error with stack:
    	// -> "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors_test/TestErrorFormatting():errors_test.go(17): login failed, because gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors_test/TestErrorFormatting():errors_test.go(16): failed to find account, because gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors_test/TestErrorFormatting():errors_test.go(15): query failed, because you have problem in your SQL near xxx
    	t.Logf("%%+v: %+v", e4)
    
    	// you may also JSON marshal the error
    	// ->
    	jsonError, _ := json.Marshal(e4.Description())
    	t.Logf("json: %s", string(jsonError))
    
    	// using the Description(), one can also verify that the correct
    	// source references are reported
    	desc := e4.Description()
    	if desc.Source == nil || desc.Source.Line != 18 {
    		t.Fatalf("failed not on line 18")
    	}
    	desc = *desc.Cause
    	if desc.Source == nil || desc.Source.Line != 17 {
    		t.Fatalf("failed not on line 17")
    	}
    	desc = *desc.Cause
    	if desc.Source == nil || desc.Source.Line != 16 {
    		t.Fatalf("failed not on line 16")
    	}
    }
    
    func TestErrorStack(t *testing.T) {
    	// if some function (e.g. 3rd party) fail without stack, then store the calling stack and append as it is wrapped from there...
    	err := a(1)
    	t.Logf("err: %+v", err)
    }
    
    func a(i int) error {
    	return errors.Wrapf(b(i), "b failed")
    }
    
    func b(i int) error {
    	return errors.Wrapf(c(i), "c failed")
    }
    
    func c(i int) error {