# SpawnError.java

    /*
    ** Module   : SpawnError.java
    ** Abstract : This class defines error codes that describe errors occurring during new client spawn.
    **
    ** Copyright (c) 2023-2025, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- -------------------------------Description------------------------------------
    ** 001 SBI 20230615 Added NO_AVAILABLE_PORTS_ERR_CODE, CAN_NOT_SPAWN_REMOTE_ERR_CODE,
    **                  CAN_NOT_SET_REMOTE_LAUNCH_OPTION_ERR_CODE.
    ** 002 CA  20230704 Added UNKNOWN_ERROR.
    ** 003 GBB 20230825 Complete rework of the handling of spawning exit code.
    **                  Resolving the message of the error code moved here.
    **                  SpawnErrorCode renamed to SpawnError.
    ** 004 RNC 20250127 Added SESSION_LOCKED.
    ** 005 AP  20250305 Added EXCEEDED_TIMEOUT.
    ** 006 GBB 20250403 Added utility method isInvalidOsCredentials to check if a code is for invalid credentials.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.main;

    import java.util.*;

    /**
     * Class to collect all spawn error codes, that don't clash with spawn.c and winspawn.c exit codes.
     * The enum constructor takes pairs of error codes and error messages. The messages are displayed to the
     * web users with handling POST requests, processed by {@link WebDriverHandler#handleStartClientRequest}.
     */
    public enum SpawnError
    {
       // spawn.c UNIX or Linux client platforms
       UNIX_INVALID_CRED_1(1, "Invalid OS username or password."),
       UNIX_INVALID_CRED_2(2, "Invalid OS username or password."),
       UNIX_ROOT_NOT_ALLOWED(3, "OS user `root` not allowed."),
       UNIX_PAM_FAILED(4, "PAM authentication failed."),
       UNIX_SPAWNER_NO_PERM(-1, "Spawner has no root access permissions."),

       // winspawn.c Windows client platforms
       WIN_INVALID_CRED(-2, "Invalid OS username or password."),
       WIN_CANT_CREATE_ENV(-3, "Cannot create user environment."),
       WIN_CANT_GET_PROFILE(-4, "Cannot get user profile."),
       WIN_CANT_SET_DIR(-5, "Cannot set working directory."),
       WIN_CANT_CREATE_PROC(-6, "Cannot create process for OS user."),
       WIN_CANT_LOAD_LIBS(-7, "Cannot load dynamic libraries."),

       DEFAULT_SPAWN_ERR(-999999, "The spawn process failed with %d error. Please see spawner logs."),

       // ClientSpawner
       NO_AVAILABLE_PORTS_ERR_CODE(-99, "There are no available ports on running host."),
       CAN_NOT_SPAWN_REMOTE_ERR_CODE(-98, "Cannot spawn a new remote client."),
       GENERIC(-97, "Cannot spawn a new client process."),
       SESSION_LOCKED(-96, "New sessions are disabled on the FWD server."),
       EXCEEDED_TIMEOUT(-95, "Spawner launch procedure exceeded the timeout.");

       /** Error message code. */
       private int code;

       /** Error message. */
       private String msg;

       /**
        * The constructor for SpawnError enum.
        *
        * @param    code
        *           The numeric code for the error.
        * @param    msg
        *           The string representation of the error.
        */
       SpawnError(int code, String msg)
       {
          this.code = code;
          this.msg = msg;
       }

       /**
        * Getter for {@link #code}.
        *
        * @return   See above.
        */
       public int getCode()
       {
          return code;
       }

       /**
        * Getter for {@link #msg}.
        *
        * @return   See above.
        */
       public String getMsg()
       {
          return msg;
       }

       /**
        * Returns the string representation of the error.
        *
        * @return   See above.
        */
       @Override
       public String toString()
       {
          return msg;
       }

       /**
        * Returns the error matching the provided code. If none, the optional is empty
        *
        * @param    code
        *           The code to look for.
        *
        * @return   See above.
        */
       public static Optional<SpawnError> get(int code)
       {
          return Arrays.stream(SpawnError.values())
                .parallel()
                .filter(error -> error.code == code)
                .findAny();
       }

       /**
        * Returns if the exit code implies invalid OS credentials. Applicable only when no SSO enabled.
        * Otherwise the actual credentials entered by the end-user are not OS user / pass and the error code is
        * a misconfiguration on the admin side. The message should not be shown to the end-user.
        *
        * @return   <code>true</code> if the exit code implies invalid credentials, <code>false</code> otherwise.
        */
       public boolean isInvalidOsCredentials()
       {
          return this == UNIX_INVALID_CRED_1 || this == UNIX_INVALID_CRED_2 || this == WIN_INVALID_CRED;
       }

       /**
        * Returns if the exit code implies invalid OS credentials. Applicable only when no SSO enabled.
        * Otherwise the actual credentials entered by the end-user are not OS user / pass and the error code is
        * a misconfiguration on the admin side. The message should not be shown to the end-user.
        *
        * @param    code
        *           The code to check.
        *
        * @return   <code>true</code> if the exit code implies invalid credentials, <code>false</code> otherwise.
        */
       public static boolean isInvalidOsCredentials(int code)
       {
          return code == UNIX_INVALID_CRED_1.code ||
             code == UNIX_INVALID_CRED_2.code ||
             code == WIN_INVALID_CRED.code;
       }

       /**
        * Returns the error message corresponding to the provided code or the default spawn error message.
        *
        * @param    code
        *           Exit code.
        *
        * @return   See above.
        */
       public static String getSpawnMsgOrDefault(int code)
       {
          return String.format(get(code).orElse(DEFAULT_SPAWN_ERR).msg, code);
       }
    }