The Stack Snippet I included in this question sometimes produces the QR code as expected, but sometimes throws an error.
When I was preparing the code snippet, it worked well. But now, after it has been posted, it isn't working reliably.
What did I do wrong, and how can I fix it?
