Try doing an SMC Reset (power management) procedure. For an Intel iMac, this is basically unplugging the power cord and reconnecting it, following a specific procedure described in this document
Reset the System Management Controller (SMC) on your Mac - Apple Support (see section Reset the SMC on Mac desktop computers)
Also take a look at how your iMac is connected to power. If it's connected to a power strip (surge suppressor) that is like an "octopus" under your desk, along with an external drive or two, a printer, assorted power adapters for other peripherals, secondary display, speakers, and your desk lamp, connect your iMac to a separate wall outlet. The amount of current going through one power outlet is limited, and your iMac could have such errors if it's not getting enough power to operate. The occurrence of the problem could be sporadic or repeated, depending the total power draw at any given moment.