Skip to content
Snippets Groups Projects
Select Git revision
  • 9f7e8890386c08213a5ef382ca5d8cbc649baa99
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • 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
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

http_error_test.go

Blame
  • http_error_test.go 1.68 KiB
    package errors_test
    
    import (
    	"net/http"
    	"testing"
    
    	"gitlab.com/uafrica/go-utils/errors"
    )
    
    func TestHTTPError(t *testing.T) {
    	var err error
    
    	//you can wrap any error with an HTTP code:
    	err = errors.Errorf("failed to connect to db")
    	//err = errors.HTTP(http.StatusInternalServerError, err)
    
    	//or if you know you are creating the HTTP error, do it as one statement
    	err = errors.HTTP(http.StatusBadRequest, err, "failed to get user")
    
    	//and one more to give a lower code again
    	err = errors.HTTP(http.StatusInsufficientStorage, err, "jissis this is bad!")
    
    	//and higher again -... many layers...
    	err = errors.HTTP(http.StatusNotFound, err, "terrible mistake")
    
    	//now log:
    	t.Logf("failed:\n\t%+v", err)
    	t.Logf("HTTP Code: %d", errors.HTTPCode(err)) //will return smallest code in the stack, 0 if none.
    
    	//you can wrap any error with an HTTP code:
    	err = errors.Errorf("failed to connect to db")
    	//err = errors.HTTP(http.StatusInternalServerError, err)
    
    	//and one more to give a lower code again
    	err = errors.HTTP(http.StatusInsufficientStorage, err, "jissis this is bad!")
    
    	//and higher again -... many layers...
    	err = errors.HTTP(http.StatusNotFound, err, "terrible mistake")
    
    	//or if you know you are creating the HTTP error, do it as one statement
    	err = errors.HTTP(http.StatusBadRequest, err, "failed to get user")
    
    	//now log:
    	t.Logf("failed:\n\t%+v", err)
    	t.Logf("HTTP Code: %d", errors.HTTPCode(err)) //will return smallest code in the stack, 0 if none.
    
    	err = errors.Errorf("failed to connect to db")
    	err = errors.Wrapf(err, "failed to connect to db")
    	t.Logf("failed:\n\t%+v", err)
    	t.Logf("HTTP Code: %d", errors.HTTPCode(err)) //will return smallest code in the stack, 0 if none.
    }