package response

import (
	"errors"
	"net/http"
)

func RespondWithError(w http.ResponseWriter, r *http.Request, err error, msg string) {
	if err != nil {
		code := ErrUnauthorized.Code
		status := http.StatusUnauthorized

		if errors.Is(err, ErrExpiryToken) {
			code = ErrExpiryToken.Code
			status = ErrExpiryToken.HttpCode
		}

		RespondJsonErrorWithCode(r.Context(), w, err, code, status, msg)
	} else {
		RespondJsonErrorWithCode(r.Context(), w, nil,
			ErrUnauthorized.Code, http.StatusUnauthorized, msg)
	}
}
