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.
}