So I have a terrier for years. He's a lovely thing. He's always rolled on his back for cuddles, since day one. And will be all snuggly and affectionate. He's very much a lap dog, and very cuddly. Wants to be petted all the time.
About a year ago, out of the blue, after years with us, he started snapping the odd time. We used to pet him in his bed when we'd come home from work - he would do his usual lie on his back and literally all sometimes we'd be doing is shaking his paw, and he snapped a couple of times. So we figured it was some "bed" thing and now when we come in we say hello but don't go near him in the bed. If he gets out and says hi, we pet him.
Then what happened is that he'd come over and say hi, and flop down on his back all "pet me pet me". So we would. But he snapped a couple of times doing that too. Which was a bit baffling. So now we basically give him a pat on the shoulder if he gets out of bed to say hi, but as soon as he flops down on his back we stop, as we just aren't sure of whats going on there.
So because we've been careful, we thought we'd nipped it in the bud and it hasn't happened in ages. We were congratulating ourselves!
So this morning I'm in bed, and he comes upstairs to say hi and have a cuddle. Awww...right? He likes to burrow into the duvet all adorably, and he was on his back beside me and I was patting his right shoulder and telling him what a sweet dog he was, and he just hopped up and bit me! All bared teeth! WTF???
I'm baffled - no warning signs at all. Of course I couldn't see his tail cos of the duvet, but he seemed alert and engaged at all times with his mouth lolling wide open. What the hell did I do?
And I have to add that he rolls on his back for cuddles 100% of the time - that can't possibly have been the common denominator. We've been cuddling him that way all his life!
Now in the moments before that he was on active guard duty downstairs - strange builders making noise next door. So may have been a bit keyed up from that? But he hadn't been barking. But FFS why should I suffer because of that? And maybe that had nothing to do with it at all?
I thought we had it figured out, but apparently not. 
By the way my reaction was to roar at him to get out of the room. He ran out and I closed the door behind him.
Fuck, have to bring him for a bloody walk now and all. I know I have to be normal as they don't associate stuff with earlier events, but I'm so upset.
I need to identify the triggers here. I don't want to be wary of ever petting my dog.