code: private boolean tryCustomErrorPage(CmsObject cms, HttpServletRequest req, HttpServletResponse res, int errorCode) {
  
          String siteRoot = OpenCms.getSiteManager().matchRequest(req).getSiteRoot();
          CmsSite site = OpenCms.getSiteManager().getSiteForSiteRoot(siteRoot);
          if (site != null) {
              // store current site root and URI
              String currentSiteRoot = cms.getRequestContext().getSiteRoot();
              String currentUri = cms.getRequestContext().getUri();
              try {
                  if (site.getErrorPage() != null) {
                      String rootPath = site.getErrorPage();
                      if (loadCustomErrorPage(cms, req, res, rootPath)) {
                          return true;
                      }
                  }
                  String rootPath = CmsStringUtil.joinPaths(siteRoot, "/.errorpages/handle" + errorCode + ".html");
                  if (loadCustomErrorPage(cms, req, res, rootPath)) {
                      return true;
                  }
              } finally {
                  cms.getRequestContext().setSiteRoot(currentSiteRoot);
                  cms.getRequestContext().setUri(currentUri);
              }
          }
          return false;
      }
docs: Tries to load a site specific error page. If
  @param cms {@link CmsObject} used for reading the resource (site root and uri get adjusted!)
  @param req the current request
  @param res the current response
  @param errorCode the error code to display
  @return a flag, indicating if the custom error page could be loaded.