I am struggling with following challenge in my free time programming project $-$ how is it possible to make reflection vector that reflects along normal with angle that is not larger than some $\alpha$?

I have already seen classical reflection formula $ r = d - 2 (d \cdot n) n $, which unfortunately does not provide answer to my problem.
I tried to fiddle around with JS atan2() function, as described here, but it didn't work for all cases and I would appreciate some elegant general solution instead of patching special cases.
Thank you in advance.
I'm assuming $n$ is a unit vector.
If $d \cdot n \le 0$: ERROR
$\alpha = \arccos(d \cdot n)$
If $\alpha < \alpha_\text{max}$, return $r = d - 2(d \cdot n) n$
Else, $v = d - (d \cdot n) n$ is a vector perpendicular to $n$, on the $d$ side of the $nd$ plane.
Let $w = \frac{1}{\|v\|} v$.
Set $r = \cos(\alpha_\text{max}) n - \sin(\alpha_\text{max}) w$; return $r$.
What I've done is test as @mvw suggests, and when the test fails, I've computed a vector in the $dn$-plane whose angle is the max angle, and then computed its reflection.