Search in sources :

Example 1 with ErrorResponse

use of me.retrodaredevil.couchdbjava.response.ErrorResponse in project solarthing by wildmountainfarms.

the class SolarMain method doMain.

private static int doMain(String[] args) {
    String logMessage = "Beginning main. Jar: " + getJarInfo();
    LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "[LOG] " + logMessage);
    System.out.println("[stdout] " + logMessage);
    System.err.println("[stderr] " + logMessage);
    Cli<CommandOptions> cli = CliFactory.createCli(CommandOptions.class);
    final CommandOptions commandOptions;
    try {
        commandOptions = cli.parseArguments(args);
    } catch (ArgumentValidationException ex) {
        System.out.println(cli.getHelpMessage());
        if (ex instanceof HelpRequestedException) {
            return 0;
        }
        LOGGER.error(SolarThingConstants.SUMMARY_MARKER, ex.getMessage());
        LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "(Fatal)Incorrect args");
        return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
    }
    if (commandOptions.getBaseConfigFile() != null) {
        return doMainCommand(commandOptions, commandOptions.getBaseConfigFile());
    }
    if (commandOptions.getCouchDbSetupFile() != null) {
        final DatabaseConfig config;
        try {
            config = ConfigUtil.MAPPER.readValue(commandOptions.getCouchDbSetupFile(), DatabaseConfig.class);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Problem reading CouchDB database settings file.");
            return SolarThingConstants.EXIT_CODE_INVALID_CONFIG;
        }
        DatabaseSettings settings = config.getSettings();
        if (!(settings instanceof CouchDbDatabaseSettings)) {
            System.err.println("Must be CouchDB database settings!");
            return SolarThingConstants.EXIT_CODE_INVALID_CONFIG;
        }
        try {
            return CouchDbSetupMain.createFrom((CouchDbDatabaseSettings) settings).doCouchDbSetupMain();
        } catch (CouchDbException e) {
            if (e instanceof CouchDbCodeException) {
                ErrorResponse error = ((CouchDbCodeException) e).getErrorResponse();
                if (error != null) {
                    System.err.println(error.getError());
                    System.err.println(error.getReason());
                }
            }
            throw new RuntimeException(e);
        }
    }
    List<String> legacyArguments = commandOptions.getLegacyOptionsRaw();
    if (legacyArguments == null || legacyArguments.isEmpty()) {
        System.err.println(cli.getHelpMessage());
        return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
    }
    System.err.println("Invalid sub command: " + legacyArguments.get(0));
    return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
}
Also used : CouchDbException(me.retrodaredevil.couchdbjava.exception.CouchDbException) ErrorResponse(me.retrodaredevil.couchdbjava.response.ErrorResponse) DatabaseSettings(me.retrodaredevil.solarthing.config.databases.DatabaseSettings) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) HelpRequestedException(com.lexicalscope.jewel.cli.HelpRequestedException) CouchDbCodeException(me.retrodaredevil.couchdbjava.exception.CouchDbCodeException) ArgumentValidationException(com.lexicalscope.jewel.cli.ArgumentValidationException)

Aggregations

ArgumentValidationException (com.lexicalscope.jewel.cli.ArgumentValidationException)1 HelpRequestedException (com.lexicalscope.jewel.cli.HelpRequestedException)1 CouchDbCodeException (me.retrodaredevil.couchdbjava.exception.CouchDbCodeException)1 CouchDbException (me.retrodaredevil.couchdbjava.exception.CouchDbException)1 ErrorResponse (me.retrodaredevil.couchdbjava.response.ErrorResponse)1 DatabaseSettings (me.retrodaredevil.solarthing.config.databases.DatabaseSettings)1 CouchDbDatabaseSettings (me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings)1