A formula for the wavefront of a wave reflected from a diffraction grating with an arbitrary surface profile, as well as with arbitrary non-equidistant and non-parallel grooves was obtained. It was shown that the wavefront of the reflected wave can change significantly when the grating is rotated by 180 degrees around the normal. Surface imperfection and groove pattern imperfection are equivalent for monochromatic radiation; whereas for broadband radiation, the two imperfections lead to similar but different space-time coupling effects. For holographic diffraction gratings, wavefront distortions are the sum of distortions caused by grating surface imperfection and the total surface imperfections of the optics used for writing the grating. The second summand is inversely proportional to the frequency of the radiation used to write the grating. The requirements for the flatness of the optics used to write the grating are always more stringent than the requirements for the flatness of the grating.