You have fucked up. You know that you have fucked up. Ideally, you have figured this out before anyone else has but even if you haven't, this guide will still serve as a shovel with which you may cut stairs into the side of the hole of despair in which you find yourself.
First, determine whether or not you can fix your fuckup on your own without intervention, and how long that will take. If the fix is trivial and guaranteed to work, effect it and move on. Should it hint at taking enough time to fix that someone else might reasonably discover your mistake, get on the phone immediately, the conversation should sound something like: "Sorry boss, I hosed A in manner C, effects are Y, I am considering fixes P and Q, if you have something else to suggest please do so now otherwise I have to go clean up my mess". In the unlikely event that you fix your mistake without anyone spotting it, for the sake of honesty you should also tell your immediate superior, and in more or less the same fashion: "I hosed A by doing C, effects were Y. I fixed it with P, and the effect upon our shared goals is D. Apologies for the fuckup, ship's upright again." In all cases you must own your mess and its resolution, lest you develop the reputation for dodging either responsibility or work.
Your response to the unleashing of your immediate superior's rage in both the case where he's discovered that you've fucked up before you have and the case where he's still wrathful after the bleeding's staunched must be identical: eg to stand there, spine erect and accept whatever lashings of tongue or leather he decides to hand out. Under no circumstances may you engage in weepy self-flagellation, a reprehensible and cowardly behavior I've seen trotted out in an effort to bypass the critical phase of punishment.
Resolving your fuckup means your ass is getting whooped, and your having committed the crime disqualifies you on the spot from determining the appropriate degree of whoopin'. Either the universe or your superior officer will determine the appropriate punishment for your work, and you will bear it as best you can.
Weepily declaring that you're a piece of shit and so sorry and stupid &c. &c. is a blatant tactic to coerce the weak-willed into forgiving your crimes because you are obviously so contrite and already feel so bad. Insofar as you attempt to escape your due punishment, you are a coward1. It may have worked on your father, but it will never work on me. If you are not willing to bear the punishment from those to whom you report, you are not worthy to report to them. Moreover, your boss will forever resent you for stealing the yelling at you from him, and you'll forever suffer from not getting yelled at (although at least in America, your reputation in the company will erode further, and you'll simply "fail to thrive", as we say of babies who suckle vigorously but whose stressful home environs forestall secretion of enzymes humans need to digest breast milk. Yes, this means that maturation into any sort of respectable person entails a boatload of punishment either at the hands of the universe or your superiors).
Once you've taken your licks and fixed the problem, the final step in salvaging your reputation is conveying to everyone involved that you know the following things: a) the mistakes you made while fucking up b) what you should have done instead, and c) several classes of fuckup that having made this one has taught you to not make in the future.
It ain't hard: admit your mistakes, work your ass off to correct them, take your licks like a man, and demonstrate to everyone involved that you're a better person for the trials. This may make the difference between "ha ha, remember that one time you shared your passphrase with the world?" and "yeah no, you can't trust him with anything trickier than addition."
- I assume here you want to maintain good relations. If you're a thief or charlatan I have utterly no idea why you'd read this piece. Simply start running, and don't stop until you've found your next victim. [↩]