code: private boolean loadCustomErrorPage(
          CmsObject cms,
          HttpServletRequest req,
          HttpServletResponse res,
          String rootPath) {
  
          try {
  
              // get the site of the error page resource
              CmsSite errorSite = OpenCms.getSiteManager().getSiteForRootPath(rootPath);
              cms.getRequestContext().setSiteRoot(errorSite.getSiteRoot());
              String relPath = cms.getRequestContext().removeSiteRoot(rootPath);
              if (cms.existsResource(relPath)) {
                  cms.getRequestContext().setUri(relPath);
                  OpenCms.getResourceManager().loadResource(cms, cms.readResource(relPath), req, res);
                  return true;
              } else {
                  return false;
              }
          } catch (Throwable e) {
              // something went wrong log the exception and return false
              LOG.error(e.getMessage(), e);
              return false;
          }
      }
docs: Tries to load the custom error page at the given rootPath.
  @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 rootPath the VFS root path to the error page resource
  @return a flag, indicating if the error page could be loaded