Custom error code class and numbers for Postgres stored functions
The SQL standard gives you a guideline:


  The character string value returned in an SQLSTATE parameter comprises a 2-character class value followed
  by a 3-character subclass value, each with an implementation-defined character set that has a one-octet character
  encoding form and is restricted to &lt;digit&gt;s and &lt;simple Latin upper case letter&gt;s. Table 33, &ldquo;SQLSTATE class
  and subclass values&rdquo;, specifies the class value for each condition and the subclass value or values for each class
  value.
  
  Class values that begin with one of the &lt;digit&gt;s '0', '1', '2', '3', or '4' or one of the &lt;simple Latin upper case letter&gt;s
  'A', 'B', 'C', 'D', 'E', 'F', 'G', or 'H' are returned only for conditions defined in ISO/IEC 9075 or in any other
  International Standard. The range of such class values is called standard-defined classes. Some such class codes
  are reserved for use by specific International Standards, as specified elsewhere in this Clause. Subclass values
  associated with such classes that also begin with one of those 13 characters are returned only for conditions
  defined in ISO/IEC 9075 or some other International Standard. The range of such subclass values is called
  standard-defined subclasses. Subclass values associated with such classes that begin with one of the &lt;digit&gt;s
  '5', '6', '7', '8', or '9' or one of the &lt;simple Latin upper case letter&gt;s 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  'U', 'V', 'W', 'X', 'Y', or 'Z' are reserved for implementation-defined conditions and are called implementation-defined subclasses.
  
  Class values that begin with one of the &lt;digit&gt;s '5', '6', '7', '8', or '9' or one of the &lt;simple Latin upper case letter&gt;s
  'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', or 'Z' are reserved for implementation-defined
  exception conditions and are called implementation-defined classes. All subclass values except '000', which
  means no subclass, associated with such classes are reserved for implementation-defined conditions and are
  called implementation-defined subclasses. An implementation-defined completion condition shall be indicated
  by returning an implementation-defined subclass in conjunction with one of the classes successful completion, warning, or no data.


If your exception matches one of the SQLSTATEs in Appendix A of the PostgreSQL documentation, simply use that SQLSTATE.

If you need to use your own SQLSTATE, let it begin with any of 5 to 9 or I to Z.

If you need to define a custom warning, use an SQLSTATE that starts with 01 and whose third character is any of 5 to 9 or I to Z.